Skip to content

Commit 16970c0

Browse files
committed
Add function to save tweets in json format
1 parent b9429b8 commit 16970c0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/you_get/extractors/twitter.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def twitter_download(url, output_dir='.', merge=True, info_only=False, **kwargs)
5252
api_url = 'https://api.twitter.com/2/timeline/conversation/%s.json?tweet_mode=extended' % item_id
5353
api_content = get_content(api_url, headers={'authorization': authorization, 'x-guest-token': guest_token})
5454
info = json.loads(api_content)
55+
twitter_write_json(info, screen_name, item_id)
5556
if item_id not in info['globalObjects']['tweets']:
5657
# something wrong here
5758
#log.wtf('[Failed] ' + info['timeline']['instructions'][0]['addEntries']['entries'][0]['content']['item']['content']['tombstone']['tombstoneInfo']['richText']['text'], exit_code=None)
@@ -103,6 +104,7 @@ def twitter_download(url, output_dir='.', merge=True, info_only=False, **kwargs)
103104
api_url = 'https://api.twitter.com/1.1/statuses/show/%s.json?tweet_mode=extended' % item_id
104105
api_content = get_content(api_url, headers={'authorization': authorization, 'x-guest-token': guest_token})
105106
info = json.loads(api_content)
107+
twitter_write_json(info, screen_name, item_id)
106108
media = info['extended_entities']['media']
107109

108110
for medium in media:
@@ -129,6 +131,30 @@ def twitter_download(url, output_dir='.', merge=True, info_only=False, **kwargs)
129131
download_urls(urls, title, ext, size, output_dir, merge=merge)
130132

131133

134+
def twitter_write_json(info, screen_name, item_id):
135+
# this function save tweets in human readable json format
136+
# # uncomment these lines if you need the original api returned json
137+
# info_string = json.dumps(info, indent=" ", ensure_ascii=False)
138+
# with open(screen_name+'_'+item_id+"_tweet.json", 'w') as fw:
139+
# fw.write(info_string)
140+
if 'globalObjects' in info.keys():
141+
tweets = info['globalObjects']['tweets']
142+
info_users = info["globalObjects"]['users']
143+
tweets_simplified = {}
144+
for key in tweets.keys():
145+
user_id_str = tweets[key]['user_id_str']
146+
tweets_simplified[key] = {}
147+
tweets_simplified[key]['created_at'] = tweets[key]['created_at']
148+
tweets_simplified[key]['user_id_str'] = tweets[key]['user_id_str']
149+
tweets_simplified[key]['full_text'] = tweets[key]['full_text']
150+
tweets_simplified[key]['name'] = info_users[user_id_str]['name']
151+
152+
tweet_string = json.dumps(
153+
tweets_simplified, indent=" ", ensure_ascii=False)
154+
with open(screen_name+'_'+item_id+".json", 'w') as fw:
155+
fw.write(tweet_string)
156+
157+
132158
site_info = "Twitter.com"
133159
download = twitter_download
134160
download_playlist = playlist_not_supported('twitter')

0 commit comments

Comments
 (0)