Several changes
1. Add the /latest/ endpoint 2. Fix a problem that was leading to infinite loops in some instances 3. Attempt to add Slack support via adding the useragent 4. Make the output a bit prettier and more descriptive
This commit is contained in:
parent
327bce259d
commit
3ab59daf28
1 changed files with 29 additions and 19 deletions
48
twitfix.py
48
twitfix.py
|
@ -10,7 +10,7 @@ import urllib.parse
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
pathregex = re.compile("\\w{1,15}\\/(status|statuses)\\/\\d{2,20}")
|
pathregex = re.compile("\\w{1,15}\\/(status|statuses)\\/\\d{2,20}")
|
||||||
generate_embed_user_agents = ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0", "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)", "TelegramBot (like TwitterBot)", "Mozilla/5.0 (compatible; January/1.0; +https://gitlab.insrt.uk/revolt/january)", "test"]
|
generate_embed_user_agents = ["Slackbot-LinkExpanding 1.0 (+https://api.slack.com/robots)", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0", "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)", "TelegramBot (like TwitterBot)", "Mozilla/5.0 (compatible; January/1.0; +https://gitlab.insrt.uk/revolt/january)", "test"]
|
||||||
|
|
||||||
# Read config from config.json. If it does not exist, create new.
|
# Read config from config.json. If it does not exist, create new.
|
||||||
if not os.path.exists("config.json"):
|
if not os.path.exists("config.json"):
|
||||||
|
@ -45,6 +45,16 @@ elif link_cache_system == "db":
|
||||||
client = pymongo.MongoClient(config['config']['database'], connect=False)
|
client = pymongo.MongoClient(config['config']['database'], connect=False)
|
||||||
db = client.TwitFix
|
db = client.TwitFix
|
||||||
|
|
||||||
|
@app.route('/latest/') # Try to return the latest video
|
||||||
|
def latest():
|
||||||
|
vnf = db.linkCache.find_one(sort = [('_id', 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(" [ ✔ ] Latest video page loaded: " + vnf['tweet'] )
|
||||||
|
return render_template('inline.html', 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('/') # If the useragent is discord, return the embed, if not, redirect to configured repo directly
|
@app.route('/') # If the useragent is discord, return the embed, if not, redirect to configured repo directly
|
||||||
def default():
|
def default():
|
||||||
user_agent = request.headers.get('user-agent')
|
user_agent = request.headers.get('user-agent')
|
||||||
|
@ -77,7 +87,7 @@ def twitfix(sub_path):
|
||||||
return res
|
return res
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Redirect to " + twitter_url)
|
print(" [ R ] Redirect to " + twitter_url)
|
||||||
return redirect(twitter_url, 301)
|
return redirect(twitter_url, 301)
|
||||||
else:
|
else:
|
||||||
return message("This doesn't appear to be a twitter URL")
|
return message("This doesn't appear to be a twitter URL")
|
||||||
|
@ -110,7 +120,7 @@ def dir(sub_path):
|
||||||
return res
|
return res
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Redirect to direct MP4 URL")
|
print(" [ R ] Redirect to direct MP4 URL")
|
||||||
return direct_video(twitter_url)
|
return direct_video(twitter_url)
|
||||||
else:
|
else:
|
||||||
return redirect(url, 301)
|
return redirect(url, 301)
|
||||||
|
@ -122,13 +132,13 @@ def direct_video(video_link): # Just get a redirect to a MP4 link from any tweet
|
||||||
vnf = link_to_vnf(video_link)
|
vnf = link_to_vnf(video_link)
|
||||||
add_vnf_to_link_cache(video_link, vnf)
|
add_vnf_to_link_cache(video_link, vnf)
|
||||||
return redirect(vnf['url'], 301)
|
return redirect(vnf['url'], 301)
|
||||||
print("Redirecting to direct URL: " + vnf['url'])
|
print(" [ D ] Redirecting to direct URL: " + vnf['url'])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
return message("Failed to scan your link!")
|
return message("Failed to scan your link!")
|
||||||
else:
|
else:
|
||||||
return redirect(cached_vnf['url'], 301)
|
return redirect(cached_vnf['url'], 301)
|
||||||
print("Redirecting to direct URL: " + vnf['url'])
|
print(" [ D ] Redirecting to direct URL: " + vnf['url'])
|
||||||
|
|
||||||
def embed_video(video_link): # Return Embed from any tweet link
|
def embed_video(video_link): # Return Embed from any tweet link
|
||||||
cached_vnf = get_vnf_from_link_cache(video_link)
|
cached_vnf = get_vnf_from_link_cache(video_link)
|
||||||
|
@ -156,7 +166,7 @@ def video_info(url, tweet="", desc="", thumb="", uploader=""): # Return a dict o
|
||||||
return vnf
|
return vnf
|
||||||
|
|
||||||
def link_to_vnf_from_api(video_link):
|
def link_to_vnf_from_api(video_link):
|
||||||
print("Attempting to download tweet info from Twitter API")
|
print(" [ + ] Attempting to download tweet info from Twitter API")
|
||||||
twid = int(re.sub(r'\?.*$','',video_link.rsplit("/", 1)[-1])) # gets the tweet ID as a int from the passed url
|
twid = int(re.sub(r'\?.*$','',video_link.rsplit("/", 1)[-1])) # gets the tweet ID as a int from the passed url
|
||||||
tweet = twitter_api.statuses.show(_id=twid, tweet_mode="extended")
|
tweet = twitter_api.statuses.show(_id=twid, tweet_mode="extended")
|
||||||
|
|
||||||
|
@ -172,11 +182,11 @@ def link_to_vnf_from_api(video_link):
|
||||||
else:
|
else:
|
||||||
url = re.findall(r'(https?://[^\s]+)', tweet['full_text'])[0]
|
url = re.findall(r'(https?://[^\s]+)', tweet['full_text'])[0]
|
||||||
thumb = "Non video link with url"
|
thumb = "Non video link with url"
|
||||||
print("Non video tweet, but has a link: " + url)
|
print(" [ NV ] Non video tweet, but has a link: " + url)
|
||||||
else:
|
else:
|
||||||
url = re.findall(r'(https?://[^\s]+)', tweet['full_text'])[0]
|
url = re.findall(r'(https?://[^\s]+)', tweet['full_text'])[0]
|
||||||
thumb = "Non video link with url"
|
thumb = "Non video link with url"
|
||||||
print("Non video tweet, but has a link: " + url)
|
print(" [ NV ] Non video tweet, but has a link: " + url)
|
||||||
|
|
||||||
if len(tweet['full_text']) > 200:
|
if len(tweet['full_text']) > 200:
|
||||||
text = textwrap.shorten(tweet['full_text'], width=200, placeholder="...")
|
text = textwrap.shorten(tweet['full_text'], width=200, placeholder="...")
|
||||||
|
@ -205,14 +215,14 @@ def link_to_vnf(video_link): # Return a VideoInfo object or die trying
|
||||||
try:
|
try:
|
||||||
return link_to_vnf_from_api(video_link)
|
return link_to_vnf_from_api(video_link)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("API Failed")
|
print(" [ X ] API Failed")
|
||||||
print(e)
|
print(e)
|
||||||
return None
|
return None
|
||||||
elif config['config']['method'] == 'youtube-dl':
|
elif config['config']['method'] == 'youtube-dl':
|
||||||
try:
|
try:
|
||||||
return link_to_vnf_from_youtubedl(video_link)
|
return link_to_vnf_from_youtubedl(video_link)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Youtube-DL Failed")
|
print(" [ X ] Youtube-DL Failed")
|
||||||
print(e)
|
print(e)
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
|
@ -223,11 +233,11 @@ def get_vnf_from_link_cache(video_link):
|
||||||
if link_cache_system == "db":
|
if link_cache_system == "db":
|
||||||
collection = db.linkCache
|
collection = db.linkCache
|
||||||
vnf = collection.find_one({'tweet': video_link})
|
vnf = collection.find_one({'tweet': video_link})
|
||||||
if vnf != None:
|
if vnf != None:
|
||||||
print("Link located in DB cache")
|
print(" [ ✔ ] Link located in DB cache")
|
||||||
return vnf
|
return vnf
|
||||||
else:
|
else:
|
||||||
print("Link not in DB cache")
|
print(" [ X ] Link not in DB cache")
|
||||||
return None
|
return None
|
||||||
elif link_cache_system == "json":
|
elif link_cache_system == "json":
|
||||||
if video_link in link_cache:
|
if video_link in link_cache:
|
||||||
|
@ -235,21 +245,21 @@ def get_vnf_from_link_cache(video_link):
|
||||||
vnf = link_cache[video_link]
|
vnf = link_cache[video_link]
|
||||||
return vnf
|
return vnf
|
||||||
else:
|
else:
|
||||||
print("Link not in json cache")
|
print(" [ X ] Link not in json cache")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def add_vnf_to_link_cache(video_link, vnf):
|
def add_vnf_to_link_cache(video_link, vnf):
|
||||||
if link_cache_system == "db":
|
if link_cache_system == "db":
|
||||||
try:
|
try:
|
||||||
out = db.linkCache.insert_one(vnf)
|
out = db.linkCache.insert_one(vnf)
|
||||||
print("Link added to DB cache")
|
print(" [ + ] Link added to DB cache ")
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
print("Failed to add link to DB cache")
|
print(" [ X ] Failed to add link to DB cache")
|
||||||
return None
|
return None
|
||||||
elif link_cache_system == "json":
|
elif link_cache_system == "json":
|
||||||
link_cache[video_link] = vnf
|
link_cache[video_link] = vnf
|
||||||
with open("links.json", "w") as outfile:
|
with open("links.json", "w") as outfile:
|
||||||
json.dump(link_cache, outfile, indent=4, sort_keys=True)
|
json.dump(link_cache, outfile, indent=4, sort_keys=True)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -257,13 +267,13 @@ def message(text):
|
||||||
return render_template('default.html', message=text, color=config['config']['color'], appname=config['config']['appname'], repo=config['config']['repo'], url=config['config']['url'])
|
return render_template('default.html', message=text, color=config['config']['color'], appname=config['config']['appname'], repo=config['config']['repo'], url=config['config']['url'])
|
||||||
|
|
||||||
def embed(video_link, vnf):
|
def embed(video_link, vnf):
|
||||||
print(vnf['url'])
|
print(" [ E ] Embedding " + vnf['url'])
|
||||||
if vnf['url'].startswith('https://t.co') is not True:
|
if vnf['url'].startswith('https://t.co') is not True:
|
||||||
desc = re.sub(r' http.*t\.co\S+', '', vnf['description'])
|
desc = re.sub(r' http.*t\.co\S+', '', vnf['description'])
|
||||||
urlUser = urllib.parse.quote(vnf['uploader'])
|
urlUser = urllib.parse.quote(vnf['uploader'])
|
||||||
urlDesc = urllib.parse.quote(desc)
|
urlDesc = urllib.parse.quote(desc)
|
||||||
urlLink = urllib.parse.quote(video_link)
|
urlLink = urllib.parse.quote(video_link)
|
||||||
return render_template('index.html', vidurl=vnf['url'], desc=desc, pic=vnf['thumbnail'], user=vnf['uploader'], video_link=video_link, color=config['config']['color'], appname=config['config']['appname'], repo=config['config']['repo'], url=config['config']['url'], urlDesc=urlDesc, urlUser=urlUser, urlLink=urlLink)
|
return render_template('index.html', vidlink=vnf['url'], vidurl=vnf['url'], desc=desc, pic=vnf['thumbnail'], user=vnf['uploader'], video_link=video_link, color=config['config']['color'], appname=config['config']['appname'], repo=config['config']['repo'], url=config['config']['url'], urlDesc=urlDesc, urlUser=urlUser, urlLink=urlLink)
|
||||||
else:
|
else:
|
||||||
return redirect(vnf['url'], 301)
|
return redirect(vnf['url'], 301)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue