From 5567498a20d26fcbe48d1dece94f352a921dff48 Mon Sep 17 00:00:00 2001 From: robinuniverse Date: Wed, 14 Jul 2021 19:15:06 -0500 Subject: [PATCH] Added automatic link redirecting for t.co links in non video tweets when using the API --- links.json | 7 ------- readme.md | 4 +++- twitfix.py | 30 ++++++++++++++++++++++++------ 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/links.json b/links.json index 0ea4de4..c5e02bf 100644 --- a/links.json +++ b/links.json @@ -1,10 +1,3 @@ { - "https://twitter.com/RobinUniverse_/status/1415076540833058816": { - "description": "Happy #PortfolioDay Everyone! \u2728\u2728\u2728\u2728\u2728\u2728\u2728\u2728\u2728\u2728\u2728 My name is Robin Universe and I do lots of stuff! Actually, I do so many random things that this is gonna be a thread, and I'll start off with #3dart ! I...", - "thumbnail": "http://pbs.twimg.com/ext_tw_video_thumb/1415073703860260870/pu/img/E2ucgGM_oPzhRrke.jpg", - "tweet": "https://twitter.com/RobinUniverse_/status/1415076540833058816", - "uploader": "Robin \u2728Universe \ud83c\udff3\ufe0f\u200d\u26a7\ufe0f", - "url": "https://video.twimg.com/ext_tw_video/1415073703860260870/pu/vid/1280x720/VV2t4o7UwsterxFO.mp4?tag=12" - }, "test": "test" } \ No newline at end of file diff --git a/readme.md b/readme.md index e9393dd..3b9e363 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # TwitFix -Basic flask server that serves fixed twitter video embeds to desktop discord by using either the Twitter API or Youtube-DL to grab tweet video information +Basic flask server that serves fixed twitter video embeds to desktop discord by using either the Twitter API or Youtube-DL to grab tweet video information. This also automatically embeds the first link in the text of non video tweets (API Only) ## How to use (discord side) @@ -63,3 +63,5 @@ Using the `/info/` endpoint will return a json that contains all vide Using `/other/` will attempt to run the twitter embed stuff on other websites videos - This is mostly experimental and doesn't really work for now Advanced embeds are provided via a `/oembed.json?` endpoint - This is manually pointing at my server in `/templates/index.html` and should be changed from `https://fxtwitter.com/` to whatever your domain is + +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 diff --git a/twitfix.py b/twitfix.py index 5ce1139..0514c4c 100644 --- a/twitfix.py +++ b/twitfix.py @@ -118,17 +118,31 @@ def link_to_vnf_from_api(video_link): 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 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'] + + # Check to see if tweet has a video, if not, make the url passed to the VNF the first t.co link in the tweet + if 'extended_entities' in tweet: + if 'video_info' in tweet['extended_entities']['media'][0]: + 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'] + thumb = tweet['extended_entities']['media'][0]['media_url'] + else: + url = tweet['extended_entities']['media'][0]['video_info']['variants'][-2]['url'] + thumb = tweet['extended_entities']['media'][0]['media_url'] + else: + url = re.findall(r'(https?://[^\s]+)', tweet['full_text'])[0] + thumb = "Non video link with url" + print("Non video tweet, but has a link: " + url) else: - url = tweet['extended_entities']['media'][0]['video_info']['variants'][-2]['url'] + url = re.findall(r'(https?://[^\s]+)', tweet['full_text'])[0] + thumb = "Non video link with url" + print("Non video tweet, but has a link: " + url) if len(tweet['full_text']) > 200: text = textwrap.shorten(tweet['full_text'], width=200, placeholder="...") else: text = tweet['full_text'] - vnf = video_info(url, video_link, text, tweet['extended_entities']['media'][0]['media_url'], tweet['user']['name']) + vnf = video_info(url, video_link, text, thumb, tweet['user']['name']) return vnf def link_to_vnf_from_youtubedl(video_link): @@ -202,8 +216,12 @@ 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']) def embed(video_link, vnf): - desc = re.sub(r' http.*t\.co\S+', '', vnf['description'].replace("#","#")) # some funky string manipulation to get rid of the t.co vid link and replace # with a similar looking character, the normal # breaks when getting fed into the oembed endpoint - 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']) + print(vnf['url']) + if vnf['url'].startswith('https://t.co') is not True: + desc = re.sub(r' http.*t\.co\S+', '', vnf['description'].replace("#","#")) # some funky string manipulation to get rid of the t.co vid link and replace # with a similar looking character, the normal # breaks when getting fed into the oembed endpoint + 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']) + else: + return redirect(vnf['url'], 301) def o_embed_gen(description, user, video_link): out = {