Added automatic link redirecting for t.co links in non video tweets when using the API

This commit is contained in:
robinuniverse 2021-07-14 19:15:06 -05:00
parent f3f62f3cb4
commit 5567498a20
3 changed files with 27 additions and 14 deletions

View file

@ -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"
}

View file

@ -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/<video-url>` endpoint will return a json that contains all vide
Using `/other/<video-url>` 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

View file

@ -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 = {