|
1 | 1 | """
|
2 | 2 | This module contains a list of the known Twitter V2+ API expansions and fields
|
3 |
| -for each expansion, and a function for "flattening" a result set, including all |
4 |
| -expansions inline |
| 3 | +for each expansion, and a function flatten() for "flattening" a result set, |
| 4 | +including all expansions inline. |
5 | 5 |
|
| 6 | +ensure_flattened() can be used in tweet processing programs that need to make |
| 7 | +sure that data is flattened. |
6 | 8 | """
|
7 | 9 |
|
8 | 10 | from collections import defaultdict
|
@@ -116,11 +118,12 @@ def extract_includes(response, expansion, _id="id"):
|
116 | 118 | def flatten(response):
|
117 | 119 | """
|
118 | 120 | Flatten an API response by moving all "included" entities inline with the
|
119 |
| - tweets they are referenced from. Expects an entire page response from the |
120 |
| - API (data, includes, meta) and will raise a ValueError if what is passed in |
121 |
| - does not appear to be an API response. Returns empty objects for things |
122 |
| - missing in includes. Returns a list of dictionaries where each dictionary |
123 |
| - is a tweet. |
| 121 | + tweets they are referenced from. flatten expects an entire page response |
| 122 | + from the API (data, includes, meta) and will raise a ValueError if what is |
| 123 | + passed in does not appear to be an API response. It will return a list of |
| 124 | + dictionaries where each dictionary represents a tweet. Empty objects will |
| 125 | + be returned for things that are missing in includes, which can happen when |
| 126 | + protected or delete users or tweets are referenced. |
124 | 127 | """
|
125 | 128 |
|
126 | 129 | # Users extracted both by id and by username for expanding mentions
|
@@ -220,10 +223,16 @@ def expand_payload(payload):
|
220 | 223 |
|
221 | 224 | def ensure_flattened(data):
|
222 | 225 | """
|
223 |
| - Will ensure that the supplied data is "flattened". The supplied data can be a |
| 226 | + Will ensure that the supplied data is "flattened". The input data can be a |
224 | 227 | response from the Twitter API, a list of tweet dictionaries, or a single tweet
|
225 |
| - dictionary. An exception will be thrown if the supplied data is not |
226 |
| - recognizable or it cannot be flattened. |
| 228 | + dictionary. It will always return a list of tweet dictionaries. A ValueError |
| 229 | + will be thrown if the supplied data is not recognizable or it cannot be |
| 230 | + flattened. |
| 231 | +
|
| 232 | + ensure_flattened is designed for use in twarc plugins and other tweet |
| 233 | + processing applications that want to operate on a stream of tweets, and |
| 234 | + examine included entities like users and tweets without hunting and |
| 235 | + pecking in the response data. |
227 | 236 | """
|
228 | 237 | if isinstance(data, dict) and 'data' in data:
|
229 | 238 | return flatten(data)
|
|
0 commit comments