2021-07-09 21:04:46 +02:00
from flask import Flask , render_template , request
2021-07-04 01:52:30 +02:00
import youtube_dl
2021-07-12 22:48:21 +02:00
import textwrap
2021-07-12 10:49:07 +02:00
import twitter
import pymongo
2021-07-04 11:40:22 +02:00
import json
2021-07-04 03:41:47 +02:00
import re
2021-07-11 22:48:17 +02:00
import os
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-11 22:48:17 +02:00
if not os . path . exists ( " config.json " ) :
with open ( " config.json " , " w " ) as outfile :
2021-07-12 10:49:07 +02:00
default = { " config " : { " link_cache " : " json " , " database " : " [url to mongo database goes here] " , " method " : " youtube-dl " } , " api " : { " api_key " : " [api_key goes here] " , " consumer_secret " : " [api_secret goes here] " , " access_token " : " [access_token goes here] " , " access_secret " : " [access_secret goes here] " } }
2021-07-11 22:48:17 +02:00
json . dump ( default , outfile , indent = 4 , sort_keys = True )
f = open ( " config.json " )
config = json . load ( f )
f . close ( )
2021-07-12 21:49:29 +02:00
if config [ ' config ' ] [ ' method ' ] in ( ' api ' , ' hybrid ' ) :
2021-07-12 10:49:07 +02:00
auth = twitter . oauth . OAuth ( config [ ' api ' ] [ ' access_token ' ] , config [ ' api ' ] [ ' access_secret ' ] , config [ ' api ' ] [ ' api_key ' ] , config [ ' api ' ] [ ' api_secret ' ] )
twitter_api = twitter . Twitter ( auth = auth )
link_cache_system = config [ ' config ' ] [ ' link_cache ' ]
2021-07-08 05:17:23 +02:00
if link_cache_system == " json " :
link_cache = { }
2021-07-11 22:48:17 +02:00
if not os . path . exists ( " config.json " ) :
with open ( " config.json " , " w " ) as outfile :
deflinkcache = { " test " : " test " }
json . dump ( deflinkcache , outfile , indent = 4 , sort_keys = True )
2021-07-08 05:17:23 +02:00
f = open ( ' links.json ' , )
link_cache = json . load ( f )
f . close ( )
elif link_cache_system == " db " :
2021-07-11 22:48:17 +02:00
client = pymongo . MongoClient ( config [ ' config ' ] [ ' database ' ] , connect = False )
2021-07-08 05:17:23 +02:00
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-12 10:49:07 +02:00
def embedVideo ( vidlink ) :
cached_vnf = getVNFfromLinkCache ( vidlink )
2021-07-08 05:17:23 +02:00
2021-07-12 10:49:07 +02:00
if cached_vnf == None :
2021-07-08 05:17:23 +02:00
try :
2021-07-12 10:49:07 +02:00
vnf = linkToVNF ( vidlink )
addVNFtoLinkCache ( vidlink , vnf )
return embed ( vidlink , vnf )
except Exception as e :
print ( e )
2021-07-08 05:17:23 +02:00
return render_template ( ' default.html ' , message = " Failed to scan your link! " )
2021-07-12 10:49:07 +02:00
else :
return embed ( vidlink , cached_vnf )
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-12 20:49:19 +02:00
def linkToVNFfromAPI ( vidlink ) :
print ( " Attempting to download tweet info from Twitter API " )
twid = int ( re . sub ( r ' \ ?.*$ ' , ' ' , vidlink . rsplit ( " / " , 1 ) [ - 1 ] ) ) # gets the tweet ID as a int from the passed url
tweet = twitter_api . statuses . show ( _id = twid , tweet_mode = " extended " )
if tweet [ ' extended_entities ' ] [ ' media ' ] [ 0 ] [ ' video_info ' ] [ ' variants ' ] [ - 1 ] [ ' content_type ' ] == " video/mp4 " :
url = tweet [ ' extended_entities ' ] [ ' media ' ] [ 0 ] [ ' video_info ' ] [ ' variants ' ] [ - 1 ] [ ' url ' ]
else :
url = tweet [ ' extended_entities ' ] [ ' media ' ] [ 0 ] [ ' video_info ' ] [ ' variants ' ] [ - 2 ] [ ' url ' ]
2021-07-12 23:41:07 +02:00
if len ( tweet [ ' full_text ' ] ) > 200 :
text = textwrap . shorten ( tweet [ ' full_text ' ] , width = 200 , placeholder = " ... " )
else :
text = tweet [ ' full_text ' ]
2021-07-12 22:48:21 +02:00
print ( text )
2021-07-12 23:41:07 +02:00
print ( len ( text ) )
2021-07-12 22:48:21 +02:00
vnf = vidInfo ( url , vidlink , text , tweet [ ' extended_entities ' ] [ ' media ' ] [ 0 ] [ ' media_url ' ] , tweet [ ' user ' ] [ ' name ' ] )
2021-07-12 20:49:19 +02:00
return vnf
def linkToVNFfromYoutubeDL ( vidlink ) :
print ( " Attempting to download tweet info via YoutubeDL " )
with youtube_dl . YoutubeDL ( { ' outtmpl ' : ' %(id)s . %(ext)s ' } ) as ydl :
result = ydl . extract_info ( vidlink , download = False )
2021-07-12 22:48:21 +02:00
vnf = vidInfo ( result [ ' url ' ] , vidlink , result [ ' description ' ] . rsplit ( ' ' , 1 ) [ 0 ] , result [ ' thumbnail ' ] , result [ ' uploader ' ] )
2021-07-12 10:49:07 +02:00
return vnf
2021-07-12 20:49:19 +02:00
def linkToVNF ( vidlink ) : # Return a VideoInfo object or die trying
if config [ ' config ' ] [ ' method ' ] == ' hybrid ' :
try :
return linkToVNFfromAPI ( vidlink )
2021-07-12 21:49:29 +02:00
except Exception as e :
2021-07-12 20:49:19 +02:00
print ( " API Failed " )
2021-07-12 21:49:29 +02:00
print ( e )
2021-07-12 20:49:19 +02:00
return linkToVNFfromYoutubeDL ( vidlink )
elif config [ ' config ' ] [ ' method ' ] == ' api ' :
try :
return linkToVNFfromAPI ( vidlink )
except Exception as e :
print ( " API Failed " )
print ( e )
return None
elif config [ ' config ' ] [ ' method ' ] == ' youtube-dl ' :
try :
return linkToVNFfromYoutubeDL ( vidlink )
except Exception as e :
print ( " Youtube-DL Failed " )
print ( e )
return None
else :
print ( " Please set the method key in your config file to ' api ' ' youtube-dl ' or ' hybrid ' " )
return None
2021-07-12 10:49:07 +02:00
def getVNFfromLinkCache ( vidlink ) :
if link_cache_system == " db " :
collection = db . linkCache
vnf = collection . find_one ( { ' tweet ' : vidlink } )
if vnf != None :
print ( " Link located in DB cache " )
return vnf
else :
print ( " Link not in DB cache " )
return None
elif link_cache_system == " json " :
if vidlink in link_cache :
print ( " Link located in json cache " )
vnf = link_cache [ vidlink ]
return vnf
else :
print ( " Link not in json cache " )
return None
def addVNFtoLinkCache ( vidlink , vnf ) :
if link_cache_system == " db " :
try :
out = db . linkCache . insert_one ( vnf )
print ( " Link added to DB cache " )
return True
except Exception :
print ( " Failed to add link to DB cache " )
return None
elif link_cache_system == " json " :
link_cache [ vidlink ] = vnf
with open ( " links.json " , " w " ) as outfile :
json . dump ( link_cache , outfile , indent = 4 , sort_keys = True )
return None
def embed ( vidlink , vnf ) :
2021-07-12 23:41:07 +02:00
desc = re . sub ( r ' http.*t \ .co \ S+ ' , ' ' , vnf [ ' description ' ] . replace ( " # " , " # " ) )
return render_template ( ' index.html ' , vidurl = vnf [ ' url ' ] , desc = desc , pic = vnf [ ' thumbnail ' ] , user = vnf [ ' uploader ' ] , vidlink = vidlink )
2021-07-12 10:49:07 +02:00
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 ' )