Created
May 24, 2023 01:13
-
-
Save roldan/79f2fee836a9524c00bd5164578dd12f to your computer and use it in GitHub Desktop.
Download twitter threads from an account to csv file
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 characters
import tweepy | |
import csv | |
import time | |
consumer_key = 'xxxxxx' | |
consumer_secret = 'xxxxxx' | |
access_token = 'xxxx-xxxxxxxxxxx' | |
access_token_secret = 'xxxxxxxxx' | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
api = tweepy.API(auth) | |
username = 'Username' | |
with open('mis_hilos.csv', 'w', newline='') as file: | |
writer = csv.writer(file) | |
writer.writerow(['Fecha', 'Url', 'Tweet']) | |
thread_ids = [] | |
for tweet in tweepy.Cursor(api.user_timeline, screen_name=username, tweet_mode='extended').items(): | |
time.sleep(0.5) | |
if tweet.in_reply_to_status_id is not None and str(tweet.in_reply_to_status_id) not in thread_ids: | |
try: | |
prev_tweet_id = str(tweet.in_reply_to_status_id) | |
prev_tweet = api.get_status(prev_tweet_id, tweet_mode='extended') | |
if prev_tweet.author.screen_name != username: | |
continue | |
thread_ids.append(tweet.id_str) | |
thread_text = [] | |
thread_text.append((tweet.created_at, "https://twitter.com/{}/status/{}".format(username, tweet.id_str), tweet.full_text)) | |
while prev_tweet_id is not None and prev_tweet_id not in thread_ids: | |
prev_tweet = api.get_status(prev_tweet_id, tweet_mode='extended') | |
thread_text.append((prev_tweet.created_at, "https://twitter.com/{}/status/{}".format(username, prev_tweet.id_str), prev_tweet.full_text)) | |
thread_ids.append(prev_tweet.id_str) | |
prev_tweet_id = prev_tweet.in_reply_to_status_id | |
time.sleep(0.5) | |
thread_text.reverse() | |
writer.writerows(thread_text) | |
print("{} {} downloaded!".format(thread_text[0][0], thread_text[0][1])) | |
except Exception as ex: | |
print(ex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment