mirror of
https://github.com/ReviveMii/revivetube
synced 2025-04-29 04:29:25 -04:00
Update helper.py
This commit is contained in:
parent
647f6ed4d5
commit
9008fc0da0
1 changed files with 17 additions and 28 deletions
45
helper.py
45
helper.py
|
@ -6,46 +6,35 @@ import aiohttp
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
async def read_file(path):
|
async def read_file(path):
|
||||||
assert isinstance(path, str), "Path must be a string"
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with aiofiles.open(path, 'r', encoding='utf-8') as file:
|
async with aiofiles.open(path, 'r', encoding='utf-8') as file:
|
||||||
content = await file.read()
|
return await file.read()
|
||||||
return content
|
|
||||||
except FileNotFoundError:
|
|
||||||
return "Error: File not found."
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f"Error: {str(e)}"
|
print(f"Error reading file: {str(e)}")
|
||||||
|
return None
|
||||||
|
|
||||||
async def get_video_duration_from_file(video_path):
|
async def get_video_duration_from_file(video_path):
|
||||||
try:
|
try:
|
||||||
result = await asyncio.create_subprocess_exec(
|
proc = await asyncio.create_subprocess_exec(
|
||||||
'ffprobe', '-v', 'error', '-show_format', '-show_streams', '-of', 'json', video_path,
|
'ffprobe', '-v', 'error', '-show_format',
|
||||||
|
'-show_streams', '-of', 'json', video_path,
|
||||||
stdout=asyncio.subprocess.PIPE,
|
stdout=asyncio.subprocess.PIPE,
|
||||||
stderr=asyncio.subprocess.PIPE
|
stderr=asyncio.subprocess.PIPE
|
||||||
)
|
)
|
||||||
|
stdout, stderr = await proc.communicate()
|
||||||
stdout, stderr = await result.communicate()
|
if proc.returncode != 0:
|
||||||
|
|
||||||
if result.returncode != 0:
|
|
||||||
raise Exception(stderr.decode())
|
raise Exception(stderr.decode())
|
||||||
|
return float(json.loads(stdout.decode())['format']['duration'])
|
||||||
video_info = json.loads(stdout.decode())
|
|
||||||
duration = float(video_info['format']['duration'])
|
|
||||||
return duration
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Can't fetch Video-Duration: {str(e)}")
|
print(f"Error getting video duration: {str(e)}")
|
||||||
return 0
|
return 0.0
|
||||||
|
|
||||||
async def format_duration(seconds):
|
async def format_duration(seconds):
|
||||||
minutes = seconds // 60
|
return f"{int(seconds//60)}:{int(seconds%60):02d}"
|
||||||
seconds = seconds % 60
|
|
||||||
return f"{minutes}:{str(seconds).zfill(2)}"
|
|
||||||
|
|
||||||
async def get_file_size(file_path):
|
async def get_file_size(file_path):
|
||||||
try:
|
try:
|
||||||
stat = await aiofiles.os.stat(file_path)
|
return os.path.getsize(file_path)
|
||||||
return stat.st_size
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error getting file size: {str(e)}")
|
print(f"Error getting file size: {str(e)}")
|
||||||
return 0
|
return 0
|
||||||
|
@ -54,8 +43,7 @@ async def get_range(file_path, byte_range):
|
||||||
try:
|
try:
|
||||||
async with aiofiles.open(file_path, 'rb') as f:
|
async with aiofiles.open(file_path, 'rb') as f:
|
||||||
await f.seek(byte_range[0])
|
await f.seek(byte_range[0])
|
||||||
data = await f.read(byte_range[1] - byte_range[0] + 1)
|
return await f.read(byte_range[1] - byte_range[0] + 1)
|
||||||
return data
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error reading file range: {str(e)}")
|
print(f"Error reading file range: {str(e)}")
|
||||||
return b''
|
return b''
|
||||||
|
@ -64,5 +52,6 @@ async def get_api_key():
|
||||||
try:
|
try:
|
||||||
async with aiofiles.open("token.txt", "r") as f:
|
async with aiofiles.open("token.txt", "r") as f:
|
||||||
return (await f.read()).strip()
|
return (await f.read()).strip()
|
||||||
except FileNotFoundError:
|
except Exception as e:
|
||||||
raise FileNotFoundError("Missing token.txt. Please go to README.md")
|
print(f"Error reading API key: {str(e)}")
|
||||||
|
raise
|
||||||
|
|
Loading…
Add table
Reference in a new issue