Improved messages for errors, removed apiMirrors
This commit is contained in:
parent
bbcb3ceb05
commit
403b82d7fe
@ -11,8 +11,7 @@ if ('RUNNING_SERVERLESS' in os.environ and os.environ['RUNNING_SERVERLESS'] == '
|
||||
"appname": os.environ["VXTWITTER_APP_NAME"],
|
||||
"repo": os.environ["VXTWITTER_REPO"],
|
||||
"url": os.environ["VXTWITTER_URL"],
|
||||
"combination_method": os.environ["VXTWITTER_COMBINATION_METHOD"], # can either be 'local' or a URL to a server handling requests in the same format
|
||||
"apiMirrors":[]
|
||||
"combination_method": os.environ["VXTWITTER_COMBINATION_METHOD"] # can either be 'local' or a URL to a server handling requests in the same format
|
||||
}
|
||||
}
|
||||
else:
|
||||
@ -28,8 +27,7 @@ else:
|
||||
"appname": "vxTwitter",
|
||||
"repo": "https://github.com/dylanpdx/BetterTwitFix",
|
||||
"url": "https://vxtwitter.com",
|
||||
"combination_method": "local", # can either be 'local' or a URL to a server handling requests in the same format
|
||||
"apiMirrors":[]
|
||||
"combination_method": "local" # can either be 'local' or a URL to a server handling requests in the same format
|
||||
}
|
||||
}
|
||||
|
||||
|
4
msgs.py
4
msgs.py
@ -1,5 +1,5 @@
|
||||
failedToScan="Failed to scan your link! This may be due to an incorrect link, private account, or the twitter API itself might be having issues (Check here: https://api.twitterstat.us/)\nIt's also possible that Twitter is API limiting me, in which case I can't do anything about it."
|
||||
|
||||
failedToScan="Failed to scan your link! This may be due to an incorrect link, private/suspended account, deleted tweet, or Twitter itself might be having issues (Check here: https://api.twitterstat.us/)"
|
||||
failedToScanExtra = "\n\nTwitter gave me this error: "
|
||||
|
||||
def genLikesDisplay(vnf):
|
||||
return ("\n\n💖 " + str(vnf['likes']) + " 🔁 " + str(vnf['rts']) + "\n")
|
||||
|
@ -54,9 +54,6 @@ vxTwitter generates a config.json in its root directory the first time you run i
|
||||
|
||||
**combination_method** - using c.vxtwitter as the url causes vxTwitter to combine all images in the post into one. This is CPU intensive, so you might not want it running on the same machine that's serving requests. When `combination_method` is set to `local`, it will use the local machine to combine the images. This requires pillow to be installed. If you want to use another server, replace `local` with the URL to the endpoint which combines images. Both methods use the code in the `combineImg` module. Inside, there's also a `Dockerfile` intended to be deployed as a combination endpoint on an [AWS Lambda function](https://docs.aws.amazon.com/lambda/latest/dg/images-create.html).
|
||||
|
||||
**apiMirrors** - During an influx of traffic (i.e when Twitter embeds break!) it's very likely that Twitter will begin to block you for sending too many requests.
|
||||
This is an array of replacement Twitter API URLs which can be called upon when requesting tweet info locally fails. An example of an implementation of an API Mirror is in the twExtract directory, and is intended to be hosted on AWS Lambda. If multiple mirror URLs are specified, one will be selected at random.
|
||||
|
||||
## Other stuff
|
||||
|
||||
We check for t.co links in non video tweets, and if one is found, we direct the discord useragent to embed that link directly, this means that twitter links containing youtube / vimeo links will automatically embed those as if you had just directly linked to that content
|
||||
|
@ -48,7 +48,7 @@ functions:
|
||||
handler: wsgi_handler.handler
|
||||
url: true
|
||||
timeout: 5
|
||||
memorySize: 300
|
||||
memorySize: 150
|
||||
layers:
|
||||
- Ref: PythonRequirementsLambdaLayer
|
||||
|
||||
|
61
twitfix.py
61
twitfix.py
@ -1,8 +1,10 @@
|
||||
from email import utils
|
||||
from random import Random, random
|
||||
from weakref import finalize
|
||||
from flask import Flask, render_template, request, redirect, abort, Response, send_from_directory, url_for, send_file, make_response, jsonify
|
||||
from flask_cors import CORS
|
||||
import textwrap
|
||||
from pkg_resources import ExtractionError
|
||||
import requests
|
||||
import re
|
||||
import os
|
||||
@ -16,6 +18,7 @@ import twExtract as twExtract
|
||||
from configHandler import config
|
||||
from cache import addVnfToLinkCache,getVnfFromLinkCache
|
||||
import random
|
||||
from yt_dlp.utils import ExtractorError
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
@ -68,7 +71,11 @@ def twitfix(sub_path):
|
||||
else:
|
||||
clean = twitter_url
|
||||
|
||||
vnf = vnfFromCacheOrDL(clean)
|
||||
vnf,e = vnfFromCacheOrDL(clean)
|
||||
if vnf == None:
|
||||
if e is not None:
|
||||
return message(msgs.failedToScan+msgs.failedToScanExtra+e)
|
||||
return message(msgs.failedToScan)
|
||||
return getTemplate("rawvideo.html",vnf,"","",clean,"","","","")
|
||||
else:
|
||||
return message("To use a direct MP4 link in discord, remove anything past '?' and put '.mp4' at the end")
|
||||
@ -94,7 +101,11 @@ def twitfix(sub_path):
|
||||
else:
|
||||
clean = twitter_url
|
||||
|
||||
vnf = vnfFromCacheOrDL(clean)
|
||||
vnf,e = vnfFromCacheOrDL(clean)
|
||||
if vnf is None:
|
||||
if e is not None:
|
||||
return message(msgs.failedToScan+msgs.failedToScanExtra+e)
|
||||
return message(msgs.failedToScan)
|
||||
return getTemplate("rawvideo.html",vnf,"","",clean,"","","","")
|
||||
|
||||
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"):
|
||||
@ -194,33 +205,47 @@ def vnfFromCacheOrDL(video_link):
|
||||
try:
|
||||
vnf = link_to_vnf(video_link)
|
||||
addVnfToLinkCache(video_link, vnf)
|
||||
return vnf
|
||||
return vnf,None
|
||||
except ExtractorError as exErr:
|
||||
if 'HTTP Error 404' in exErr.msg:
|
||||
exErr.msg="Tweet not found."
|
||||
elif 'suspended' in exErr.msg:
|
||||
exErr.msg="This Tweet is from a suspended account."
|
||||
else:
|
||||
exErr.msg=None
|
||||
return None,exErr.msg
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return None
|
||||
return None,None
|
||||
else:
|
||||
return upgradeVNF(cached_vnf)
|
||||
return upgradeVNF(cached_vnf),None
|
||||
|
||||
def direct_video(video_link): # Just get a redirect to a MP4 link from any tweet link
|
||||
vnf = vnfFromCacheOrDL(video_link)
|
||||
vnf,e = vnfFromCacheOrDL(video_link)
|
||||
if vnf != None:
|
||||
return redirect(vnf['url'], 301)
|
||||
else:
|
||||
if e is not None:
|
||||
return message(msgs.failedToScan+msgs.failedToScanExtra+e)
|
||||
return message(msgs.failedToScan)
|
||||
|
||||
def direct_video_link(video_link): # Just get a redirect to a MP4 link from any tweet link
|
||||
vnf = vnfFromCacheOrDL(video_link)
|
||||
vnf,e = vnfFromCacheOrDL(video_link)
|
||||
if vnf != None:
|
||||
return vnf['url']
|
||||
else:
|
||||
if e is not None:
|
||||
return message(msgs.failedToScan+msgs.failedToScanExtra+e)
|
||||
return message(msgs.failedToScan)
|
||||
|
||||
def embed_video(video_link, image=0): # Return Embed from any tweet link
|
||||
vnf = vnfFromCacheOrDL(video_link)
|
||||
vnf,e = vnfFromCacheOrDL(video_link)
|
||||
|
||||
if vnf != None:
|
||||
return embed(video_link, vnf, image)
|
||||
else:
|
||||
if e is not None:
|
||||
return message(msgs.failedToScan+msgs.failedToScanExtra+e)
|
||||
return message(msgs.failedToScan)
|
||||
|
||||
def tweetInfo(url, tweet="", desc="", thumb="", uploader="", screen_name="", pfp="", tweetType="", images="", hits=0, likes=0, rts=0, time="", qrt={}, nsfw=False,ttl=None,verified=False,size={}): # Return a dict of video info with default values
|
||||
@ -327,15 +352,9 @@ def link_to_vnf_from_tweet_data(tweet,video_link):
|
||||
|
||||
|
||||
def link_to_vnf_from_unofficial_api(video_link):
|
||||
tweet=None
|
||||
print(" ➤ [ + ] Attempting to download tweet info from UNOFFICIAL Twitter API")
|
||||
try:
|
||||
tweet = twExtract.extractStatus(video_link)
|
||||
except Exception as e:
|
||||
print(' ➤ [ !!! ] Local UNOFFICIAL API Failed')
|
||||
if ('apiMirrors' in config['config'] and len(config['config']['apiMirrors']) > 0):
|
||||
mirror = random.choice(config['config']['apiMirrors'])
|
||||
print(" ➤ [ + ] Using API Mirror: "+mirror)
|
||||
tweet = requests.get(mirror+"?url="+video_link).json()
|
||||
tweet = twExtract.extractStatus(video_link)
|
||||
print (" ➤ [ ✔ ] Unofficial API Success")
|
||||
return link_to_vnf_from_tweet_data(tweet,video_link)
|
||||
|
||||
@ -345,11 +364,7 @@ def link_to_vnf_from_api(video_link):
|
||||
return link_to_vnf_from_tweet_data(tweet,video_link)
|
||||
|
||||
def link_to_vnf(video_link): # Return a VideoInfo object or die trying
|
||||
try:
|
||||
return link_to_vnf_from_unofficial_api(video_link)
|
||||
except Exception as e:
|
||||
print(" ➤ [ !!! ] Unofficial Twitter API Failed")
|
||||
print(e)
|
||||
return link_to_vnf_from_unofficial_api(video_link)
|
||||
|
||||
def message(text):
|
||||
return render_template(
|
||||
@ -433,11 +448,13 @@ def embed(video_link, vnf, image):
|
||||
|
||||
|
||||
def embedCombined(video_link):
|
||||
vnf = vnfFromCacheOrDL(video_link)
|
||||
vnf,e = vnfFromCacheOrDL(video_link)
|
||||
|
||||
if vnf != None:
|
||||
return embedCombinedVnf(video_link, vnf)
|
||||
else:
|
||||
if e is not None:
|
||||
return message(msgs.failedToScan+msgs.failedToScanExtra+e)
|
||||
return message(msgs.failedToScan)
|
||||
|
||||
def embedCombinedVnf(video_link,vnf):
|
||||
|
Loading…
x
Reference in New Issue
Block a user