Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python code to get text and link of the bookmarked tweets and save in markdown
import json
import glob
all_bookmarks = []
md_file = open("bookmarks.md", "w+") # saving in markdown file, if no file exists using '+' creates one
files = [file for file in glob.glob("JSONBookmarks/*")] # using glob to read all files from the folder
for file_name in files:
print(file_name)
with open(file_name) as bk:
data = json.load(bk) # reads json data
all_bookmarks.append(data)
#print(json.dumps(response, indent = 1)) # pretty prints JSON data
# Function to get profile pic if you need
def getAuthorProfilePic(id):
return response["users"][id]["profile_image_url_https"]
# Function to get username
def getUserName(id):
return response["users"][id]["screen_name"]
# Function to construct bookmarked tweet url
def constructUrl(tweet_id, username):
return "https://twitter.com/" + username + "/status/" + tweet_id
# Function to format the text to write in file
def formatText(text):
text = text.replace("\n-", " ")
text = text.replace("\n", " ")
text = text[:100] + "..."
return text
# Run a loop through all_bookmarks
for data in all_bookmarks:
response = data["globalObjects"]
for tweet_id in response["tweets"]:
tweet = response["tweets"][tweet_id]
text = tweet["full_text"]
text = formatText(text)
url = constructUrl(tweet_id, getUserName(tweet["user_id_str"]))
bookmarked_tweet = "\n- " + text + "\n" + "\t - " + url
md_file.write(bookmarked_tweet)
import json
import glob
import functools
all_bookmarks = []
md_file = open("bookmarks.md", "w+") # saving in markdown file, if no file exists using '+' creates one
files = [file for file in glob.glob("JSONBookmarks/*")] # using glob to read all files from the folder
for file_name in files:
print(file_name)
with open(file_name) as bk:
data = json.load(bk) # reads json data
all_bookmarks.append(data)
#print(json.dumps(response, indent = 1)) # pretty prints JSON data
# Function to construct bookmarked tweet url
def constructUrl(tweet_id, username):
return "https://twitter.com/" + username + "/status/" + tweet_id
# Function to format the text to write in file
def formatText(text):
text = text.replace("\n-", " ")
text = text.replace("\n", " ")
text = text[:100] + "..."
return text
# Method to get value of nested dictionary
def deep_get(dictionary, keys, default=None):
return functools.reduce(lambda d, key: d.get(key, default) if isinstance(d, dict) else default, keys.split("."), dictionary)
# Run a loop through all_bookmarks
for data in all_bookmarks:
instructions_list = deep_get(data, "data.bookmark_timeline.timeline.instructions")
tweet_entries_list = deep_get(instructions_list[0], "entries")
for tweet_entry in tweet_entries_list:
result = deep_get(tweet_entry, "content.itemContent.tweet_results.result")
username = deep_get(result, "core.user.legacy.screen_name")
text = deep_get(result, "legacy.full_text")
tweet_id = deep_get(result, "rest_id")
if tweet_id == None or username == None or text == None:
continue
text = formatText(text)
url = constructUrl(tweet_id, username)
bookmarked_tweet = "\n- " + text + "\n" + "\t - " + url
md_file.write(bookmarked_tweet)
@ShreyaPrasad1209

This comment has been minimized.

Copy link

@ShreyaPrasad1209 ShreyaPrasad1209 commented Jun 12, 2021

Hi, thank you for writing this script! A quick question - the response I got from Twitter is different from the one you showed. Here's the link for your reference - https://jsonformatter.org/json-editor/fd67cb.

Is there something I should have done differently while getting the response from Twitter? I used DevTools to get this response.

@divyajyotiuk

This comment has been minimized.

Copy link
Owner Author

@divyajyotiuk divyajyotiuk commented Jun 12, 2021

Seems like twitter has updated their api response! I'll put an updated version as soon as possible.
Edit: I have updated the code. Checkout version v2 of the code. Thank you for the update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment