Code Monkey home page Code Monkey logo

twitter-likes-exporter's Introduction

Twitter Likes Exporter

Hacky Python scripts for downloading your Twitter likes and converting to HTML. Includes support for downloading user avatars and image media in tweets. Scrapes tweets using the GraphQL API powering Twitter.com - the equivalent of you scrolling through all your likes in your web browser, only saved locally forever!

example output rendered html of tweets

Note only currently supports grabbing the liked tweet. So if it was a quote tweet, does not download the RT'd tweet. If it's a reply or part of a thread, does not download the other tweets.

Meant to jumpstart you to at least getting your tweets offline from Twitter, if not building something much better!

Setup

Intended for use with Python 3.7. First install requirements:

pip install -r requirements.txt

(It's just requests). Next you'll need to populate the config.json file with credentials needed to match how your web browser gets your likes from Twitter.com:

  1. Open up your web browser and ensure the Network web debugging tab is open so you can inspect network requests
  2. Navigate to https://twitter.com/<your_user_handle>/likes
  3. Look for a network request to an api.twitter.com domain path ending in /Likes
  4. From the request headers: a. Copy the Authorization value and save as HEADER_AUTHORIZATION in config.json b. Copy the Cookies value and save as HEADER_COOKIES in config.json c. Copy the x-csrf-token value and save as HEADER_CSRF in config.json
  5. Find your Twitter user ID (available in the /Likes request params, or elsewhere) and save as USER_ID in config.json

Download Likes to JSON

By default, liked tweets will be saved to a file liked_tweets.json in this repo's folder path. If you'd like to override this, set new path as OUTPUT_JSON_FILE_PATH in config.json.

Run as follows:

python download_tweets.py

Should provide output like the following:

Starting retrieval of likes for Twitter user 1234...
Fetching likes page: 1...
Fetching likes page: 2...
Fetching likes page: 3...
Done. Likes JSON saved to: liked_tweets.json

The output JSON will be a list of dictionaries like the following:

[
   {
      "tweet_id": "780770946428829696",
      "user_id": "265447323",
      "user_handle": "LeahTiscione",
      "user_name": "Leah Tiscione",
      "user_avatar_url": "https://pbs.twimg.com/profile_images/1563330281838284805/aUtIY2vj_normal.jpg",
      "tweet_content":"What are you hiding in your locked instagram? sandwiches? Sunsets???? let us see your nephew!!!!",
      "tweet_media_urls": [],
      "tweet_created_at": "Sun Mar 13 15:16:45 +0000 2011"
   }
]

Convert JSON Likes to HTML

If you want your tweets as a local HTML file, you can run the second script to convert the output JSON file from the above step.

NOTE: This will attempt to download all media images and tweet author avatars locally by default to avoid relying on Twitter hosting. You can override this by changing the DOWNLOAD_IMAGES boolean in config.json to false.

  1. Be sure the OUTPUT_JSON_FILE_PATH value in config.json is pointing to the output JSON file of your tweets.
  2. Run:
python parse_tweets_json_to_html.py

This will download all images (if specified; saved to tweet_likes_html/images) and construct an HTML file at tweet_likes_html/index.html containing all liked tweets, as well as individual HTML files within tweet_likes_html/tweets/.

twitter-likes-exporter's People

Contributors

mlschmitt avatar

Stargazers

greefea avatar Robert Wall avatar Adriaan Dens avatar midi avatar Paolo Cozzi avatar Schnirivadze avatar  avatar yibie avatar Prince avatar  avatar Roblox Thot avatar  avatar Efe Tunca avatar JuulFeen avatar  avatar JMaxU avatar Eric_Ren avatar  avatar cσgscídєs avatar  avatar Jin Yijie avatar Serban Mogos avatar Hari Krishna Dara avatar  avatar Tyler Davis Mitchell avatar Gaberber avatar Jaki Rose avatar Stephen avatar Jonas Frede avatar  avatar  avatar Manuel avatar Pete avatar  avatar INVALID-SYNTAX avatar  avatar Antoine Dujardin avatar NULL avatar  avatar  avatar Dersu Jo avatar Ravi M avatar  avatar  avatar

Watchers

 avatar

twitter-likes-exporter's Issues

error in download_tweets.py

Error:

Starting retrieval of likes for Twitter user 1371351228509196295...
Fetching likes page: 1...
Fetching likes page: 2...
Traceback (most recent call last):
  File "./twitter-likes-exporter-main/download_tweets.py", line 117, in <module>
    downloader.retrieve_all_likes()
  File "./twitter-likes-exporter-main/download_tweets.py", line 30, in retrieve_all_likes
    tweet_parser = TweetParser(raw_tweet)
                   ^^^^^^^^^^^^^^^^^^^^^^
  File "./twitter-likes-exporter-main/tweet_parser.py", line 11, in __init__
    self.key_data = raw_tweet_json["content"]["itemContent"]["tweet_results"]["result"]
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
KeyError: 'result'

config.json is filled with needed data

KeyError on 'result'

Heya, wondering if Twitter/X has cut this off from working.

Run the script (on two different linux boxes) and both fail at the same result. This differs from #2 as I am using the correct numeric userID:

$ python3 download_tweets.py
Starting retrieval of likes for Twitter user ...
Fetching likes page: 1...
...
Fetching likes page: 35...
Traceback (most recent call last):
File "/home/ollie/twitter-likes-exporter-main/download_tweets.py", line 117, in
downloader.retrieve_all_likes()
File "/home/ollie/twitter-likes-exporter-main/download_tweets.py", line 30, in retrieve_all_likes
tweet_parser = TweetParser(raw_tweet)
File "/home/ollie/twitter-likes-exporter-main/tweet_parser.py", line 11, in __init__
self.key_data = raw_tweet_json["content"]["itemContent"]["tweet_results"]["result"]
KeyError: 'result'

This is using fresh cookie/auth data for both. Any pointers? Thanks in advance

Getting a KeyError on data

Firstly, thanks for writing this script!

Has something changed since you made this script? I'm getting the following error:

$ python3 download_tweets.py
Starting retrieval of likes for Twitter user ■■■■■■...
Traceback (most recent call last):
File "/home/■■■/Repositories/twitter-likes-exporter/download_tweets.py", line 117, in
downloader.retrieve_all_likes()
File "/home/■■■/Repositories/twitter-likes-exporter/download_tweets.py", line 21, in retrieve_all_likes
likes_page = self.retrieve_likes_page()
File "/home/■■■/Repositories/twitter-likes-exporter/download_tweets.py", line 49, in retrieve_likes_page
return self.extract_likes_entries(response.json())
File "/home/■■■/Repositories/twitter-likes-exporter/download_tweets.py", line 52, in extract_likes_entries
return raw_data['data']['user']['result']['timeline_v2']['timeline']['instructions'][0]['entries']
KeyError: 'data'

I have also tried to unprivate my twitter account to see if that works, but it does not.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.