diff --git a/readme.md b/readme.md index 4da7984..27e8101 100644 --- a/readme.md +++ b/readme.md @@ -8,9 +8,11 @@ This does work! but I'm new to flask, so it can probably be improved a great dea just put the url to the server, and directly after, the full URL to the tweet you want to embed -I have a version running on my computer, I don't promise it'll be up all the time, but for now, you can use it by using the url +**I now have a copy of this running on a Linode server, you can use it via the following url** `http://twtfx.me/` +**Note**: If you enjoy this service, please considering donating via [Ko-Fi](https://ko-fi.com/robin_universe) to help cover server costs + ## How to run (server side) this script uses the youtube-dl python module, along with flask, so install those with pip (you can use `pip install -r requirements.txt`) and start the server with `python twitfix.py` ( will need sudo if you leave it at port 80 ) @@ -19,3 +21,10 @@ By default I have the port set to 80, just cause that's what was convenient for This project is licensed under the **Do What The Fuck You Want Public License** + + +## Other stuff + +Using the `/info/` endpoint will return a json that contains all video info that youtube-dl can grab about any given video + +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 diff --git a/twitfix.py b/twitfix.py index 136074b..bccd79a 100644 --- a/twitfix.py +++ b/twitfix.py @@ -21,40 +21,52 @@ def twitfix(subpath): if match.start() == 0: twitter_url = "https://twitter.com/" + subpath - if twitter_url in link_cache: - print("Link located in cache") - return render_template('index.html', vidurl=link_cache[twitter_url]['url'], tweet=link_cache[twitter_url]['description'], pic=link_cache[twitter_url]['thumbnail'], user=link_cache[twitter_url]['uploader'], tweeturl=twitter_url) - else: - with youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'}) as ydl: - try: - print("Link not in cache, downloading and adding details to cache file") - result = ydl.extract_info(twitter_url, download=False) - - link_cache[twitter_url] = { - "url" :result['url'], - "description" :result['description'], - "thumbnail" :result['thumbnail'], - "uploader" :result['uploader'] - } - - with open("links.json", "w") as outfile: - json.dump(link_cache, outfile, indent=4, sort_keys=True) - - except Exception: # Just to keep from 500s that are messy - print(Exception) - return render_template('default.html', message="Failed to scan your twitter link! ( there is a chance I ran out of api calls.. wait a bit and try again )") - - return render_template('index.html', vidurl=result['url'], tweet=result['description'], pic=result['thumbnail'], user=result['uploader'], tweeturl=twitter_url) + res = embedVideo(twitter_url) + return res else: - print("Link invalid") - return render_template('default.html', message="Please use a twitter link!") + return render_template('default.html', message="This doesn't seem to be a twitter link, try /other/ to see if other kinds of video link works? (experimental)") -@app.route('/info/') +@app.route('/other/') # Show all info that Youtube-DL can get about a video as a json +def other(subpath): + res = embedVideo(subpath) + return res + +@app.route('/info/') # Show all info that Youtube-DL can get about a video as a json def info(subpath): with youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'}) as ydl: result = ydl.extract_info(subpath, download=False) return result +def embedVideo(vidlink): # Return a render template from a video url + if vidlink in link_cache: + print("Link located in cache") + return render_template('index.html', vidurl=link_cache[vidlink]['url'], desc=link_cache[vidlink]['description'], pic=link_cache[vidlink]['thumbnail'], user=link_cache[vidlink]['uploader'], vidlink=vidlink) + else: + with youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'}) as ydl: + try: + print("Link not in cache, downloading and adding details to cache file") + result = ydl.extract_info(vidlink, download=False) + vnf = vidInfo(result['url'], result['description'], result['thumbnail'], result['uploader']) + link_cache[vidlink] = vnf + + with open("links.json", "w") as outfile: + json.dump(link_cache, outfile, indent=4, sort_keys=True) + + except Exception: # Just to keep from 500s that are messy + print("Failed to download link") + return render_template('default.html', message="Failed to scan your link!") + + return render_template('index.html', vidurl=vnf['url'], desc=vnf['description'], pic=vnf['thumbnail'], user=vnf['uploader'], vidlink=vidlink) + +def vidInfo(url, desc="", thumb="", uploader=""): # Return a dict of video info with default values + vnf = { + "url" :url, + "description" :desc, + "thumbnail" :thumb, + "uploader" :uploader + } + return vnf + if __name__ == "__main__": app.run(host='0.0.0.0', port=80)