2021-07-04 03:46:02 +02:00
from flask import Flask , render_template
2021-07-08 05:17:23 +02:00
import pymongo
2021-07-04 01:52:30 +02:00
import youtube_dl
2021-07-04 11:40:22 +02:00
import json
2021-07-04 03:41:47 +02:00
import re
2021-07-04 01:52:30 +02:00
app = Flask ( __name__ )
2021-07-04 03:41:47 +02:00
pathregex = re . compile ( " \\ w { 1,15} \\ /status \\ / \\ d {19} " )
2021-07-04 01:52:30 +02:00
2021-07-08 05:17:23 +02:00
link_cache_system = " json " # Select your prefered link cache system, "db" for mongoDB or "json" for locally writing a json file ( not great if using multiple workers )
if link_cache_system == " json " :
link_cache = { }
f = open ( ' links.json ' , )
link_cache = json . load ( f )
f . close ( )
elif link_cache_system == " db " :
client = pymongo . MongoClient ( " PUT YOUR MONGODB URL HERE " )
db = client . TwitFix
2021-07-04 11:40:22 +02:00
2021-07-06 05:30:16 +02:00
@app.route ( ' / ' )
def default ( ) :
2021-07-08 05:17:23 +02:00
return render_template ( ' default.html ' , message = " TwitFix is an attempt to fix twitter video embeds in discord! created by Robin Universe :) 💖 " )
2021-07-06 05:30:16 +02:00
2021-07-09 12:32:04 +02:00
@app.route ( ' /oembed.json ' )
def oembedend ( ) :
desc = request . args . get ( " desc " , None )
user = request . args . get ( " user " , None )
link = request . args . get ( " link " , None )
return oEmbedGen ( desc , user , link )
2021-07-04 02:08:19 +02:00
@app.route ( ' /<path:subpath> ' )
2021-07-04 01:52:30 +02:00
def twitfix ( subpath ) :
2021-07-04 03:41:47 +02:00
match = pathregex . search ( subpath )
if match is not None :
twitter_url = subpath
if match . start ( ) == 0 :
twitter_url = " https://twitter.com/ " + subpath
2021-07-05 18:02:17 +02:00
res = embedVideo ( twitter_url )
return res
2021-07-04 01:52:30 +02:00
else :
2021-07-05 18:02:17 +02:00
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
2021-07-05 18:02:17 +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
2021-07-05 18:02:17 +02:00
def embedVideo ( vidlink ) : # Return a render template from a video url
2021-07-08 05:17:23 +02:00
if link_cache_system == " db " :
collection = db . linkCache
dbresult = collection . find_one ( { ' tweet ' : vidlink } )
if dbresult != None :
print ( " Link located in DB cache " )
return render_template ( ' index.html ' , vidurl = dbresult [ ' url ' ] , desc = dbresult [ ' description ' ] , pic = dbresult [ ' thumbnail ' ] , user = dbresult [ ' uploader ' ] , vidlink = vidlink )
else :
with youtube_dl . YoutubeDL ( { ' outtmpl ' : ' %(id)s . %(ext)s ' } ) as ydl :
try :
print ( " Link not in json cache, downloading and adding details to cache file " )
result = ydl . extract_info ( vidlink , download = False )
vnf = vidInfo ( result [ ' url ' ] , vidlink , result [ ' description ' ] , result [ ' thumbnail ' ] , result [ ' uploader ' ] )
try :
out = db . linkCache . insert_one ( vnf )
print ( " Link added to DB cache " )
except Exception :
print ( " Failed to add link to DB cache " )
return render_template ( ' index.html ' , vidurl = vnf [ ' url ' ] , desc = vnf [ ' description ' ] , pic = vnf [ ' thumbnail ' ] , user = vnf [ ' uploader ' ] , vidlink = vidlink )
except Exception :
print ( " Failed to download link " )
return render_template ( ' default.html ' , message = " Failed to scan your link! " )
elif link_cache_system == " json " :
if vidlink in link_cache :
print ( " Link located in json 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 json cache, downloading and adding details to cache file " )
result = ydl . extract_info ( vidlink , download = False )
vnf = vidInfo ( result [ ' url ' ] , vidlink , 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 )
2021-07-05 18:02:17 +02:00
else :
2021-07-08 05:17:23 +02:00
try :
with youtube_dl . YoutubeDL ( { ' outtmpl ' : ' %(id)s . %(ext)s ' } ) as ydl :
result = ydl . extract_info ( subpath , download = False )
vnf = vidInfo ( result [ ' url ' ] , vidlink , result [ ' description ' ] , result [ ' thumbnail ' ] , result [ ' uploader ' ] )
return render_template ( ' index.html ' , vidurl = vnf [ ' url ' ] , desc = vnf [ ' description ' ] , pic = vnf [ ' thumbnail ' ] , user = vnf [ ' uploader ' ] , vidlink = vidlink )
except Exception :
print ( " Failed to download link " )
return render_template ( ' default.html ' , message = " Failed to scan your link! " )
2021-07-05 18:02:17 +02:00
2021-07-08 05:17:23 +02:00
def vidInfo ( url , tweet = " " , desc = " " , thumb = " " , uploader = " " ) : # Return a dict of video info with default values
2021-07-05 18:02:17 +02:00
vnf = {
2021-07-08 05:17:23 +02:00
" tweet " : tweet ,
2021-07-05 18:02:17 +02:00
" url " : url ,
" description " : desc ,
" thumbnail " : thumb ,
" uploader " : uploader
}
return vnf
2021-07-09 12:32:04 +02:00
def oEmbedGen ( description , user , vidlink ) :
out = {
" type " : " video " ,
" version " : " 1.0 " ,
" provider_name " : " TwitFix " ,
" provider_url " : " https://github.com/robinuniverse/twitfix " ,
" title " : description ,
" author_name " : user ,
" author_url " : vidlink
}
return out
2021-07-04 01:52:30 +02:00
if __name__ == " __main__ " :
2021-07-08 05:17:23 +02:00
app . run ( host = ' 0.0.0.0 ' )