twitwi 0.20.0__tar.gz → 0.21.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. twitwi-0.21.1/PKG-INFO +436 -0
  2. twitwi-0.21.1/README.md +409 -0
  3. twitwi-0.21.1/setup.py +22 -0
  4. twitwi-0.21.1/test/bluesky/__init__.py +0 -0
  5. twitwi-0.21.1/test/bluesky/formatters_test.py +101 -0
  6. twitwi-0.21.1/test/bluesky/normalizers_test.py +130 -0
  7. {twitwi-0.20.0 → twitwi-0.21.1}/twitwi/__init__.py +19 -2
  8. {twitwi-0.20.0 → twitwi-0.21.1}/twitwi/anonymizers.py +3 -9
  9. twitwi-0.21.1/twitwi/bluesky/__init__.py +16 -0
  10. twitwi-0.21.1/twitwi/bluesky/constants.py +19 -0
  11. twitwi-0.21.1/twitwi/bluesky/formatters.py +29 -0
  12. twitwi-0.21.1/twitwi/bluesky/normalizers.py +686 -0
  13. twitwi-0.21.1/twitwi/bluesky/types.py +135 -0
  14. twitwi-0.21.1/twitwi/bluesky/utils.py +110 -0
  15. twitwi-0.21.1/twitwi/constants.py +404 -0
  16. {twitwi-0.20.0 → twitwi-0.21.1}/twitwi/exceptions.py +8 -1
  17. {twitwi-0.20.0 → twitwi-0.21.1}/twitwi/formatters.py +35 -37
  18. twitwi-0.21.1/twitwi/normalizers.py +871 -0
  19. twitwi-0.21.1/twitwi/utils.py +115 -0
  20. twitwi-0.21.1/twitwi.egg-info/PKG-INFO +436 -0
  21. {twitwi-0.20.0 → twitwi-0.21.1}/twitwi.egg-info/SOURCES.txt +10 -1
  22. {twitwi-0.20.0 → twitwi-0.21.1}/twitwi.egg-info/requires.txt +1 -0
  23. {twitwi-0.20.0 → twitwi-0.21.1}/twitwi.egg-info/top_level.txt +1 -0
  24. twitwi-0.20.0/PKG-INFO +0 -156
  25. twitwi-0.20.0/README.md +0 -130
  26. twitwi-0.20.0/setup.py +0 -23
  27. twitwi-0.20.0/twitwi/constants.py +0 -430
  28. twitwi-0.20.0/twitwi/normalizers.py +0 -807
  29. twitwi-0.20.0/twitwi/utils.py +0 -87
  30. twitwi-0.20.0/twitwi.egg-info/PKG-INFO +0 -156
  31. {twitwi-0.20.0 → twitwi-0.21.1}/LICENSE.txt +0 -0
  32. {twitwi-0.20.0 → twitwi-0.21.1}/setup.cfg +0 -0
  33. {twitwi-0.20.0 → twitwi-0.21.1}/twitwi.egg-info/dependency_links.txt +0 -0
  34. {twitwi-0.20.0 → twitwi-0.21.1}/twitwi.egg-info/zip-safe +0 -0
