Last active
August 30, 2024 11:58
-
-
Save iandanforth/f72e750db0c022d76a93eb922f5e0f3c to your computer and use it in GitHub Desktop.
Revisions
-
iandanforth revised this gist
Nov 18, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # This script replaces all strings matching https://t.co/ links in a the manifest with their expanded versions import json import re import urlexpander -
iandanforth created this gist
Nov 18, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ # This python finds all strings matching https://t.co/ links in a text import json import re import urlexpander # Open the file containing twitter bookmarks in json # and convert to a dictionary with open('bookmark-manifest.json', 'r') as fh: contents = fh.read() bookmarks = json.loads(contents) # Go through each bookmark and collect all the short url links for mark in bookmarks: text = mark['text'] links = re.findall(r'https://t.co/\w+', text) if links: print(links) # Use urlexpander.expand() to expand any found links # then replace them in `text` for link in links: expanded = urlexpander.expand(link) text = text.replace(link, expanded) # Save the expanded text back to the bookmark mark['text'] = text # Write the updated bookmarks back to the file with open('bookmark-manifest-expanded.json', 'w') as fh: json.dump(bookmarks, fh, indent=4)