Added more config options for color, name, and repo, made clicking on the bare url directly redirect to the repo based on useragent, fixed one config that was being created with the wrong name, finally added a url config so oembed works on other servers without digging around in the html

This commit is contained in:
robinuniverse 2021-07-14 03:33:46 -05:00
parent e276425b74
commit f3f62f3cb4
5 changed files with 39 additions and 19 deletions

View file

@ -1,3 +1,10 @@
{ {
"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" "test": "test"
} }

View file

@ -1,6 +1,6 @@
# TwitFix # TwitFix
Flask server that serves fixed twitter video embeds to 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
## How to use (discord side) ## How to use (discord side)
@ -44,6 +44,14 @@ TwitFix generates a config.json in its root directory the first time you run it,
- **api**: this directly uses the twitter API to grab tweet info, limited to 900 calls per 15m - **api**: this directly uses the twitter API to grab tweet info, limited to 900 calls per 15m
- **hybrid**: This will start off by using the twitter API to grab tweet info, but if the rate limit is reached or the api fails for any other reason it will switch over to youtube-dl to avoid downtime - **hybrid**: This will start off by using the twitter API to grab tweet info, but if the rate limit is reached or the api fails for any other reason it will switch over to youtube-dl to avoid downtime
**color** - Accepts a hex formatted color code, can change the embed color
**appname** - Can change the app name easily wherever it's shown
**repo** - used to change the repo url that some links redirect to
**url** - used to tell the user where to look for the oembed endpoint, make sure to set this to your public facing url
This project is licensed under the **Do What The Fuck You Want Public License** This project is licensed under the **Do What The Fuck You Want Public License**

View file

@ -1,14 +1,14 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% block head %} {% block head %}
<title>TwitFix</title> <title>{{ appname }}</title>
<meta content="TwitFix" property="og:title" /> <meta content="{{ appname }}" property="og:title" />
<meta content="{{ message }}" property="og:description" /> <meta content="{{ message }}" property="og:description" />
<meta content="https://github.com/robinuniverse/TwitFix" property="og:url" /> <meta content="{{ repo }}" property="og:url" />
<meta content="#43B581" data-react-helmet="true" name="theme-color" /> <meta content="{{ color }}" data-react-helmet="true" name="theme-color" />
<meta http-equiv = "refresh" content = "0; url = https://github.com/robinuniverse/TwitFix" /> <meta http-equiv = "refresh" content = "0; url = {{ repo }}" />
{% endblock %} {% endblock %}
{% block body %} {% block body %}
You will be redirected to this projects github page in a moment. <a href="https://github.com/robinuniverse/TwitFix">Or click here.</a> You will be redirected to this projects github page in a moment. <a href="{{ repo }}">Or click here.</a>
{% endblock %} {% endblock %}

View file

@ -2,8 +2,8 @@
{% block head %} {% block head %}
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' /> <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
<meta content="#43B581" data-react-helmet="true" name="theme-color" /> <meta content="{{ color }}" data-react-helmet="true" name="theme-color" />
<meta property="og:site_name" content="TwitFix"> <meta property="og:site_name" content="{{ appname }}">
<meta name="twitter:card" content="player" /> <meta name="twitter:card" content="player" />
<meta name="twitter:title" content="{{ user }}" /> <meta name="twitter:title" content="{{ user }}" />
@ -27,7 +27,7 @@
<meta property="og:image" content="{{ pic }}" /> <meta property="og:image" content="{{ pic }}" />
<!--!--> <!--!-->
<link rel="alternate" href="https://fxtwitter.com/oembed.json?desc={{ user }}&user={{ desc }}&link={{ link }}" type="application/json+oembed" title="{{ user }}"> <link rel="alternate" href="{{ url }}/oembed.json?desc={{ user }}&user={{ desc }}&link={{ link }}" type="application/json+oembed" title="{{ user }}">
<meta http-equiv = "refresh" content = "0; url = {{ vidlink }}" /> <meta http-equiv = "refresh" content = "0; url = {{ vidlink }}" />
{% endblock %} {% endblock %}

