This repository has been archived on 2022-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
Twxtter-main/twitfix.py

77 lines
2.9 KiB
Python
Raw Normal View History

2021-07-04 03:46:02 +02:00
from flask import Flask, render_template
2021-07-04 01:52:30 +02:00
import youtube_dl
import json
import re
2021-07-04 01:52:30 +02:00
app = Flask(__name__)
pathregex = re.compile("\\w{1,15}\\/status\\/\\d{19}")
2021-07-04 01:52:30 +02:00
link_cache = {}
f = open('links.json',)
link_cache = json.load(f)
f.close()
2021-07-06 05:30:16 +02:00
@app.route('/')
def default():
return render_template('default.html', message="TwitFix is an attempt to fix twitter video embeds in discord click this link to be directed to my github page for the project!")
@app.route('/<path:subpath>')
2021-07-04 01:52:30 +02:00
def twitfix(subpath):
match = pathregex.search(subpath)
if match is not None:
twitter_url = subpath
if match.start() == 0:
twitter_url = "https://twitter.com/" + subpath
res = embedVideo(twitter_url)
return res
2021-07-04 01:52:30 +02:00
else:
return render_template('default.html', message="This doesn't seem to be a twitter link, try /other/ to see if other kinds of video link works? (experimental)")
2021-07-04 01:52:30 +02:00
@app.route('/other/<path:subpath>') # Show all info that Youtube-DL can get about a video as a json
def other(subpath):
res = embedVideo(subpath)
return res
@app.route('/info/<path:subpath>') # Show all info that Youtube-DL can get about a video as a json
2021-07-04 01:52:30 +02:00
def info(subpath):
2021-07-04 23:54:23 +02:00
with youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'}) as ydl:
2021-07-04 01:52:30 +02:00
result = ydl.extract_info(subpath, download=False)
return result
def embedVideo(vidlink): # Return a render template from a video url
if vidlink in link_cache:
print("Link located in cache")
return render_template('index.html', vidurl=link_cache[vidlink]['url'], desc=link_cache[vidlink]['description'], pic=link_cache[vidlink]['thumbnail'], user=link_cache[vidlink]['uploader'], vidlink=vidlink)
else:
with youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'}) as ydl:
try:
print("Link not in cache, downloading and adding details to cache file")
result = ydl.extract_info(vidlink, download=False)
vnf = vidInfo(result['url'], result['description'], result['thumbnail'], result['uploader'])
link_cache[vidlink] = vnf
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("Failed to download link")
return render_template('default.html', message="Failed to scan your link!")
return render_template('index.html', vidurl=vnf['url'], desc=vnf['description'], pic=vnf['thumbnail'], user=vnf['uploader'], vidlink=vidlink)
def vidInfo(url, desc="", thumb="", uploader=""): # Return a dict of video info with default values
vnf = {
"url" :url,
"description" :desc,
"thumbnail" :thumb,
"uploader" :uploader
}
return vnf
2021-07-04 01:52:30 +02:00
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)