forked from mlschmitt/twitter-likes-exporter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tweet_parser.py
66 lines (54 loc) · 1.95 KB
/
tweet_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class TweetParser():
def __init__(self, raw_tweet_json):
self.is_valid_tweet = True
self.raw_tweet_json = raw_tweet_json
self._media_urls = None
if not raw_tweet_json["content"].get("itemContent", None):
self.is_valid_tweet = False
return
self.key_data = raw_tweet_json["content"]["itemContent"]["tweet_results"]["result"]
if not self.key_data.get("legacy", None):
self.is_valid_tweet = False
def tweet_as_json(self):
return {
"tweet_id": self.tweet_id,
"user_id": self.user_id,
"user_handle": self.user_handle,
"user_name": self.user_name,
"user_avatar_url": self.user_avatar_url,
"tweet_content": self.tweet_content,
"tweet_media_urls": self.media_urls,
"tweet_created_at": self.tweet_created_at
}
@property
def tweet_id(self):
return self.key_data["legacy"]["id_str"]
@property
def tweet_content(self):
return self.key_data["legacy"]["full_text"]
@property
def tweet_created_at(self):
return self.key_data["legacy"]["created_at"]
@property
def user_id(self):
return self.key_data["legacy"]["user_id_str"]
@property
def user_handle(self):
return self.user_data["screen_name"]
@property
def user_name(self):
return self.user_data["name"]
@property
def user_avatar_url(self):
return self.user_data["profile_image_url_https"]
@property
def user_data(self):
return self.key_data["core"]["user_results"]["result"]["legacy"]
@property
def media_urls(self):
if self._media_urls is None:
self._media_urls = []
media_entries = self.key_data["legacy"]["entities"].get("media", [])
for entry in media_entries:
self._media_urls.append(entry["media_url_https"])
return self._media_urls