From 334e8f8ff1043ab8501c36345b5c06051371a4ba Mon Sep 17 00:00:00 2001 From: Robin Universe Date: Sun, 4 Jul 2021 04:40:22 -0500 Subject: [PATCH] Added a link cache system to increase speed and cut way down on time spent downloading / hitting the twitter api call limit --- links.json | 3 +++ twitfix.py | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 links.json diff --git a/links.json b/links.json new file mode 100644 index 0000000..7137478 --- /dev/null +++ b/links.json @@ -0,0 +1,3 @@ +{ + "test": "test" +} diff --git a/twitfix.py b/twitfix.py index 30ec535..5ec71ad 100644 --- a/twitfix.py +++ b/twitfix.py @@ -1,11 +1,17 @@ from flask import Flask, render_template import youtube_dl +import json import re ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'}) app = Flask(__name__) pathregex = re.compile("\\w{1,15}\\/status\\/\\d{19}") +link_cache = {} +f = open('links.json',) +link_cache = json.load(f) +f.close() + @app.route('/') def twitfix(subpath): @@ -16,14 +22,32 @@ def twitfix(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" + if twitter_url in link_cache: + print("Link located in cache") + return render_template('index.html', vidurl=link_cache[twitter_url]['url'], tweet=link_cache[twitter_url]['description'], pic=link_cache[twitter_url]['thumbnail'], user=link_cache[twitter_url]['uploader'], tweeturl=twitter_url) + else: + with ydl: + try: + print("Link not in cache, downloading and adding details to cache file") + result = ydl.extract_info(twitter_url, download=False) + + link_cache[twitter_url] = { + "url" :result['url'], + "description" :result['description'], + "thumbnail" :result['thumbnail'], + "uploader" :result['uploader'] + } - return render_template('index.html', vidurl=result['url'], tweet=result['description'], pic=result['thumbnail'], user=result['uploader'], tweeturl=twitter_url) + with open("links.json", "w") as outfile: + json.dump(link_cache, outfile, indent=4, sort_keys=True) + + except Exception: # Just to keep from 500s that are messy + print(Exception) + 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: + print("Link invalid") return "Please use a twitter link" @app.route('/info/')