Removed tracking/stats, anything non-embed related
This commit is contained in:
172
twitfix.py
172
twitfix.py
@ -82,86 +82,6 @@ elif link_cache_system == "db":
|
||||
table = config['config']['table']
|
||||
db = client[table]
|
||||
|
||||
@app.route('/stats/')
|
||||
def statsPage():
|
||||
today = str(date.today())
|
||||
stats = getStats(today)
|
||||
return render_template('stats.html', embeds=stats['embeds'], downloadss=stats['downloads'], api=stats['api'], linksCached=stats['linksCached'], date=today)
|
||||
|
||||
@app.route('/latest/')
|
||||
def latest():
|
||||
return render_template('latest.html')
|
||||
|
||||
@app.route('/copy.svg') # Return a SVG needed for Latest
|
||||
def icon():
|
||||
return send_from_directory(os.path.join(app.root_path, 'static'),
|
||||
'copy.svg',mimetype='image/svg+xml')
|
||||
|
||||
@app.route('/font.ttf') # Return a font needed for Latest
|
||||
def font():
|
||||
return send_from_directory(os.path.join(app.root_path, 'static'),
|
||||
'NotoColorEmoji.ttf',mimetype='application/octet-stream')
|
||||
|
||||
@app.route('/top/') # Try to return the most hit video
|
||||
def top():
|
||||
vnf = db.linkCache.find_one(sort = [('hits', pymongo.DESCENDING)])
|
||||
desc = re.sub(r' http.*t\.co\S+', '', vnf['description'])
|
||||
urlUser = urllib.parse.quote(vnf['uploader'])
|
||||
urlDesc = urllib.parse.quote(desc)
|
||||
urlLink = urllib.parse.quote(vnf['url'])
|
||||
print(" ➤ [ ✔ ] Top video page loaded: " + vnf['tweet'] )
|
||||
return render_template('inline.html', page="Top", vidlink=vnf['url'], vidurl=vnf['url'], desc=desc, pic=vnf['thumbnail'], user=vnf['uploader'], video_link=vnf['url'], color=config['config']['color'], appname=config['config']['appname'], repo=config['config']['repo'], url=config['config']['url'], urlDesc=urlDesc, urlUser=urlUser, urlLink=urlLink, tweet=vnf['tweet'])
|
||||
|
||||
@app.route('/api/latest/') # Return some raw VNF data sorted by top tweets
|
||||
def apiLatest():
|
||||
bigvnf = []
|
||||
|
||||
tweets = request.args.get("tweets", default=10, type=int)
|
||||
page = request.args.get("page", default=0, type=int)
|
||||
|
||||
if tweets > 15:
|
||||
tweets = 1
|
||||
|
||||
vnf = db.linkCache.find(sort = [('_id', pymongo.DESCENDING)]).skip(tweets * page).limit(tweets)
|
||||
|
||||
for r in vnf:
|
||||
bigvnf.append(r)
|
||||
|
||||
print(" ➤ [ ✔ ] Latest video API called")
|
||||
addToStat('api')
|
||||
return Response(response=json.dumps(bigvnf, default=str), status=200, mimetype="application/json")
|
||||
|
||||
@app.route('/api/top/') # Return some raw VNF data sorted by top tweets
|
||||
def apiTop():
|
||||
bigvnf = []
|
||||
|
||||
tweets = request.args.get("tweets", default=10, type=int)
|
||||
page = request.args.get("page", default=0, type=int)
|
||||
|
||||
if tweets > 15:
|
||||
tweets = 1
|
||||
|
||||
vnf = db.linkCache.find(sort = [('hits', pymongo.DESCENDING )]).skip(tweets * page).limit(tweets)
|
||||
|
||||
for r in vnf:
|
||||
bigvnf.append(r)
|
||||
|
||||
print(" ➤ [ ✔ ] Top video API called")
|
||||
addToStat('api')
|
||||
return Response(response=json.dumps(bigvnf, default=str), status=200, mimetype="application/json")
|
||||
|
||||
@app.route('/api/stats/') # Return a json of a usage stats for a given date (defaults to today)
|
||||
def apiStats():
|
||||
try:
|
||||
addToStat('api')
|
||||
today = str(date.today())
|
||||
desiredDate = request.args.get("date", default=today, type=str)
|
||||
stat = getStats(desiredDate)
|
||||
print (" ➤ [ ✔ ] Stats API called")
|
||||
return Response(response=json.dumps(stat, default=str), status=200, mimetype="application/json")
|
||||
except:
|
||||
print (" ➤ [ ✔ ] Stats API failed")
|
||||
|
||||
@app.route('/') # If the useragent is discord, return the embed, if not, redirect to configured repo directly
|
||||
def default():
|
||||
user_agent = request.headers.get('user-agent')
|
||||
@ -184,9 +104,9 @@ def twitfix(sub_path):
|
||||
match = pathregex.search(sub_path)
|
||||
print(request.url)
|
||||
|
||||
if request.url.startswith("https://d.fx"): # Matches d.fx? Try to give the user a direct link
|
||||
if request.url.startswith("https://d.vx"): # Matches d.fx? Try to give the user a direct link
|
||||
if user_agent in generate_embed_user_agents:
|
||||
print( " ➤ [ D ] d.fx link shown to discord user-agent!")
|
||||
print( " ➤ [ D ] d.vx link shown to discord user-agent!")
|
||||
if request.url.endswith(".mp4") and "?" not in request.url:
|
||||
return dl(sub_path)
|
||||
else:
|
||||
@ -205,22 +125,22 @@ def twitfix(sub_path):
|
||||
|
||||
return dl(clean)
|
||||
|
||||
elif request.url.endswith(".json") or request.url.endswith("%2Ejson"):
|
||||
twitter_url = "https://twitter.com/" + sub_path
|
||||
# elif request.url.endswith(".json") or request.url.endswith("%2Ejson"):
|
||||
# twitter_url = "https://twitter.com/" + sub_path
|
||||
|
||||
if "?" not in request.url:
|
||||
clean = twitter_url[:-5]
|
||||
else:
|
||||
clean = twitter_url
|
||||
# if "?" not in request.url:
|
||||
# clean = twitter_url[:-5]
|
||||
# else:
|
||||
# clean = twitter_url
|
||||
|
||||
print( " ➤ [ API ] VNF Json api hit!")
|
||||
# print( " ➤ [ API ] VNF Json api hit!")
|
||||
|
||||
vnf = link_to_vnf_from_api(clean.replace(".json",""))
|
||||
# vnf = link_to_vnf_from_api(clean.replace(".json",""))
|
||||
|
||||
if user_agent in generate_embed_user_agents:
|
||||
return message("VNF Data: ( discord useragent preview )\n\n"+ json.dumps(vnf, default=str))
|
||||
else:
|
||||
return Response(response=json.dumps(vnf, default=str), status=200, mimetype="application/json")
|
||||
# if user_agent in generate_embed_user_agents:
|
||||
# return message("VNF Data: ( discord useragent preview )\n\n"+ json.dumps(vnf, default=str))
|
||||
# else:
|
||||
# return Response(response=json.dumps(vnf, default=str), status=200, mimetype="application/json")
|
||||
|
||||
elif request.url.endswith("/1") or request.url.endswith("/2") or request.url.endswith("/3") or request.url.endswith("/4") or request.url.endswith("%2F1") or request.url.endswith("%2F2") or request.url.endswith("%2F3") or request.url.endswith("%2F4"):
|
||||
twitter_url = "https://twitter.com/" + sub_path
|
||||
@ -249,51 +169,6 @@ def twitfix(sub_path):
|
||||
else:
|
||||
return message("This doesn't appear to be a twitter URL")
|
||||
|
||||
@app.route('/other/<path:sub_path>') # Show all info that Youtube-DL can get about a video as a json
|
||||
def other(sub_path):
|
||||
otherurl = request.url.split("/other/", 1)[1].replace(":/","://")
|
||||
print(" ➤ [ OTHER ] Other URL embed attempted: " + otherurl)
|
||||
res = embed_video(otherurl)
|
||||
return res
|
||||
|
||||
@app.route('/info/<path:sub_path>') # Show all info that Youtube-DL can get about a video as a json
|
||||
def info(sub_path):
|
||||
infourl = request.url.split("/info/", 1)[1].replace(":/","://")
|
||||
print(" ➤ [ INFO ] Info data requested: " + infourl)
|
||||
with youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'}) as ydl:
|
||||
result = ydl.extract_info(infourl, download=False)
|
||||
|
||||
return result
|
||||
|
||||
@app.route('/dl/<path:sub_path>') # Download the tweets video, and rehost it
|
||||
def dl(sub_path):
|
||||
print(' ➤ [[ !!! TRYING TO DOWNLOAD FILE !!! ]] Downloading file from ' + sub_path)
|
||||
url = sub_path
|
||||
match = pathregex.search(url)
|
||||
if match is not None:
|
||||
twitter_url = url
|
||||
if match.start() == 0:
|
||||
twitter_url = "https://twitter.com/" + url
|
||||
|
||||
mp4link = direct_video_link(twitter_url)
|
||||
filename = (sub_path.split('/')[-1].split('.mp4')[0] + '.mp4')
|
||||
|
||||
PATH = ( './static/' + filename )
|
||||
if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
|
||||
print(" ➤ [[ FILE EXISTS ]]")
|
||||
else:
|
||||
print(" ➤ [[ FILE DOES NOT EXIST, DOWNLOADING... ]]")
|
||||
addToStat('downloads')
|
||||
mp4file = urllib.request.urlopen(mp4link)
|
||||
with open(('/home/robin/twitfix/static/' + filename), 'wb') as output:
|
||||
output.write(mp4file.read())
|
||||
|
||||
print(' ➤ [[ PRESENTING FILE: '+ filename +', URL: https://fxtwitter.com/static/'+ filename +' ]]')
|
||||
r = make_response(send_file(('static/' + filename), mimetype='video/mp4', max_age=100))
|
||||
r.headers['Content-Type'] = 'video/mp4'
|
||||
r.headers['Sec-Fetch-Site'] = 'none'
|
||||
r.headers['Sec-Fetch-User'] = '?1'
|
||||
return r
|
||||
|
||||
@app.route('/dir/<path:sub_path>') # Try to return a direct link to the MP4 on twitters servers
|
||||
def dir(sub_path):
|
||||
@ -351,23 +226,6 @@ def direct_video_link(video_link): # Just get a redirect to a MP4 link from any
|
||||
return cached_vnf['url']
|
||||
print(" ➤ [ D ] Redirecting to direct URL: " + vnf['url'])
|
||||
|
||||
def addToStat(stat):
|
||||
#print(stat)
|
||||
today = str(date.today())
|
||||
try:
|
||||
collection = db.stats.find_one({'date': today})
|
||||
delta = ( collection[stat] + 1 )
|
||||
query = { "date" : today }
|
||||
change = { "$set" : { stat : delta } }
|
||||
out = db.stats.update_one(query, change)
|
||||
except:
|
||||
collection = db.stats.insert_one({'date': today, "embeds" : 1, "linksCached" : 1, "api" : 1, "downloads" : 1 })
|
||||
|
||||
|
||||
def getStats(day):
|
||||
collection = db.stats.find_one({'date': day})
|
||||
return collection
|
||||
|
||||
def embed_video(video_link, image=0): # Return Embed from any tweet link
|
||||
cached_vnf = getVnfFromLinkCache(video_link)
|
||||
|
||||
@ -511,7 +369,6 @@ def getVnfFromLinkCache(video_link):
|
||||
query = { 'tweet': video_link }
|
||||
change = { "$set" : { "hits" : hits } }
|
||||
out = db.linkCache.update_one(query, change)
|
||||
addToStat('embeds')
|
||||
return vnf
|
||||
else:
|
||||
print(" ➤ [ X ] Link not in DB cache")
|
||||
@ -530,7 +387,6 @@ def addVnfToLinkCache(video_link, vnf):
|
||||
try:
|
||||
out = db.linkCache.insert_one(vnf)
|
||||
print(" ➤ [ + ] Link added to DB cache ")
|
||||
addToStat('linksCached')
|
||||
return True
|
||||
except Exception:
|
||||
print(" ➤ [ X ] Failed to add link to DB cache")
|
||||
|
Reference in New Issue
Block a user