Skip to content

Instantly share code, notes, and snippets.

@iandanforth
Last active August 30, 2024 11:58
Show Gist options
  • Save iandanforth/f72e750db0c022d76a93eb922f5e0f3c to your computer and use it in GitHub Desktop.
Save iandanforth/f72e750db0c022d76a93eb922f5e0f3c to your computer and use it in GitHub Desktop.

Revisions

  1. iandanforth revised this gist Nov 18, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion expander.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # This python finds all strings matching https://t.co/ links in a text
    # This script replaces all strings matching https://t.co/ links in a the manifest with their expanded versions
    import json
    import re
    import urlexpander
  2. iandanforth created this gist Nov 18, 2022.
    28 changes: 28 additions & 0 deletions expander.py
    Original 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)