From f3f62f3cb4b95a51f491774f3fb8a86dd20d2ea7 Mon Sep 17 00:00:00 2001 From: robinuniverse Date: Wed, 14 Jul 2021 03:33:46 -0500 Subject: [PATCH] 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 --- links.json | 7 +++++++ readme.md | 10 +++++++++- templates/default.html | 12 ++++++------ templates/index.html | 6 +++--- twitfix.py | 23 ++++++++++++++--------- 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/links.json b/links.json index c5e02bf..0ea4de4 100644 --- a/links.json +++ b/links.json @@ -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" } \ No newline at end of file diff --git a/readme.md b/readme.md index c2ac1b0..e9393dd 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # 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) @@ -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 - **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** diff --git a/templates/default.html b/templates/default.html index d10ed8f..4c86f4d 100644 --- a/templates/default.html +++ b/templates/default.html @@ -1,14 +1,14 @@ {% extends 'base.html' %} {% block head %} - TwitFix - + {{ appname }} + - - - + + + {% endblock %} {% block body %} - You will be redirected to this projects github page in a moment. Or click here. + You will be redirected to this projects github page in a moment. Or click here. {% endblock %} diff --git a/templates/index.html b/templates/index.html index 7f2fdd5..e0459fd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,8 +2,8 @@ {% block head %} - - + + @@ -27,7 +27,7 @@ - + {% endblock %} diff --git a/twitfix.py b/twitfix.py index 6fd98a1..5ce1139 100644 --- a/twitfix.py +++ b/twitfix.py @@ -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. if not os.path.exists("config.json"): 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) config = default_config @@ -44,9 +44,13 @@ elif link_cache_system == "db": client = pymongo.MongoClient(config['config']['database'], connect=False) db = client.TwitFix -@app.route('/') +@app.route('/') # If the useragent is discord, return the embed, if not, redirect to configured repo directly 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') def oembedend(): @@ -59,7 +63,6 @@ def oembedend(): def twitfix(sub_path): user_agent = request.headers.get('user-agent') match = pathregex.search(sub_path) - print(sub_path) if match is not None: twitter_url = sub_path @@ -73,15 +76,14 @@ def twitfix(sub_path): print("Redirect to " + twitter_url) return redirect(twitter_url, 301) else: - print(sub_path) return redirect(sub_path, 301) -@app.route('/other/') # Show all info that Youtube-DL can get about a video as a json +@app.route('/other/') # Show all info that Youtube-DL can get about a video as a json def other(sub_path): res = embed_video(sub_path) return res -@app.route('/info/') # Show all info that Youtube-DL can get about a video as a json +@app.route('/info/') # Show all info that Youtube-DL can get about a video as a json def info(sub_path): with youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'}) as ydl: result = ydl.extract_info(sub_path, download=False) @@ -98,7 +100,7 @@ def embed_video(video_link): return embed(video_link, vnf) except Exception as e: print(e) - return render_template('default.html', message="Failed to scan your link!") + return message("Failed to scan your link!") else: 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) 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): 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): out = {