View file

@ -14,7 +14,7 @@ discord_user_agents = ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) G
# 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"):
with open("config.json", "w") as outfile: with open("config.json", "w") as outfile:
default_config = {"config":{"link_cache":"json","database":"[url to mongo database goes here]","method":"youtube-dl"},"api":{"api_key":"[api_key goes here]","consumer_secret":"[api_secret goes here]","access_token":"[access_token goes here]","access_secret":"[access_secret goes here]"}} default_config = {"config":{"link_cache":"json","database":"[url to mongo database goes here]","method":"youtube-dl", "color":"#43B581", "appname": "TwitFix", "repo": "https://github.com/robinuniverse/twitfix", "url": "https://fxtwitter.com"},"api":{"api_key":"[api_key goes here]","api_secret":"[api_secret goes here]","access_token":"[access_token goes here]","access_secret":"[access_secret goes here]"}}
json.dump(default_config, outfile, indent=4, sort_keys=True) json.dump(default_config, outfile, indent=4, sort_keys=True)
config = default_config config = default_config
@ -44,9 +44,13 @@ 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('/') @app.route('/') # If the useragent is discord, return the embed, if not, redirect to configured repo directly
def default(): def default():
return render_template('default.html', message="TwitFix is an attempt to fix twitter video embeds in discord! created by Robin Universe :) 💖 ") user_agent = request.headers.get('user-agent')
if user_agent in discord_user_agents:
return message("TwitFix is an attempt to fix twitter video embeds in discord! created by Robin Universe :)\n\n💖\n\nClick me to be redirected to the repo!")
else:
return redirect(config['config']['repo'], 301)
@app.route('/oembed.json') @app.route('/oembed.json')
def oembedend(): def oembedend():
@ -59,7 +63,6 @@ def oembedend():
def twitfix(sub_path): def twitfix(sub_path):
user_agent = request.headers.get('user-agent') user_agent = request.headers.get('user-agent')
match = pathregex.search(sub_path) match = pathregex.search(sub_path)
print(sub_path)
if match is not None: if match is not None:
twitter_url = sub_path twitter_url = sub_path
@ -73,15 +76,14 @@ def twitfix(sub_path):
print("Redirect to " + twitter_url) print("Redirect to " + twitter_url)
return redirect(twitter_url, 301) return redirect(twitter_url, 301)
else: else:
print(sub_path)
return redirect(sub_path, 301) return redirect(sub_path, 301)
@app.route('/other/<path:subpath>') # Show all info that Youtube-DL can get about a video as a json @app.route('/other/<path:sub_path>') # Show all info that Youtube-DL can get about a video as a json
def other(sub_path): def other(sub_path):
res = embed_video(sub_path) res = embed_video(sub_path)
return res return res
@app.route('/info/<path:subpath>') # Show all info that Youtube-DL can get about a video as a json @app.route('/info/<path:sub_path>') # Show all info that Youtube-DL can get about a video as a json
def info(sub_path): def info(sub_path):
with youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'}) as ydl: with youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'}) as ydl:
result = ydl.extract_info(sub_path, download=False) result = ydl.extract_info(sub_path, download=False)
@ -98,7 +100,7 @@ def embed_video(video_link):
return embed(video_link, vnf) return embed(video_link, vnf)
except Exception as e: except Exception as e:
print(e) print(e)
return render_template('default.html', message="Failed to scan your link!") return message("Failed to scan your link!")
else: else:
return embed(video_link, cached_vnf) return embed(video_link, cached_vnf)
@ -196,9 +198,12 @@ def add_vnf_to_link_cache(video_link, vnf):
json.dump(link_cache, outfile, indent=4, sort_keys=True) json.dump(link_cache, outfile, indent=4, sort_keys=True)
return None return None
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): 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 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) 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'])
def o_embed_gen(description, user, video_link): def o_embed_gen(description, user, video_link):
out = { out = {