Compare commits
No commits in common. "cddbbcc86682456ab72462c7cae2f3c21df3ae6e" and "83690c45358850cc619c78c05f670cbc7905aa88" have entirely different histories.
cddbbcc866
...
83690c4535
30
server.py
30
server.py
@ -11,6 +11,15 @@ import logging
|
|||||||
import re
|
import re
|
||||||
from textvid import generate_video_from_text
|
from textvid import generate_video_from_text
|
||||||
|
|
||||||
|
def get_expire(url):
|
||||||
|
alt_expire = datetime.now() + timedelta(hours=5)
|
||||||
|
if not url: return alt_expire
|
||||||
|
q = parse_qs(urlparse(url).query)
|
||||||
|
expire = q.get('expire')
|
||||||
|
if not expire: return alt_expire
|
||||||
|
expire = datetime.fromtimestamp(int(expire[0])) #this seems to always be +6 hours
|
||||||
|
return min([expire, alt_expire])
|
||||||
|
|
||||||
ctx_cache = {}
|
ctx_cache = {}
|
||||||
ips_running_ytdl = []
|
ips_running_ytdl = []
|
||||||
|
|
||||||
@ -47,15 +56,7 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
|
|
||||||
path = unquote(self.path)
|
path = unquote(self.path)
|
||||||
match = re.match("\/(?:id\/|(?:https?:\/\/)?(?:(?:www\.)?youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/))([A-Za-z0-9_-]{11})", path)
|
match = re.match("\/(?:id\/|(?:https?:\/\/)?(?:(?:www\.)?youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/))([A-Za-z0-9_-]{11})", path)
|
||||||
if match:
|
query = match[1] if match else "ytsearch:" + path[1:]
|
||||||
if self.is_pc_vrchat():
|
|
||||||
self.send_response(302)
|
|
||||||
self.send_header("Location", "https://www.youtube.com/watch?v=" + match[1])
|
|
||||||
self.end_headers()
|
|
||||||
return
|
|
||||||
query = match[1]
|
|
||||||
else:
|
|
||||||
query = "ytsearch:" + path[1:]
|
|
||||||
|
|
||||||
ctx = ctx_cache.get(query)
|
ctx = ctx_cache.get(query)
|
||||||
|
|
||||||
@ -66,10 +67,7 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
ips_running_ytdl.append(client_ip)
|
ips_running_ytdl.append(client_ip)
|
||||||
ctx_cache[query] = ctx = {
|
ctx_cache[query] = ctx = {'event': Event()}
|
||||||
'event': Event(),
|
|
||||||
'expire': datetime.now() + timedelta(hours=5)
|
|
||||||
}
|
|
||||||
with YoutubeDL() as ydl:
|
with YoutubeDL() as ydl:
|
||||||
info = ydl.extract_info(query, download=False)
|
info = ydl.extract_info(query, download=False)
|
||||||
|
|
||||||
@ -88,11 +86,7 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
best_format = max(suitable_formats, key=lambda x: x['height'])
|
best_format = max(suitable_formats, key=lambda x: x['height'])
|
||||||
|
|
||||||
ctx['url'] = best_format['url']
|
ctx['url'] = best_format['url']
|
||||||
|
ctx['expire'] = get_expire(best_format['url'])
|
||||||
expire = parse_qs(urlparse(best_format['url']).query).get('expire', [])[0]
|
|
||||||
if expire:
|
|
||||||
expire = datetime.fromtimestamp(int(expire))
|
|
||||||
if expire < ctx['expire']: ctx['expire'] = expire
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception(e)
|
logging.exception(e)
|
||||||
ctx['exception'] = e
|
ctx['exception'] = e
|
||||||
|
Loading…
Reference in New Issue
Block a user