| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import json |
| 10 |
import re |
| 11 |
import sys |
| 12 |
import requests |
| 13 |
from datetime import datetime |
| 14 |
|
| 15 |
twitter_url = "https://twitter.com/" + sys.argv[1] |
| 16 |
twitter_username = twitter_url[twitter_url.rfind("/") + 1:] |
| 17 |
twitter_bearer = "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA" |
| 18 |
|
| 19 |
|
| 20 |
url_response = url_response = requests.get(twitter_url) |
| 21 |
twitter_html = url_response.text |
| 22 |
twitter_token = re.search("gt=(\d+);", twitter_html).group(1) |
| 23 |
|
| 24 |
url_response = requests.get("https://twitter.com/i/api/graphql/hc-pka9A7gyS3xODIafnrQ/UserByScreenName?variables=%7B%22screen_name%22%3A%22sportscenter%22%2C%22withHighlightedLabel%22%3Atrue%7D", headers = { |
| 25 |
"x-guest-token": twitter_token, |
| 26 |
"Authorization": twitter_bearer |
| 27 |
}) |
| 28 |
twitter_id = json.loads(url_response.text)["data"]["user"]["rest_id"] |
| 29 |
|
| 30 |
|
| 31 |
twitter_json_url = "https://twitter.com/i/api/2/timeline/profile/{user_id}.json?include_profile_interstitial_type=1&include_blocking=1&include_blocked_by=1&include_followed_by=1&include_want_retweets=1&include_mute_edge=1&include_can_dm=1&include_can_media_tag=1&skip_status=1&cards_platform=Web-12&include_cards=1&include_ext_alt_text=true&include_quote_count=true&include_reply_count=1&tweet_mode=extended&include_entities=true&include_user_entities=true&include_ext_media_color=true&include_ext_media_availability=true&send_error_codes=true&simple_quoted_tweet=true&include_tweet_replies=false&count=50&userId={user_id}&ext=mediaStats&2ChighlightedLabel".format(user_id = twitter_id) |
| 32 |
|
| 33 |
url_response = requests.get(twitter_json_url, headers = { |
| 34 |
"x-guest-token": twitter_token, |
| 35 |
"Authorization": twitter_bearer |
| 36 |
}) |
| 37 |
|
| 38 |
|
| 39 |
json_data = json.loads(url_response.text) |
| 40 |
json_root = json_data["globalObjects"]["tweets"] |
| 41 |
json_feed = "{{\"title\": \"{title}\", \"home_page_url\": \"{url}\", \"items\": [{items}]}}" |
| 42 |
items = list() |
| 43 |
|
| 44 |
for ite in json_root: |
| 45 |
article = json_root[ite] |
| 46 |
|
| 47 |
if "urls" in article["entities"] and len(article["entities"]["urls"]) > 0: |
| 48 |
article_url = json.dumps(article["entities"]["urls"][0]["expanded_url"]) |
| 49 |
else: |
| 50 |
article_url = json.dumps("") |
| 51 |
|
| 52 |
article_title = json.dumps(article["full_text"][:75] + (article["full_text"][75:] and '...')) |
| 53 |
article_time = json.dumps(datetime.strptime(article["created_at"], "%a %b %d %H:%M:%S %z %Y").isoformat()) |
| 54 |
article_contents = json.dumps(article["full_text"]) |
| 55 |
|
| 56 |
items.append("{{\"title\": {title}, \"authors\": [{{\"name\": {author}}}], \"content_text\": {text}, \"url\": {url}, \"date_published\": {date}}}".format( |
| 57 |
title = article_title, |
| 58 |
text = article_contents, |
| 59 |
author = json.dumps(twitter_username), |
| 60 |
url = article_url, |
| 61 |
date = article_time)) |
| 62 |
|
| 63 |
json_feed = json_feed.format( |
| 64 |
title = "twitter.com/" + twitter_username, |
| 65 |
url = twitter_url, |
| 66 |
items = ", ".join(items)) |
| 67 |
print(json_feed) |