Last active
April 1, 2024 20:57
-
-
Save catdance124/3e0313ed070a8cddf1ac713c10e7b6b7 to your computer and use it in GitHub Desktop.
Script to download all videos of a Twitter user.
This file contains 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
""" | |
Script to download all videos of a Twitter user. | |
Requirements: | |
- Twitter API key | |
- tweepy: https://github.com/tweepy/tweepy | |
> pip install tweepy | |
- youtube-dl: https://github.com/ytdl-org/youtube-dl/ | |
> pip install youtube-dl | |
Usage: | |
> python twi_vid_dl.py | |
Enter account name:{any Twitter ID} | |
""" | |
import os | |
import subprocess | |
import tweepy | |
import re | |
from config import CONFIG # ./config.py | |
CONSUMER_KEY = CONFIG["CONSUMER_KEY"] | |
CONSUMER_SECRET = CONFIG["CONSUMER_SECRET"] | |
ACCESS_TOKEN = CONFIG["ACCESS_TOKEN"] | |
ACCESS_SECRET = CONFIG["ACCESS_SECRET"] | |
def main(): | |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) | |
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET) | |
api = tweepy.API(auth) | |
key_account = input("Enter account name:") | |
media_dir = f'./{key_account}' | |
if not os.path.exists(media_dir): | |
os.makedirs(media_dir) | |
search_results = tweepy.Cursor(api.user_timeline, screen_name=key_account).items() | |
for tweet in search_results: | |
if tweet.text[:2] != 'RT': # exclusion RT | |
if hasattr(tweet, 'extended_entities'): # exclusion no media | |
text = tweet.text.replace('\n', ' ') | |
text = re.sub(r'#.*', '', text) | |
text = re.sub(r'http.*', '', text) | |
tweetURL = f'https://twitter.com/{tweet.user.screen_name}/status/{tweet.id}' | |
filename = f'{media_dir}/{tweet.user.screen_name}_{tweet.created_at.strftime("%Y%m%d")}_{text}.mp4' | |
if not os.path.exists(filename): | |
print('Downloading...') | |
print(tweetURL) | |
subprocess.run(['youtube-dl', '-o', filename, '--retries', '3', tweetURL]) | |
print('Completed!\n') | |
else: | |
print('pass coz already exist: ', filename) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment