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
|
||||
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 = {}
|
||||
ips_running_ytdl = []
|
||||
|
||||
@ -47,15 +56,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
|
||||
path = unquote(self.path)
|
||||
match = re.match("\/(?:id\/|(?:https?:\/\/)?(?:(?:www\.)?youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/))([A-Za-z0-9_-]{11})", path)
|
||||
if match:
|
||||
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:]
|
||||
query = match[1] if match else "ytsearch:" + path[1:]
|
||||
|
||||
ctx = ctx_cache.get(query)
|
||||
|
||||
@ -66,10 +67,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
return
|
||||
try:
|
||||
ips_running_ytdl.append(client_ip)
|
||||
ctx_cache[query] = ctx = {
|
||||
'event': Event(),
|
||||
'expire': datetime.now() + timedelta(hours=5)
|
||||
}
|
||||
ctx_cache[query] = ctx = {'event': Event()}
|
||||
with YoutubeDL() as ydl:
|
||||
info = ydl.extract_info(query, download=False)
|
||||
|
||||
@ -88,11 +86,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
best_format = max(suitable_formats, key=lambda x: x['height'])
|
||||
|
||||
ctx['url'] = 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
|
||||
ctx['expire'] = get_expire(best_format['url'])
|
||||
except Exception as e:
|
||||
logging.exception(e)
|
||||
ctx['exception'] = e
|
||||
|
Loading…
Reference in New Issue
Block a user