~boringcactus/youtube-dl-twitter-thread

84623e3a6646deec7ca5e3494464762f8c18f0da — Melody Horn 1 year, 8 months ago main
write script
1 files changed, 46 insertions(+), 0 deletions(-)

A youtube-dl-twitter-thread.py
A  => youtube-dl-twitter-thread.py +46 -0
@@ 1,46 @@
import os
import subprocess
import urllib.request
import xml.etree.ElementTree as ET

def get_thread_after(url):
    yield url
    alt_url = url.replace('twitter.com', 'nitter.net')
    with urllib.request.urlopen(alt_url) as alt_f:
        doc = ET.parse(alt_f)
        follow_ups = doc.findall(".//div[@class='main-thread']/div[@class='after-tweet thread-line']//a[@class='tweet-link']")
        next_tweets = ['https://twitter.com' + x.get('href').replace('#m', '') for x in follow_ups]
        if len(next_tweets) > 0:
            yield from next_tweets
            yield from get_thread_after(next_tweets[-1])

def download_thread_from(url):
    for url in get_thread_after(url):
        print(' --- Downloading', url)
        subprocess.run(['youtube-dl', url])

print('* youtube-dl a twitter thread *')
print('a highly janky script by @boring_cactus')
print()
print('files will wind up in', os.getcwd())
print('it might get stuck if twitter starts being a dick, but if you try again later and start where it got stuck earlier, then you should be alright.')

youtube_dl_check = subprocess.run(['youtube-dl', '--version'], capture_output=True)
if youtube_dl_check.returncode != 0:
    print()
    print("it looks like you don't have youtube-dl.")
    if input("mind if i grab it for you [yes/no]? ") == 'yes':
        with urllib.request.urlopen('https://youtube-dl.org/downloads/latest/youtube-dl.exe') as f_in:
            with open('youtube-dl.exe', 'wb') as f_out:
                f_out.write(f_in.read())
        print('should be good, thanks!')
    else:
        print('in that case, uhhh. good luck i guess')

print()

thread_url = input('start thread URL to download: ')

download_thread_from(thread_url)

input('All done! Press Enter to quit.')