Updated the readme, started work on seperating things into functions to attempt to work with other sites

This commit is contained in:
Robin Universe 2021-07-05 11:02:17 -05:00
parent af263ce55f
commit 79faca77fe
2 changed files with 49 additions and 28 deletions

View file

@ -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 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/<twitter url>` `http://twtfx.me/<twitter url>`
**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) ## 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 ) 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** This project is licensed under the **Do What The Fuck You Want Public License**
## Other stuff
Using the `/info/<video-url>` endpoint will return a json that contains all video info that youtube-dl can grab about any given video
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

View file

@ -21,40 +21,52 @@ def twitfix(subpath):
if match.start() == 0: if match.start() == 0:
twitter_url = "https://twitter.com/" + subpath twitter_url = "https://twitter.com/" + subpath
if twitter_url in link_cache: res = embedVideo(twitter_url)
print("Link located in cache") return res
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)
else: else:
print("Link invalid") 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)")
return render_template('default.html', message="Please use a twitter link!")
@app.route('/info/<path:subpath>') @app.route('/other/<path:subpath>') # 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/<path:subpath>') # Show all info that Youtube-DL can get about a video as a json
def info(subpath): def info(subpath):
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(subpath, download=False) result = ydl.extract_info(subpath, download=False)
return result 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__": if __name__ == "__main__":
app.run(host='0.0.0.0', port=80) app.run(host='0.0.0.0', port=80)