mirror of
https://codeberg.org/ashley/poke.git
synced 2024-11-26 19:18:20 +01:00
apparently there are things to commit
This commit is contained in:
parent
ac1cf91adc
commit
174e0564a7
1 changed files with 83 additions and 85 deletions
|
@ -11,97 +11,95 @@ app = web.Application()
|
||||||
app.router._frozen = False
|
app.router._frozen = False
|
||||||
|
|
||||||
def get_random_string(length):
|
def get_random_string(length):
|
||||||
# choose from all lowercase letter
|
# choose from all lowercase letter
|
||||||
letters = string.ascii_lowercase
|
letters = string.ascii_lowercase
|
||||||
result_str = "".join(random.choice(letters) for i in range(length))
|
result_str = "".join(random.choice(letters) for i in range(length))
|
||||||
return result_str
|
return result_str
|
||||||
|
|
||||||
async def run_command(cmd):
|
async def run_command(cmd):
|
||||||
# Create subprocess
|
# Create subprocess
|
||||||
process = await asyncio.create_subprocess_shell(
|
process = await asyncio.create_subprocess_shell(
|
||||||
cmd,
|
cmd,
|
||||||
stdout=asyncio.subprocess.PIPE,
|
stdout=asyncio.subprocess.PIPE,
|
||||||
)
|
)
|
||||||
# Wait for the subprocess to finish
|
# Wait for the subprocess to finish
|
||||||
stdout, stderr = await process.communicate()
|
stdout, stderr = await process.communicate()
|
||||||
# Check for errors
|
# Check for errors
|
||||||
if process.returncode!= 0:
|
if process.returncode!= 0:
|
||||||
# Log or handle the error
|
# Log or handle the error
|
||||||
print(f"Command '{args}' failed with return code {process.returncode}")
|
print(f"Command '{args}' failed with return code {process.returncode}")
|
||||||
return None
|
return None
|
||||||
# Decode stdout and return
|
# Decode stdout and return
|
||||||
return stdout
|
return stdout
|
||||||
|
|
||||||
async def merge(request):
|
async def merge(request: aiohttp.web.Request):
|
||||||
# register params
|
# register params
|
||||||
job_id = request.rel_url.query["id"]
|
video_id: str = request.rel_url.query["id"]
|
||||||
video_id: str = request.rel_url.query["id"]
|
audio_itag: str = request.rel_url.query["audio_itag"]
|
||||||
audio_itag: str = request.rel_url.query["audio_itag"]
|
video_itag: str = request.rel_url.query["video_itag"]
|
||||||
video_itag: str = request.rel_url.query["video_itag"]
|
# validate
|
||||||
# validate
|
if " " in video_id or len(video_id) > 11:
|
||||||
if " " in video_id or len(video_id) > 11:
|
print(f"Video {video_id} flagged as invalid, dropping request")
|
||||||
print(f"Video {video_id} flagged as invalid, dropping request")
|
return
|
||||||
return
|
if not audio_itag.isdigit():
|
||||||
if not audio_itag.isdigit():
|
print(f"Audio itag {audio_itag} flagged as invalid, dropping request")
|
||||||
print(f"Audio itag {audio_itag} flagged as invalid, dropping request")
|
return
|
||||||
return
|
if not video_itag.isdigit():
|
||||||
if not video_itag.isdigit():
|
print(f"Video itag {video_itag} flagged as invalid, dropping request")
|
||||||
print(f"Video itag {video_itag} flagged as invalid, dropping request")
|
return
|
||||||
return
|
if "Firefox" in request.headers["User-Agent"]:
|
||||||
if os.path.isfile(f"{job_id}.mp4"):
|
# Sane browser that supports streaming
|
||||||
return web.FileResponse(
|
|
||||||
path=f"{job_id}.mp4"
|
cmdline = f"ffmpeg -i \"https://eu-proxy.poketube.fun/latest_version?id={video_id}&itag={audio_itag}&local=true\" -i \"https://eu-proxy.poketube.fun/latest_version?id={video_id}&itag={video_itag}&local=true\" -c copy -f mp4 -movflags frag_keyframe+empty_moov -"
|
||||||
)
|
process = await asyncio.create_subprocess_shell(
|
||||||
cmdline = f"ffmpeg -i \"https://eu-proxy.poketube.fun/latest_version?id={video_id}&itag={audio_itag}&local=true\" -i \"https://eu-proxy.poketube.fun/latest_version?id={video_id}&itag={video_itag}&local=true\" -c copy -f mp4 -movflags frag_keyframe+empty_moov --duration 300 -"
|
cmdline,
|
||||||
#proc_ffmpeg = await asyncio.create_subprocess_shell(
|
stdout=asyncio.subprocess.PIPE,
|
||||||
# cmdline,
|
stderr=asyncio.subprocess.PIPE
|
||||||
# stdout=asyncio.subprocess.PIPE
|
)
|
||||||
#)
|
response = web.StreamResponse(status=206, reason='OK', headers={
|
||||||
#print(f"ffmpeg -i \"https://eu-proxy.poketube.fun/latest_version?id={video_id}&itag={audio_itag}&local=true\" -i \"https://eu-proxy.poketube.fun/latest_version?id={video_id}&itag={video_itag}&local=true\" -c copy -f mp4 -movflags frag_keyframe+empty_moov -")
|
'Content-Type': 'video/mp4',
|
||||||
#stdout, _ = await proc_ffmpeg.communicate()
|
'Transfer-Encoding': 'chunked',
|
||||||
#iwannakillmyself = await run_command(cmdline)
|
'Accept-Ranges': 'bytes'
|
||||||
process = await asyncio.create_subprocess_shell(
|
})
|
||||||
cmdline,
|
await response.prepare(request)
|
||||||
stdout=asyncio.subprocess.PIPE,
|
try:
|
||||||
)
|
while True:
|
||||||
# Wait for the subprocess to finish
|
chunk = await process.stdout.readline()
|
||||||
#stdout, stderr = await process.communicate()
|
if not chunk:
|
||||||
response = web.StreamResponse(status=200, reason='OK', headers={
|
break
|
||||||
'Content-Type': 'video/mp4',
|
await response.write(chunk)
|
||||||
'Transfer-Encoding': 'chunked',
|
except Exception as e:
|
||||||
})
|
print(f"Error streaming FFmpeg output: {e}")
|
||||||
await response.prepare(request)
|
finally:
|
||||||
try:
|
await response.write_eof()
|
||||||
while True:
|
else:
|
||||||
# Read data from stdout
|
# Likely to be chromium browser, so to avoid browser shitting itself we download file
|
||||||
chunk = await process.stdout.readline()
|
job_id = f"{request.rel_url.query["id"]}_{request.rel_url.query["audio_itag"]}_{request.rel_url.query["video_itag"]}"
|
||||||
if not chunk:
|
if os.path.isfile(f"{job_id}.mp4"):
|
||||||
break
|
return web.FileResponse(
|
||||||
# Write the chunk to the response
|
path=f"{job_id}.mp4"
|
||||||
await response.write(chunk)
|
)
|
||||||
except Exception as e:
|
cmdline = f"ffmpeg -i \"https://eu-proxy.poketube.fun/latest_version?id={video_id}&itag={audio_itag}&local=true\" -i \"https://eu-proxy.poketube.fun/latest_version?id={video_id}&itag={video_itag}&local=true\" -c copy -f mp4 -movflags frag_keyframe+empty_moov {job_id}.mp4"
|
||||||
print(f"Error streaming FFmpeg output: {e}")
|
process = await asyncio.create_subprocess_shell(
|
||||||
finally:
|
cmdline,
|
||||||
# Close the response
|
stdout=asyncio.subprocess.PIPE,
|
||||||
await response.write_eof()
|
stderr=asyncio.subprocess.PIPE
|
||||||
return response
|
)
|
||||||
# Check for errors
|
await process.wait()
|
||||||
#if process.returncode != 0: # Log or handle the error
|
if process.returncode != 0: # Log or handle the error
|
||||||
#print(f"Command '{args}' failed with return code {process.returncode}")
|
return None
|
||||||
#return None
|
response = FileResponse(path=f"{job_id}.mp4")
|
||||||
# Decode stdout and return return stdout
|
return response
|
||||||
#response = Response(body=stdout, content_type="video/mp4", headers="")
|
|
||||||
#return response
|
|
||||||
|
|
||||||
async def ping(request):
|
async def ping(request):
|
||||||
return web.Response(body='{"success": true}', content_type="application/json")
|
return web.Response(body='{"success": true}', content_type="application/json")
|
||||||
|
|
||||||
async def init_app():
|
async def init_app():
|
||||||
app.router.add_get("/{id:.+}", merge)
|
app.router.add_get("/{id:.+}", merge)
|
||||||
app.router.add_get("/", ping)
|
app.router.add_get("/", ping)
|
||||||
return app
|
return app
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
app = loop.run_until_complete(init_app())
|
app = loop.run_until_complete(init_app())
|
||||||
web.run_app(app, port=3030)
|
web.run_app(app, port=3030)
|
||||||
|
|
Loading…
Reference in a new issue