Changed variable and function naming conventions to match that of pythons spec
This commit is contained in:
parent
76ed67b42b
commit
c3c2dbb008
1 changed files with 42 additions and 39 deletions
75
twitfix.py
75
twitfix.py
|
@ -11,11 +11,14 @@ app = Flask(__name__)
|
||||||
pathregex = re.compile("\\w{1,15}\\/status\\/\\d{19}")
|
pathregex = re.compile("\\w{1,15}\\/status\\/\\d{19}")
|
||||||
discord_user_agents = ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0", "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)"]
|
discord_user_agents = ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0", "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)"]
|
||||||
|
|
||||||
|
# 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":{"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"},"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]"}}
|
||||||
json.dump(default, outfile, indent=4, sort_keys=True)
|
json.dump(default_config, outfile, indent=4, sort_keys=True)
|
||||||
|
|
||||||
|
config = default_config
|
||||||
|
else:
|
||||||
f = open("config.json")
|
f = open("config.json")
|
||||||
config = json.load(f)
|
config = json.load(f)
|
||||||
f.close()
|
f.close()
|
||||||
|
@ -30,8 +33,8 @@ if link_cache_system == "json":
|
||||||
link_cache = {}
|
link_cache = {}
|
||||||
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:
|
||||||
deflinkcache = {"test":"test"}
|
default_link_cache = {"test":"test"}
|
||||||
json.dump(deflinkcache, outfile, indent=4, sort_keys=True)
|
json.dump(default_link_cache, outfile, indent=4, sort_keys=True)
|
||||||
|
|
||||||
f = open('links.json',)
|
f = open('links.json',)
|
||||||
link_cache = json.load(f)
|
link_cache = json.load(f)
|
||||||
|
@ -49,45 +52,45 @@ def oembedend():
|
||||||
desc = request.args.get("desc", None)
|
desc = request.args.get("desc", None)
|
||||||
user = request.args.get("user", None)
|
user = request.args.get("user", None)
|
||||||
link = request.args.get("link", None)
|
link = request.args.get("link", None)
|
||||||
return oEmbedGen(desc,user,link)
|
return o_embed_gen(desc,user,link)
|
||||||
|
|
||||||
@app.route('/<path:subpath>')
|
@app.route('/<path:subpath>')
|
||||||
def twitfix(subpath):
|
def twitfix(sub_path):
|
||||||
user_agent = request.headers.get('user-agent')
|
user_agent = request.headers.get('user-agent')
|
||||||
match = pathregex.search(subpath)
|
match = pathregex.search(sub_path)
|
||||||
if match is not None:
|
if match is not None:
|
||||||
twitter_url = subpath
|
twitter_url = sub_path
|
||||||
|
|
||||||
if match.start() == 0:
|
if match.start() == 0:
|
||||||
twitter_url = "https://twitter.com/" + subpath
|
twitter_url = "https://twitter.com/" + sub_path
|
||||||
|
|
||||||
if user_agent in discord_user_agents:
|
if user_agent in discord_user_agents:
|
||||||
res = embedVideo(twitter_url)
|
res = embed_video(twitter_url)
|
||||||
return res
|
return res
|
||||||
else:
|
else:
|
||||||
return redirect(twitter_url, 301)
|
return redirect(twitter_url, 301)
|
||||||
else:
|
else:
|
||||||
return redirect("https://twitter.com/" + subpath, 301)
|
return redirect("https://twitter.com/" + 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:subpath>') # Show all info that Youtube-DL can get about a video as a json
|
||||||
def other(subpath):
|
def other(sub_path):
|
||||||
res = embedVideo(subpath)
|
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:subpath>') # Show all info that Youtube-DL can get about a video as a json
|
||||||
def info(subpath):
|
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(subpath, download=False)
|
result = ydl.extract_info(sub_path, download=False)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def embedVideo(vidlink):
|
def embed_video(vidlink):
|
||||||
cached_vnf = getVNFfromLinkCache(vidlink)
|
cached_vnf = get_vnf_from_link_cache(vidlink)
|
||||||
|
|
||||||
if cached_vnf == None:
|
if cached_vnf == None:
|
||||||
try:
|
try:
|
||||||
vnf = linkToVNF(vidlink)
|
vnf = link_to_vnf(vidlink)
|
||||||
addVNFtoLinkCache(vidlink, vnf)
|
add_vnf_to_link_cache(vidlink, vnf)
|
||||||
return embed(vidlink, vnf)
|
return embed(vidlink, vnf)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
@ -95,7 +98,7 @@ def embedVideo(vidlink):
|
||||||
else:
|
else:
|
||||||
return embed(vidlink, cached_vnf)
|
return embed(vidlink, cached_vnf)
|
||||||
|
|
||||||
def vidInfo(url, tweet="", desc="", thumb="", uploader=""): # Return a dict of video info with default values
|
def video_info(url, tweet="", desc="", thumb="", uploader=""): # Return a dict of video info with default values
|
||||||
vnf = {
|
vnf = {
|
||||||
"tweet" :tweet,
|
"tweet" :tweet,
|
||||||
"url" :url,
|
"url" :url,
|
||||||
|
@ -105,7 +108,7 @@ def vidInfo(url, tweet="", desc="", thumb="", uploader=""): # Return a dict of v
|
||||||
}
|
}
|
||||||
return vnf
|
return vnf
|
||||||
|
|
||||||
def linkToVNFfromAPI(vidlink):
|
def link_to_vnf_from_api(vidlink):
|
||||||
print("Attempting to download tweet info from Twitter API")
|
print("Attempting to download tweet info from Twitter API")
|
||||||
twid = int(re.sub(r'\?.*$','',vidlink.rsplit("/", 1)[-1])) # gets the tweet ID as a int from the passed url
|
twid = int(re.sub(r'\?.*$','',vidlink.rsplit("/", 1)[-1])) # gets the tweet ID as a int from the passed url
|
||||||
tweet = twitter_api.statuses.show(_id=twid, tweet_mode="extended")
|
tweet = twitter_api.statuses.show(_id=twid, tweet_mode="extended")
|
||||||
|
@ -122,34 +125,34 @@ def linkToVNFfromAPI(vidlink):
|
||||||
print(text)
|
print(text)
|
||||||
print(len(text))
|
print(len(text))
|
||||||
|
|
||||||
vnf = vidInfo(url, vidlink, text, tweet['extended_entities']['media'][0]['media_url'], tweet['user']['name'])
|
vnf = video_info(url, vidlink, text, tweet['extended_entities']['media'][0]['media_url'], tweet['user']['name'])
|
||||||
return vnf
|
return vnf
|
||||||
|
|
||||||
def linkToVNFfromYoutubeDL(vidlink):
|
def link_to_vnf_from_youtubedl(vidlink):
|
||||||
print("Attempting to download tweet info via YoutubeDL")
|
print("Attempting to download tweet info via YoutubeDL")
|
||||||
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(vidlink, download=False)
|
result = ydl.extract_info(vidlink, download=False)
|
||||||
vnf = vidInfo(result['url'], vidlink, result['description'].rsplit(' ',1)[0], result['thumbnail'], result['uploader'])
|
vnf = video_info(result['url'], vidlink, result['description'].rsplit(' ',1)[0], result['thumbnail'], result['uploader'])
|
||||||
return vnf
|
return vnf
|
||||||
|
|
||||||
def linkToVNF(vidlink): # Return a VideoInfo object or die trying
|
def link_to_vnf(vidlink): # Return a VideoInfo object or die trying
|
||||||
if config['config']['method'] == 'hybrid':
|
if config['config']['method'] == 'hybrid':
|
||||||
try:
|
try:
|
||||||
return linkToVNFfromAPI(vidlink)
|
return link_to_vnf_from_api(vidlink)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("API Failed")
|
print("API Failed")
|
||||||
print(e)
|
print(e)
|
||||||
return linkToVNFfromYoutubeDL(vidlink)
|
return link_to_vnf_from_youtubedl(vidlink)
|
||||||
elif config['config']['method'] == 'api':
|
elif config['config']['method'] == 'api':
|
||||||
try:
|
try:
|
||||||
return linkToVNFfromAPI(vidlink)
|
return link_to_vnf_from_api(vidlink)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("API Failed")
|
print("API Failed")
|
||||||
print(e)
|
print(e)
|
||||||
return None
|
return None
|
||||||
elif config['config']['method'] == 'youtube-dl':
|
elif config['config']['method'] == 'youtube-dl':
|
||||||
try:
|
try:
|
||||||
return linkToVNFfromYoutubeDL(vidlink)
|
return link_to_vnf_from_youtubedl(vidlink)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Youtube-DL Failed")
|
print("Youtube-DL Failed")
|
||||||
print(e)
|
print(e)
|
||||||
|
@ -159,10 +162,10 @@ def linkToVNF(vidlink): # Return a VideoInfo object or die trying
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def getVNFfromLinkCache(vidlink):
|
def get_vnf_from_link_cache(video_link):
|
||||||
if link_cache_system == "db":
|
if link_cache_system == "db":
|
||||||
collection = db.linkCache
|
collection = db.linkCache
|
||||||
vnf = collection.find_one({'tweet': vidlink})
|
vnf = collection.find_one({'tweet': video_link})
|
||||||
if vnf != None:
|
if vnf != None:
|
||||||
print("Link located in DB cache")
|
print("Link located in DB cache")
|
||||||
return vnf
|
return vnf
|
||||||
|
@ -170,15 +173,15 @@ def getVNFfromLinkCache(vidlink):
|
||||||
print("Link not in DB cache")
|
print("Link not in DB cache")
|
||||||
return None
|
return None
|
||||||
elif link_cache_system == "json":
|
elif link_cache_system == "json":
|
||||||
if vidlink in link_cache:
|
if video_link in link_cache:
|
||||||
print("Link located in json cache")
|
print("Link located in json cache")
|
||||||
vnf = link_cache[vidlink]
|
vnf = link_cache[video_link]
|
||||||
return vnf
|
return vnf
|
||||||
else:
|
else:
|
||||||
print("Link not in json cache")
|
print("Link not in json cache")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def addVNFtoLinkCache(vidlink, vnf):
|
def add_vnf_to_link_cache(video_link, vnf):
|
||||||
if link_cache_system == "db":
|
if link_cache_system == "db":
|
||||||
try:
|
try:
|
||||||
out = db.linkCache.insert_one(vnf)
|
out = db.linkCache.insert_one(vnf)
|
||||||
|
@ -188,7 +191,7 @@ def addVNFtoLinkCache(vidlink, vnf):
|
||||||
print("Failed to add link to DB cache")
|
print("Failed to add link to DB cache")
|
||||||
return None
|
return None
|
||||||
elif link_cache_system == "json":
|
elif link_cache_system == "json":
|
||||||
link_cache[vidlink] = vnf
|
link_cache[video_link] = vnf
|
||||||
with open("links.json", "w") as outfile:
|
with open("links.json", "w") as outfile:
|
||||||
json.dump(link_cache, outfile, indent=4, sort_keys=True)
|
json.dump(link_cache, outfile, indent=4, sort_keys=True)
|
||||||
return None
|
return None
|
||||||
|
@ -197,7 +200,7 @@ def embed(vidlink, vnf):
|
||||||
desc = re.sub(r' http.*t\.co\S+', '', vnf['description'].replace("#","#"))
|
desc = re.sub(r' http.*t\.co\S+', '', vnf['description'].replace("#","#"))
|
||||||
return render_template('index.html', vidurl=vnf['url'], desc=desc, pic=vnf['thumbnail'], user=vnf['uploader'], vidlink=vidlink)
|
return render_template('index.html', vidurl=vnf['url'], desc=desc, pic=vnf['thumbnail'], user=vnf['uploader'], vidlink=vidlink)
|
||||||
|
|
||||||
def oEmbedGen(description, user, vidlink):
|
def o_embed_gen(description, user, vidlink):
|
||||||
out = {
|
out = {
|
||||||
"type":"video",
|
"type":"video",
|
||||||
"version":"1.0",
|
"version":"1.0",
|
||||||
|
|
Loading…
Reference in a new issue