twitwi-0.21.1/PKG-INFO ADDED
@@ -0,0 +1,436 @@
1
+ Metadata-Version: 2.4
2
+ Name: twitwi
3
+ Version: 0.21.1
4
+ Summary: A collection of Twitter-related helper functions for python.
5
+ Home-page: http://github.com/medialab/twitwi
6
+ Author: Béatrice Mazoyer, Guillaume Plique, Benjamin Ooghe-Tabanou
7
+ Author-email: guillaume.plique@sciencespo.fr
8
+ License: MIT
9
+ Keywords: twitter
10
+ Requires-Python: >=3.8
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE.txt
13
+ Requires-Dist: pytz>=2019.3
14
+ Requires-Dist: ural>=0.31.1
15
+ Requires-Dist: python-dateutil>=2.9.0
16
+ Dynamic: author
17
+ Dynamic: author-email
18
+ Dynamic: description
19
+ Dynamic: description-content-type
20
+ Dynamic: home-page
21
+ Dynamic: keywords
22
+ Dynamic: license
23
+ Dynamic: license-file
24
+ Dynamic: requires-dist
25
+ Dynamic: requires-python
26
+ Dynamic: summary
27
+
28
+ [![Build Status](https://github.com/medialab/twitwi/workflows/Tests/badge.svg)](https://github.com/medialab/twitwi/actions)
29
+
30
+ # Twitwi
31
+
32
+ A collection of Twitter & Bluesky related helper functions for Python intended to preprocess JSON payloads from the platforms' APIs and return "normalized" flat versions in order to cleanup and optimize some fields before storing them into consistent tabular formats.
33
+
34
+ A description of the "normalized" fields [for Twitter is available here](twitwi/constants.py) and [for Bluesky there](twitwi/bluesky/types.py).
35
+
36
+ It also provides a few convenient tools to anonymize tweets data, convert it into other formats, or extract datetime/timestamp from Twitter IDs (without querying it to Twitter).
37
+
38
+ ## Installation
39
+
40
+ You can install `twitwi` with pip with the following command:
41
+
42
+ ```
43
+ pip install twitwi
44
+ ```
45
+
46
+ ## Requirements
47
+
48
+ - Python 3.8 +
49
+ - [pytz](https://pypi.org/project/pytz/) (for timezones handling)
50
+ - [ural](https://github.com/medialab/ural?tab=readme-ov-file#ural) (for urls cleaning)
51
+
52
+ ## Usage
53
+
54
+ **Bluesky (within `twitwi.bluesky`)**
55
+
56
+ *Normalization functions*
57
+
58
+ * [normalize_profile](#normalize_profile)
59
+ * [normalize_post](#normalize_post)
60
+
61
+ *Formatting functions*
62
+
63
+ * [transform_profile_into_csv_dict](#transform_profile_into_csv_dict)
64
+ * [transform_post_into_csv_dict](#transform_post_into_csv_dict)
65
+ * [format_profile_as_csv_row](#format_profile_as_csv_row)
66
+ * [format_post_as_csv_row](#format_post_as_csv_row)
67
+
68
+ *Useful constants (under `twitwi.bluesky.constants`)*
69
+
70
+ * [PROFILE_FIELDS](#profile_fields)
71
+ * [POST_FIELDS](#post_fields)
72
+
73
+ *Examples*
74
+
75
+ ```python
76
+
77
+ # Working with Blueksy posts payloads coming directly from the API, either from clasical payloads,
78
+ # for instance via the routes app.bsky.feed.getPosts https://docs.bsky.app/docs/api/app-bsky-feed-get-posts
79
+ # or app.bsky.feed.searchPosts https://docs.bsky.app/docs/api/app-bsky-feed-search-posts
80
+ # or from feeds payloads, such as the route app.bsky.feed.getAuthorFeed https://docs.bsky.app/docs/api/app-bsky-feed-get-author-feed
81
+
82
+ from twitwi.bluesky import normalize_post
83
+
84
+ normalized_posts = []
85
+ for post_data in posts_payload_from_API:
86
+ normalized_posts.append(normalize_post(post_data))
87
+
88
+ # Or to convert found datetimes into a local chosen timezone
89
+ from pytz import timezone
90
+ paris_tz = timezone('Europe/Paris')
91
+ normalized_posts.append(normalize_post(post_data, locale=paris_tz))
92
+
93
+ # Or to also produce full metadata for other posts embedded such as quotes or posts answered to:
94
+ normalized_posts += normalize_post(post_data, extract_referenced_posts=True) # Returns a list of dicts
95
+
96
+ # Then, saving normalized profiles into a CSV using DictWriter:
97
+
98
+ from csv import DictWriter
99
+ from twitwi.bluesky.constants import POST_FIELDS
100
+ from twitwi.bluesky import transform_post_into_csv_dict
101
+
102
+ with open("normalized_bluesky_posts.csv", "w") as f:
103
+ w = csv.DictWriter(f, fieldnames=POST_FIELDS)
104
+ w.writeheader()
105
+ for p in normalized_posts:
106
+ transform_post_into_csv_dict(p) # The function returns nothing, p has been mutated
107
+ w.writerow(p)
108
+
109
+ # Or using the basic CSV writer:
110
+
111
+ from csv import writer
112
+ from twitwi.bluesky import format_post_as_csv_row
113
+
114
+ with open("normalized_bluesky_posts.csv", "w") as f:
115
+ w = csv.writer(f)
116
+ w.writerow(POST_FIELDS)
117
+ for p in normalized_posts:
118
+ w.writerow(format_post_as_csv_row(p))
119
+
120
+
121
+ # Similarily, working with Bluesky user profiles coming directly from the API
122
+ # for instance via the route app.bsky.actor.getProfiles https://docs.bsky.app/docs/api/app-bsky-actor-get-profiles
123
+
124
+ from twitwi.bluesky import normalize_profile
125
+
126
+ normalized_profiles = []
127
+ for profile_data in profiles_payload_from_API:
128
+ normalized_profiles.append(normalize_profile(profile_data))
129
+
130
+ # Or in the local timezone
131
+ normalized_profiles.append(normalize_profile(profile_payload, locale=paris_tz))
132
+
133
+ # Then saving as a CSV with csv.writer or csv.DictWriter similarily:
134
+
135
+ from twitwi.bluesky.constants import PROFILE_FIELDS
136
+ from twitwi.bluesky import transform_profile_into_csv_dict, format_profile_as_csv_row
137
+
138
+ with open("normalized_bluesky_profiles.csv", "w") as f:
139
+ w = csv.DictWriter(f, fieldnames=PROFILE_FIELDS)
140
+ w.writeheader()
141
+ for p in normalized_profiles:
142
+ transform_profile_into_csv_dict(p) # The function returns nothing, p has been mutated
143
+ w.writerow(p)
144
+
145
+ with open("normalized_bluesky_profiles.csv", "w") as f:
146
+ w = csv.writer(f)
147
+ w.writerow(PROFILE_FIELDS)
148
+ for p in normalized_profiles:
149
+ w.writerow(format_profile_as_csv_row(p))
150
+ ```
151
+
152
+
153
+ **Twitter (within `twitwi`)**
154
+
155
+ *Normalization functions*
156
+
157
+ * [normalize_user](#normalize_user)
158
+ * [normalize_tweet](#normalize_tweet)
159
+ * [normalize_tweets_payload_v2](#normalize_tweets_payload_v2)
160
+
161
+ *Formatting functions*
162
+
163
+ * [transform_user_into_csv_dict](#transform_user_into_csv_dict)
164
+ * [transform_tweet_into_csv_dict](#transform_tweet_into_csv_dict)
165
+ * [format_user_as_csv_row](#format_user_as_csv_row)
166
+ * [format_tweet_as_csv_row](#format_tweet_as_csv_row)
167
+ * [apply_tcat_format](#apply_tcat_format)
168
+
169
+ *Useful constants (under `twitwi.constants`)*
170
+
171
+ * [USER_FIELDS](#user_fields)
172
+ * [TWEET_FIELDS](#tweet_fields)
173
+
174
+ *Extra functions*
175
+
176
+ * [anonymize_normalized_tweet](#anonymize_normalized_tweet)
177
+ * [get_timestamp_from_id](#get_timestamp_from_id)
178
+ * [get_dates_from_id](#get_dates_from_id)
179
+
180
+
181
+ ### normalize_profile
182
+
183
+ Function taking a nested dict describing a user profile from Bluesky's JSON payload and returning a flat "normalized" dict composed of all [PROFILE_FIELDS](#profile_fields) keys.
184
+
185
+ Will return datetimes as UTC but can take an optional second `locale` argument as a [`pytz`](https://pypi.org/project/pytz/) string timezone.
186
+
187
+ *Arguments*
188
+
189
+ * **data** *(dict)*: user profile data payload coming from Bluesky API.
190
+ * **locale** *(pytz.timezone as str, optional)*: timezone used to convert dates. If not given, will default to UTC.
191
+
192
+ ### normalize_post
193
+
194
+ Function taking a nested dict describing a post from Bluesky's JSON payload and returning a flat "normalized" dict composed of all [POST_FIELDS](#post_fields) keys.
195
+
196
+ Will return datetimes as UTC but can take an optional last `locale` argument as a `pytz` string timezone.
197
+
198
+ When setting `extract_referenced_posts` to `True` it will instead return a list of dicts including the desired post as well as each referenced ones such as quoted posts and potentially parent posts of a conversation when the data comes from a Bluesky `feed` payload.
199
+
200
+ *Arguments*
201
+
202
+ * **payload** *(dict)*: post or feed data payload coming from Bluesky API.
203
+ * **locale** *(pytz.timezone as str, optional)*: timezone used to convert dates. If not given, will default to UTC.
204
+ * **extract_referenced_posts** *(bool, optional)*: whether to return in the output, in addition to the post to be normalized, also normalized data for each other referenced posts found in the payload data (including potentially other quoted posts as well as the parent and root posts of a thread if the post comes as an answer to another one). If `False`, the function will return a `dict`, if `True` a `list` of `dict`. Defaults to `False`.
205
+ * **collection_source** *(string, optional)*: An optional information to add within the `collected_via` field of the normalized post to indicate whence it was collected.
206
+
207
+ ### transform_profile_into_csv_dict
208
+
209
+ Function transforming (i.e. mutating, so beware) a given normalized Bluesky profile into a suitable dict able to be written by a `csv.DictWriter` as a row.
210
+
211
+ Will convert list elements of the normalized data into a string with all elements separated by the `|` character, which can be changed using an optional `plural_separator` argument.
212
+
213
+ ### transform_post_into_csv_dict
214
+
215
+ Function transforming (i.e. mutating, so beware) a given normalized Bluesky post into a suitable dict able to be written by a `csv.DictWriter` as a row.
216
+
217
+ Will convert list elements of the normalized data into a string with all elements separated by the `|` character, which can be changed using an optional `plural_separator` argument.
218
+
219
+ ### format_profile_as_csv_row
220
+
221
+ Function formatting the given normalized Bluesky profile as a list able to be written by a `csv.writer` as a row in the order of [PROFILE_FIELDS](#profile_fields) (which can therefore be used as header row of the CSV).
222
+
223
+ Will convert list elements of the normalized data into a string with all elements separated by the `|` character, which can be changed using an optional `plural_separator` argument.
224
+
225
+ ### format_post_as_csv_row
226
+
227
+ Function formatting the given normalized tBluesky post as a list able to be written by a `csv.writer` as a row in the order of [POST_FIELDS](#post_fields) (which can therefore be used as header row of the CSV).
228
+
229
+ Will convert list elements of the normalized data into a string with all elements separated by the `|` character, which can be changed using an optional `plural_separator` argument.
230
+
231
+ ### PROFILE_FIELDS
232
+
233
+ List of a Bluesky user profile's normalized field names. Useful to declare headers with csv writers.
234
+
235
+ ### POST_FIELDS
236
+
237
+ List of a Bluesky post's normalized field names. Useful to declare headers with csv writers.
238
+
239
+ ### normalize_user
240
+
241
+ Function taking a nested dict describing a user from Twitter's JSON payload and returning a flat "normalized" dict composed of all [USER_FIELDS](#user_fields) keys.
242
+
243
+ Will return datetimes as UTC but can take an optional second `locale` argument as a [`pytz`](https://pypi.org/project/pytz/) string timezone.
244
+
245
+ *Arguments*
246
+
247
+ * **data** *(dict)*: user profile data payload coming from Twitter API v1.1 or v2.
248
+ * **locale** *(pytz.timezone as str, optional)*: timezone used to convert dates. If not given, will default to UTC.
249
+ * **pure** *(bool, optional)*: whether to allow the function to mutate its original `data` argument. Defaults to `True`.
250
+
251
+ ### normalize_tweet
252
+
253
+ Function taking a nested dict describing a tweet from Twitter's JSON payload (API v1.1) and returning a flat "normalized" dict composed of all [TWEET_FIELDS](#tweet_fields) keys.
254
+
255
+ Will return datetimes as UTC but can take an optional last `locale` argument as a `pytz` string timezone.
256
+
257
+ When setting `extract_referenced_posts` to `True` it will instead return a list of dicts including the desired tweet as well as each referenced ones such as quoted or retweeted tweets.
258
+
259
+ *Arguments*
260
+
261
+ * **tweet** *(dict)*: tweet data payload coming from Twitter API v1.1.
262
+ * **locale** *(pytz.timezone as str, optional)*: timezone used to convert dates. If not given, will default to UTC.
263
+ * **extract_referenced_posts** *(bool, optional)*: whether to return in the output, in addition to the tweet to be normalized, also normalized data for each other referenced tweets found in the payload data (including retweeted and quoted tweets). If `False`, the function will return a `dict`, if `True` a `list` of `dict`. Defaults to `False`.
264
+ * **collection_source** *(string, optional)*: An optional information to add within the `collected_via` field of the normalized tweet to indicate whence it was collected.
265
+
266
+ ### normalize_tweets_payload_v2
267
+
268
+ Function taking an entire tweets JSON payload from Twitter API v2 and returning a list of all contained tweets formatted as flat "normalized" dicts composed of all [TWEET_FIELDS](#tweet_fields) keys.
269
+
270
+ Will return datetimes as UTC but can take an optional last `locale` argument as a `pytz` string timezone.
271
+
272
+ When setting `extract_referenced_posts` to `True` it will instead return a list of dicts including the desired tweets as well as each referenced ones such as quoted or retweeted tweets.
273
+
274
+ *Arguments*
275
+
276
+ * **payload** *(dict)*: tweets data payload coming from Twitter API v2.
277
+ * **locale** *(pytz.timezone, optional)*: timezone used to convert dates. If not given, will default to UTC.
278
+ * **extract_referenced_tweets** *(bool, optional)*: whether to return in the output, in addition to the tweet to be normalized, also normalized data for each other referenced tweets found in the payload data (including retweeted and quoted tweets).
279
+ * **collection_source** *(string, optional)*: An optional information to add within the `collected_via` field of the normalized tweet to indicate whence it was collected.
280
+
281
+ ```python
282
+ from twitwi import normalize_tweets_payload_v2
283
+
284
+ # Normalizing an entire tweets payload to extract a list of tweets
285
+ normalize_tweets_payload_v2(payload)
286
+
287
+ # Normalizing an entire tweets payload to extract a list of tweets
288
+ # as well as the referenced tweets (quoted, retweeted, etc.)
289
+ normalize_tweets_payload_v2(payload, extract_referenced_tweets=True)
290
+
291
+ # Converting found dates to a chosen timezone
292
+ from pytz import timezone
293
+ paris_tz = timezone('Europe/Paris')
294
+
295
+ normalize_tweets_payload_v2(payload, locale=paris_tz)
296
+ ```
297
+
298
+ ### transform_user_into_csv_dict
299
+
300
+ Function transforming (i.e. mutating, so beware) a given normalized Twitter user into a suitable dict able to be written by a `csv.DictWriter` as a row.
301
+ Will convert list elements of the normalized data into a string with all elements separated by the `|` character, which can be changed using an optional `plural_separator` argument.
302
+
303
+ ```python
304
+ from twitwi import transform_user_into_csv_dict
305
+
306
+ # The function returns nothing, `normalized_user` has been mutated
307
+ transform_user_into_csv_dict(normalized_user)
308
+ ```
309
+
310
+ ### transform_tweet_into_csv_dict
311
+
312
+ Function transforming (i.e. mutating, so beware) a given normalized tweet into a suitable dict able to be written by a `csv.DictWriter` as a row.
313
+
314
+ Will convert list elements of the normalized data into a string with all elements separated by the `|` character, which can be changed using an optional `plural_separator` argument.
315
+
316
+ ```python
317
+ from twitwi import transform_tweet_into_csv_dict
318
+
319
+ # The function returns nothing, `normalized_tweet` has been mutated
320
+ transform_tweet_into_csv_dict(normalized_tweet)
321
+ ```
322
+
323
+ ### format_user_as_csv_row
324
+
325
+ Function formatting the given normalized Twitter user as a list able to be written by a `csv.writer` as a row.
326
+
327
+ Will convert list elements of the normalized data into a string with all elements separated by the `|` character, which can be changed using an optional `plural_separator` argument.
328
+
329
+ ```python
330
+ from twitwi import format_user_as_csv_row
331
+
332
+ row = format_user_as_csv_row(normalized_user)
333
+ ```
334
+
335
+ ### format_tweet_as_csv_row
336
+
337
+ Function formatting the given normalized tweet as a list able to be written by a `csv.writer` as a row.
338
+
339
+ Will convert list elements of the normalized data into a string with all elements separated by the `|` character, which can be changed using an optional `plural_separator` argument.
340
+
341
+ ```python
342
+ from twitwi import format_tweet_as_csv_row
343
+
344
+ row = format_tweet_as_csv_row(normalized_tweet)
345
+ ```
346
+
347
+ ### apply_tcat_format
348
+
349
+ Function taking a normalized tweet and returning a new dict with keys adjusted to correspond to [DMI's TCAT](https://github.com/digitalmethodsinitiative/dmi-tcat) format.
350
+
351
+ ```python
352
+ from twitwi import apply_tcat_format
353
+
354
+ tweet_tcat = apply_tcat_format(normalized_tweet)
355
+ ```
356
+
357
+ ### USER_FIELDS
358
+
359
+ List of a Twitter user profile's field names. Useful to declare headers with csv writers:
360
+
361
+ ```python
362
+ from twitwi.constants import USER_FIELDS
363
+
364
+ # Using csv.writer
365
+ w = csv.writer(f)
366
+ w.writerow(USER_FIELDS)
367
+
368
+ # Using csv.DictWriter
369
+ w = csv.DictWriter(f, fieldnames=USER_FIELDS)
370
+ w.writeheader()
371
+ ```
372
+
373
+ ### TWEET_FIELDS
374
+
375
+ List of a tweet's field names. Useful to declare headers with csv writers:
376
+
377
+ ```python
378
+ from twitwi.constants import TWEET_FIELDS
379
+
380
+ # Using csv.writer
381
+ w = csv.writer(f)
382
+ w.writerow(TWEET_FIELDS)
383
+
384
+ # Using csv.DictWriter
385
+ w = csv.DictWriter(f, fieldnames=TWEET_FIELDS)
386
+ w.writeheader()
387
+ ```
388
+
389
+ ### anonymize_normalized_tweet
390
+
391
+ Function taking a normalized tweet and mutating it by editing the text and removing all metadata related to the tweet's author user.
392
+
393
+ Note that the tweet's ID as well as other users screennames mentioned in the tweet are kept and could require extra processing depending on the use cases.
394
+
395
+ ```python
396
+ from twitwi import anonymize_normalized_tweet
397
+
398
+ # The function returns nothing, `normalized_tweet` has been mutated
399
+ anonymize_normalized_tweet(normalized_tweet)
400
+ ```
401
+
402
+ ### get_timestamp_from_id
403
+
404
+ Function taking a tweet ID and producing from it the UTC UNIX timestamp of when the tweet was posted.
405
+
406
+ This relies on the Snowflake format used by Twitter to generate tweets IDs, which builds IDs on top of the actual timestamp when a tweet was submitted.
407
+
408
+ Will only work for tweets with an ID greater than 29700859247, which is the first ID from which the Snowflake algorithm was implemented.
409
+
410
+ ```python
411
+ from twitwi import get_timestamp_from_id
412
+
413
+ timestamp = get_timestamp_from_id(tweet_ID)
414
+ ```
415
+
416
+ ### get_dates_from_id
417
+
418
+ Function taking a tweet ID and producing from it the datetime when the tweet was posted.
419
+
420
+ This relies on the Snowflake format used by Twitter to generate tweets IDs, which builds IDs on top of the actual timestamp when a tweet was submitted.
421
+
422
+ Will only work for tweets with an ID greater than 29700859247, which is the first ID from which the Snowflake algorithm was implemented.
423
+
424
+ The function can also take an optional `locale` argument as a [`pytz`](https://pypi.org/project/pytz/) string timezone.
425
+
426
+ ```python
427
+ from twitwi import get_dates_from_id
428
+
429
+ date_time = get_dates_from_id(tweet_ID)
430
+
431
+ # Or converting to a chosen timezone
432
+ from pytz import timezone
433
+ paris_tz = timezone('Europe/Paris')
434
+
435
+ date_time = get_dates_from_id(tweet_ID, locale=paris_tz)
436
+ ```