46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
import os
|
|
import json
|
|
import shlex
|
|
import asyncio
|
|
from io import BytesIO
|
|
from youtube_dl.extractor import youtube
|
|
from . import config, client, seen_videos
|
|
|
|
youtube._try_get = _try_get = youtube.try_get
|
|
def traverse_dict(src):
|
|
for (key, value) in src.items():
|
|
if key == 'scheduledStartTime':
|
|
return value
|
|
if isinstance(value, dict):
|
|
if value := traverse_dict(value):
|
|
return value
|
|
return None
|
|
|
|
def try_get(src, getter, expected_type=None):
|
|
if reason := src.get('reason'):
|
|
if isinstance(reason, str) and (reason.startswith('This live event will begin in ') or reason.startswith('Premieres in ')):
|
|
if t := _try_get(src, traverse_dict, str):
|
|
src['reason'] = f'autoytarchive:{t} {reason}'
|
|
return _try_get(src, getter, expected_type)
|
|
youtube.try_get = try_get
|
|
|
|
async def split_files(filename, destination_dir):
|
|
args = [
|
|
'split',
|
|
'--verbose',
|
|
'--numeric-suffixes=0',
|
|
'--bytes=2097152000',
|
|
'--suffix-length=2',
|
|
filename,
|
|
os.path.join(destination_dir, os.path.basename(filename)) + '.part'
|
|
]
|
|
proc = await asyncio.create_subprocess_exec(*args, stdout=asyncio.subprocess.PIPE)
|
|
stdout, _ = await proc.communicate()
|
|
return shlex.split(' '.join([i[14:] for i in stdout.decode().strip().split('\n')]))
|
|
|
|
async def update_seen_videos():
|
|
with BytesIO(json.dumps(seen_videos).encode()) as file:
|
|
file.name = 'autoytarchive.json'
|
|
file.seek(0)
|
|
await client.edit_message(config['config']['storage_chat_id'], config['config']['storage_message_id'], file=file)
|