Compare commits
2 Commits
83690c4535
...
cddbbcc866
Author | SHA1 | Date | |
---|---|---|---|
cddbbcc866 | |||
a007796147 |
30
server.py
30
server.py
@ -11,15 +11,6 @@ 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 = []
|
||||
|
||||
@ -56,7 +47,15 @@ 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)
|
||||
query = match[1] if match else "ytsearch:" + path[1:]
|
||||
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:]
|
||||
|
||||
ctx = ctx_cache.get(query)
|
||||
|
||||
@ -67,7 +66,10 @@ class Handler(BaseHTTPRequestHandler):
|
||||
return
|
||||
try:
|
||||
ips_running_ytdl.append(client_ip)
|
||||
ctx_cache[query] = ctx = {'event': Event()}
|
||||
ctx_cache[query] = ctx = {
|
||||
'event': Event(),
|
||||
'expire': datetime.now() + timedelta(hours=5)
|
||||
}
|
||||
with YoutubeDL() as ydl:
|
||||
info = ydl.extract_info(query, download=False)
|
||||
|
||||
@ -86,7 +88,11 @@ class Handler(BaseHTTPRequestHandler):
|
||||
best_format = max(suitable_formats, key=lambda x: x['height'])
|
||||
|
||||
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:
|
||||
logging.exception(e)
|
||||
ctx['exception'] = e
|
||||
|
Loading…
Reference in New Issue
Block a user