TTL for cache entries

This commit is contained in:
Dylan 2022-05-22 15:19:17 +01:00
parent f640ac69a1
commit 014f9d0e8d

View file

@ -10,7 +10,7 @@ import re
import os import os
import urllib.parse import urllib.parse
import urllib.request import urllib.request
from datetime import date from datetime import date,datetime, timedelta
app = Flask(__name__) app = Flask(__name__)
CORS(app) CORS(app)
@ -196,6 +196,9 @@ def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico',mimetype='image/vnd.microsoft.icon') 'favicon.ico',mimetype='image/vnd.microsoft.icon')
def getDefaultTTL():
return datetime.today().replace(microsecond=0) + timedelta(days=1)
def direct_video(video_link): # Just get a redirect to a MP4 link from any tweet link def direct_video(video_link): # Just get a redirect to a MP4 link from any tweet link
cached_vnf = getVnfFromLinkCache(video_link) cached_vnf = getVnfFromLinkCache(video_link)
if cached_vnf == None: if cached_vnf == None:
@ -241,7 +244,7 @@ def embed_video(video_link, image=0): # Return Embed from any tweet link
else: else:
return embed(video_link, cached_vnf, image) return embed(video_link, cached_vnf, image)
def tweetInfo(url, tweet="", desc="", thumb="", uploader="", screen_name="", pfp="", tweetType="", images="", hits=0, likes=0, rts=0, time="", qrt={}, nsfw=False): # Return a dict of video info with default values def tweetInfo(url, tweet="", desc="", thumb="", uploader="", screen_name="", pfp="", tweetType="", images="", hits=0, likes=0, rts=0, time="", qrt={}, nsfw=False,ttl=getDefaultTTL()): # Return a dict of video info with default values
vnf = { vnf = {
"tweet" : tweet, "tweet" : tweet,
"url" : url, "url" : url,
@ -257,7 +260,8 @@ def tweetInfo(url, tweet="", desc="", thumb="", uploader="", screen_name="", pfp
"rts" : rts, "rts" : rts,
"time" : time, "time" : time,
"qrt" : qrt, "qrt" : qrt,
"nsfw" : nsfw "nsfw" : nsfw,
"ttl" : ttl
} }
return vnf return vnf
@ -383,20 +387,25 @@ def getVnfFromLinkCache(video_link):
print(" ➤ [ X ] Link not in json cache") print(" ➤ [ X ] Link not in json cache")
return None return None
def serializeUnknown(obj):
if isinstance(obj, (datetime, date)):
return obj.isoformat()
raise TypeError ("Type %s not serializable" % type(obj))
def addVnfToLinkCache(video_link, vnf): def addVnfToLinkCache(video_link, vnf):
if link_cache_system == "db": try:
try: if link_cache_system == "db":
out = db.linkCache.insert_one(vnf) out = db.linkCache.insert_one(vnf)
print(" ➤ [ + ] Link added to DB cache ") print(" ➤ [ + ] Link added to DB cache ")
return True return True
except Exception: elif link_cache_system == "json":
print(" ➤ [ X ] Failed to add link to DB cache") link_cache[video_link] = vnf
return None with open("links.json", "w") as outfile:
elif link_cache_system == "json": json.dump(link_cache, outfile, indent=4, sort_keys=True, default=serializeUnknown)
link_cache[video_link] = vnf return None
with open("links.json", "w") as outfile: except Exception:
json.dump(link_cache, outfile, indent=4, sort_keys=True) print(" ➤ [ X ] Failed to add link to DB cache")
return None return None
def message(text): def message(text):
return render_template( return render_template(