From 5ac4879f021043ac3c8490dfe657d8c504c004d8 Mon Sep 17 00:00:00 2001 From: Conner Bondurant Date: Sat, 3 Jul 2021 21:41:47 -0400 Subject: [PATCH] Improve handling and allow for exclusion of hostname This allows for the removal of "https://twitter.com/" This could be more convinent as you simply replace "twitter.com" with twtfix.me in the url, removing having to type http:// each time also added additional error handling just to avoid messy 500 errors. --- twitfix.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/twitfix.py b/twitfix.py index bfcb688..c3c594d 100644 --- a/twitfix.py +++ b/twitfix.py @@ -1,20 +1,30 @@ from flask import Flask, render_template, url_for, request, redirect from datetime import datetime import youtube_dl -import requests -import psutil import os +import re ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'}) app = Flask(__name__) +pathregex = re.compile("\\w{1,15}\\/status\\/\\d{19}") @app.route('/') def twitfix(subpath): - if subpath.startswith('https://twitter.com'): - with ydl: - result = ydl.extract_info(subpath, download=False) - return render_template('index.html', vidurl=result['url'], tweet=result['description'], pic=result['thumbnail'], user=result['uploader'], tweeturl=subpath) + match = pathregex.search(subpath) + if match is not None: + twitter_url = subpath + + if match.start() == 0: + twitter_url = "https://twitter.com/" + subpath + + with ydl: + try: + result = ydl.extract_info(twitter_url, download=False) + except Exception: # Just to keep from 500s that are messy + return "Bad twitter link, try again" + + return render_template('index.html', vidurl=result['url'], tweet=result['description'], pic=result['thumbnail'], user=result['uploader'], tweeturl=twitter_url) else: return "Please use a twitter link"