using youtube-dl as a class

This commit is contained in:
jeancf 2022-11-03 22:10:23 +01:00
parent 4328c5b3d4
commit 4d1fec306f

View File

@ -156,27 +156,24 @@ def process_attachments(nitter_url, attachments_container, get_vids, twit_accoun
vid_class = attachments_container.find('div', class_='video-container') vid_class = attachments_container.find('div', class_='video-container')
if vid_class is not None: if vid_class is not None:
if get_vids: if get_vids:
# Download video from twitter and store in filesystem. Running as subprocess to avoid import youtube_dl
# requirement to install ffmpeg and ffmpeg-python for those that do not want to post videos
try:
video_file = os.path.join('https://twitter.com', author_account, 'status', status_id) video_file = os.path.join('https://twitter.com', author_account, 'status', status_id)
# Set output location to ./output/twit_account/status_id ydl_opts = {
dl_feedback = subprocess.run( 'outtmpl': "output/" + twit_account + "/" + status_id + "/%(id)s.%(ext)s",
["youtube-dl", video_file, "-ooutput/" + twit_account + "/" + status_id + "/%(id)s.%(ext)s", "-f best[width<=500]"], 'format': "best[width<=500]",
timeout=300 'socket_timeout': 60,
) }
if dl_feedback.returncode != 0:
logging.warning('Video in tweet ' + status_id + ' from ' + twit_account + ' failed to download') with youtube_dl.YoutubeDL(ydl_opts) as ydl:
try:
ydl.download([video_file])
except Exception as e:
logging.warn('Error downloading twitter video: ' + str(e))
vid_in_tweet = True vid_in_tweet = True
else: else:
logging.debug('downloaded twitter video from attachments') logging.debug('downloaded twitter video from attachments')
except OSError:
logging.fatal("Could not execute twitterdl.py (is it there? Is it set as executable?)")
sys.exit(-1)
else:
vid_in_tweet = True
return pics, vid_in_tweet return pics, vid_in_tweet
@ -193,6 +190,7 @@ def contains_class(body_classes, some_class):
return found return found
def is_time_valid(timestamp, max_age, min_delay): def is_time_valid(timestamp, max_age, min_delay):
ret = True ret = True
# Check that the tweet is not too young (might be deleted) or too old # Check that the tweet is not too young (might be deleted) or too old
@ -279,10 +277,10 @@ def main(argv):
cap = int(args['c']) cap = int(args['c'])
# Remove previous log file # Remove previous log file
#try: try:
# os.remove(twit_account + '.log') os.remove(twit_account + '.log')
#except FileNotFoundError: except FileNotFoundError:
# pass pass
# Setup logging to file # Setup logging to file
logging.basicConfig( logging.basicConfig(