twitwi 0.20.0__py3-none-any.whl → 0.21.1__py3-none-any.whl
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.
- test/bluesky/__init__.py +0 -0
- test/bluesky/formatters_test.py +101 -0
- test/bluesky/normalizers_test.py +130 -0
- twitwi/__init__.py +19 -2
- twitwi/anonymizers.py +3 -9
- twitwi/bluesky/__init__.py +16 -0
- twitwi/bluesky/constants.py +19 -0
- twitwi/bluesky/formatters.py +29 -0
- twitwi/bluesky/normalizers.py +686 -0
- twitwi/bluesky/types.py +135 -0
- twitwi/bluesky/utils.py +110 -0
- twitwi/constants.py +323 -349
- twitwi/exceptions.py +8 -1
- twitwi/formatters.py +35 -37
- twitwi/normalizers.py +403 -339
- twitwi/utils.py +46 -18
- twitwi-0.21.1.dist-info/METADATA +436 -0
- twitwi-0.21.1.dist-info/RECORD +22 -0
- {twitwi-0.20.0.dist-info → twitwi-0.21.1.dist-info}/WHEEL +1 -1
- {twitwi-0.20.0.dist-info → twitwi-0.21.1.dist-info}/top_level.txt +1 -0
- twitwi-0.20.0.dist-info/METADATA +0 -156
- twitwi-0.20.0.dist-info/RECORD +0 -13
- {twitwi-0.20.0.dist-info → twitwi-0.21.1.dist-info}/licenses/LICENSE.txt +0 -0
- {twitwi-0.20.0.dist-info → twitwi-0.21.1.dist-info}/zip-safe +0 -0
twitwi/utils.py
CHANGED
|
@@ -5,44 +5,65 @@
|
|
|
5
5
|
# Miscellaneous utility functions.
|
|
6
6
|
#
|
|
7
7
|
from pytz import timezone
|
|
8
|
+
from dateutil.parser import parse as parse_date
|
|
8
9
|
from ural import normalize_url, get_normalized_hostname
|
|
9
10
|
from functools import partial
|
|
10
11
|
from datetime import datetime
|
|
11
12
|
|
|
12
13
|
from twitwi.constants import (
|
|
13
|
-
|
|
14
|
+
SOURCE_DATETIME_FORMAT,
|
|
15
|
+
SOURCE_DATETIME_FORMAT_V2,
|
|
14
16
|
FORMATTED_TWEET_DATETIME_FORMAT,
|
|
15
|
-
|
|
17
|
+
FORMATTED_FULL_DATETIME_FORMAT,
|
|
16
18
|
CANONICAL_URL_KWARGS,
|
|
17
19
|
CANONICAL_HOSTNAME_KWARGS,
|
|
18
20
|
PRE_SNOWFLAKE_LAST_TWEET_ID,
|
|
19
21
|
OFFSET_TIMESTAMP,
|
|
20
22
|
)
|
|
21
23
|
|
|
22
|
-
UTC_TIMEZONE = timezone(
|
|
24
|
+
UTC_TIMEZONE = timezone("UTC")
|
|
23
25
|
|
|
24
|
-
custom_normalize_url = partial(
|
|
25
|
-
normalize_url,
|
|
26
|
-
**CANONICAL_URL_KWARGS
|
|
27
|
-
)
|
|
26
|
+
custom_normalize_url = partial(normalize_url, **CANONICAL_URL_KWARGS)
|
|
28
27
|
|
|
29
28
|
custom_get_normalized_hostname = partial(
|
|
30
|
-
get_normalized_hostname,
|
|
31
|
-
**CANONICAL_HOSTNAME_KWARGS
|
|
29
|
+
get_normalized_hostname, **CANONICAL_HOSTNAME_KWARGS
|
|
32
30
|
)
|
|
33
31
|
|
|
34
32
|
|
|
35
|
-
def
|
|
33
|
+
def get_collection_time():
|
|
34
|
+
return datetime.now().strftime(FORMATTED_FULL_DATETIME_FORMAT)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def get_dates(date_str, locale=None, source="v1"):
|
|
38
|
+
if source not in ["v1", "v2", "bluesky"]:
|
|
39
|
+
raise Exception("source should be one of v1, v2 or bluesky")
|
|
40
|
+
|
|
36
41
|
if locale is None:
|
|
37
42
|
locale = UTC_TIMEZONE
|
|
38
43
|
|
|
39
|
-
|
|
40
|
-
|
|
44
|
+
try:
|
|
45
|
+
parsed_datetime = datetime.strptime(
|
|
46
|
+
date_str,
|
|
47
|
+
SOURCE_DATETIME_FORMAT if source == "v1" else SOURCE_DATETIME_FORMAT_V2,
|
|
48
|
+
)
|
|
49
|
+
except ValueError as e:
|
|
50
|
+
if source != "bluesky":
|
|
51
|
+
raise e
|
|
52
|
+
parsed_datetime = parse_date(date_str)
|
|
53
|
+
|
|
54
|
+
utc_datetime = parsed_datetime
|
|
55
|
+
if not parsed_datetime.tzinfo:
|
|
56
|
+
utc_datetime = UTC_TIMEZONE.localize(parsed_datetime)
|
|
41
57
|
locale_datetime = utc_datetime.astimezone(locale)
|
|
42
58
|
|
|
43
59
|
return (
|
|
44
60
|
int(utc_datetime.timestamp()),
|
|
45
|
-
datetime.strftime(
|
|
61
|
+
datetime.strftime(
|
|
62
|
+
locale_datetime,
|
|
63
|
+
FORMATTED_FULL_DATETIME_FORMAT
|
|
64
|
+
if source == "bluesky"
|
|
65
|
+
else FORMATTED_TWEET_DATETIME_FORMAT,
|
|
66
|
+
),
|
|
46
67
|
)
|
|
47
68
|
|
|
48
69
|
|
|
@@ -50,17 +71,21 @@ def validate_payload_v2(payload):
|
|
|
50
71
|
if not isinstance(payload, dict):
|
|
51
72
|
return False
|
|
52
73
|
|
|
53
|
-
if
|
|
54
|
-
if
|
|
74
|
+
if "data" not in payload:
|
|
75
|
+
if (
|
|
76
|
+
"meta" in payload
|
|
77
|
+
and "result_count" in payload["meta"]
|
|
78
|
+
and payload["meta"]["result_count"] == 0
|
|
79
|
+
):
|
|
55
80
|
return True
|
|
56
81
|
else:
|
|
57
82
|
return False
|
|
58
83
|
|
|
59
|
-
if not isinstance(payload[
|
|
84
|
+
if not isinstance(payload["data"], list):
|
|
60
85
|
return False
|
|
61
86
|
|
|
62
87
|
# NOTE: not sure it cannot be absent altogether
|
|
63
|
-
if
|
|
88
|
+
if "includes" not in payload or not isinstance(payload["includes"], dict):
|
|
64
89
|
return False
|
|
65
90
|
|
|
66
91
|
return True
|
|
@@ -84,4 +109,7 @@ def get_dates_from_id(tweet_id, locale=None):
|
|
|
84
109
|
|
|
85
110
|
locale_datetime = datetime.fromtimestamp(timestamp, locale)
|
|
86
111
|
|
|
87
|
-
return (
|
|
112
|
+
return (
|
|
113
|
+
timestamp,
|
|
114
|
+
datetime.strftime(locale_datetime, FORMATTED_TWEET_DATETIME_FORMAT),
|
|
115
|
+
)
|
|
@@ -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
|
+
[](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
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
test/bluesky/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
test/bluesky/formatters_test.py,sha256=PUEFGdp6fUpNhiWaoEC1-CXfX9L3jPDYcAjoOZ_DiXQ,3296
|
|
3
|
+
test/bluesky/normalizers_test.py,sha256=hYIAM53RYHxxw1YsJ9kJMn8TljnttpczqRpYHyZezAo,4281
|
|
4
|
+
twitwi/__init__.py,sha256=y0bAx9gE3THtlWE1YpXDIhGwqJ5_I8DCStWyyiiXJkw,1095
|
|
5
|
+
twitwi/anonymizers.py,sha256=nkl6HL1BWLz00wJ060XSbqjN5JF8pvcpEPnRXt70TUY,1588
|
|
6
|
+
twitwi/constants.py,sha256=fvqCngJIGyz5CpdVWbcAfjmE3_kvcx9giN0rEljL7OU,16001
|
|
7
|
+
twitwi/exceptions.py,sha256=OCIDagu2ErDyOGWunRBCK3O62TnzFpIMQ9gS8l9EALQ,696
|
|
8
|
+
twitwi/formatters.py,sha256=yn14AsrGAUw8rShOnYJvoMbzdWpfTeSs0P0ZPNTwhLU,3142
|
|
9
|
+
twitwi/normalizers.py,sha256=CWUK-XwhcEjLDjWH_qb6E03WZKsbIcwiRAVUjwXKQho,28438
|
|
10
|
+
twitwi/utils.py,sha256=8BiUrkzeydTh4a-rcFW0IHhSNdxlvCXaEpHZGVROl3A,3090
|
|
11
|
+
twitwi/bluesky/__init__.py,sha256=7ITVm1bWE9p0HCWcUs2iOxwKQXK8NEzrvp9hXaicrp0,445
|
|
12
|
+
twitwi/bluesky/constants.py,sha256=CSxNZGYlZvlZ0IMYpP8tVTO6AnzoTmGDFh185hs8n88,473
|
|
13
|
+
twitwi/bluesky/formatters.py,sha256=r8MKOpU8z024z9DQ8-tlKjzaB92T1Su2IjcFV5hjfXg,731
|
|
14
|
+
twitwi/bluesky/normalizers.py,sha256=LpxWgwrIG1TsjL-6nEG8U_FsMKh_BQHuXRrAJdu9lEA,26318
|
|
15
|
+
twitwi/bluesky/types.py,sha256=TaOnAbLPKJqWumLybKmDDh46FLb9WXzqOPyDQU8RHrI,12288
|
|
16
|
+
twitwi/bluesky/utils.py,sha256=KQd0YVWZHcCRcqnX5A1aeKdavwocN6Bsi8pCnHqRgXc,3486
|
|
17
|
+
twitwi-0.21.1.dist-info/licenses/LICENSE.txt,sha256=Ddg_PcGnl0qd2167o2dheCjE_rCZJOoBxjJnJhhOpX4,1099
|
|
18
|
+
twitwi-0.21.1.dist-info/METADATA,sha256=ruJ7MYnoR-xAoMFKf8RWYSri-jqvf8ulW-2fquLjcys,18017
|
|
19
|
+
twitwi-0.21.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
20
|
+
twitwi-0.21.1.dist-info/top_level.txt,sha256=TaKyGU7j_EVbP5KI0UD6qjbaKv2Qn0OrkfUQ29a04kg,12
|
|
21
|
+
twitwi-0.21.1.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
22
|
+
twitwi-0.21.1.dist-info/RECORD,,
|