Files
2025-09-24 22:40:21 +02:00

147 lines
5.3 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import sys
import asyncio
import traceback
import aiohttp
import aiofiles
import urllib.parse
from urllib.parse import urljoin, urlparse
from tqdm.asyncio import tqdm
def is_url(string):
"""Check if the provided string is a URL."""
try:
result = urlparse(string)
return all([result.scheme, result.netloc])
except:
return False
async def download_head(session, url):
"""Download a file from URL to the specified destination asynchronously.
If head is a number, download only the first head bytes.
"""
try:
print(f"Downloading: {url} (HEAD)")
headers = {}
headers['Range'] = f'bytes=0-100'
headers['NoPriority'] = f'NoPriority'
async with session.get(url, headers=headers) as response:
response.raise_for_status()
total_size = int(response.headers.get('content-length', 0))
downloaded = 0
async for chunk in response.content.iter_chunked(8192):
if chunk:
downloaded += len(chunk)
return True
except Exception as e:
print(f"Error downloading {url}: {str(e)}")
return False
async def download_file(session, url, destination):
"""Download a file from URL to the specified destination asynchronously."""
try:
if destination is not None:
os.makedirs(os.path.dirname(destination), exist_ok=True)
if os.path.exists(destination):
print(f"File already exists: {destination}")
return True
print(f"Downloading: {url} -> {destination}")
async with session.get(url) as response:
response.raise_for_status()
total_size = int(response.headers.get('content-length', 0))
print("Len ", total_size)
block_size = 8192
if destination is not None:
async with aiofiles.open(destination, 'wb') as f:
pbar = tqdm(total=total_size, unit='B', unit_scale=True, desc=os.path.basename(destination))
async for chunk in response.content.iter_chunked(block_size):
if chunk:
await f.write(chunk)
pbar.update(len(chunk))
pbar.close()
else:
pbar = tqdm(total=total_size, unit='B', unit_scale=True, desc="To None")
pbar.update(0)
async for chunk in response.content.iter_chunked(block_size):
if chunk:
pbar.update(len(chunk))
pbar.close()
return True
except Exception as e:
print(f"Error downloading {url}: {str(e)}")
return False
async def get_m3u_urls(m3u_url, session, visited=None):
"""Recursively collect all file URLs from an M3U playlist."""
if visited is None:
visited = set()
urls = []
print(f"Adding m3u {m3u_url}")
try:
async with session.get(m3u_url) as response:
response.raise_for_status()
content = await response.text()
if "#EXTM3U" not in content:
raise ValueError("The provided file is not a valid M3U playlist.")
base_url = m3u_url.rsplit('/', 1)[0] + '/'
for line in content.splitlines():
line = line.strip()
if not line or line.startswith('#'):
continue
file_url = line if is_url(line) else urljoin(base_url, line)
path_part = urlparse(file_url).path
if '.m3u' in path_part and file_url not in visited:
visited.add(file_url)
nested_urls = await get_m3u_urls(file_url, session=session, visited=visited)
urls.extend(nested_urls)
continue
urls.append(file_url)
return urls
except Exception as e:
print(f"Error processing M3U file: {str(e)}")
traceback.print_exc()
return []
async def process_m3u(m3u_url, output_dir, session=None):
"""Process an M3U file: get all URLs and download them."""
with aiohttp.ClientSession() as session:
urls = await get_m3u_urls(m3u_url, session=session)
result = []
if session is None:
session = aiohttp.ClientSession()
for file_url in urls:
try:
local_path = None
if output_dir is not None:
path_part = urlparse(file_url).path
if path_part.startswith('/'):
path_part = path_part[1:]
path_part = urllib.parse.unquote(path_part)
local_path = None if output_dir is None else os.path.join(output_dir, path_part)
await download_file(session, file_url, local_path)
except Exception as e:
print(f"Error downloading URLs: {str(e)}")
traceback.print_exc()
return False
return result
if __name__ == "__main__":
m3u = "http://192.168.1.96:8080/playlistall/all.m3u"
if len(sys.argv) < 2:
m3u_url = m3u
else:
m3u_url = sys.argv[1]
output_dir = sys.argv[2] if len(sys.argv) > 2 else None
print(f"Downloading content from {m3u_url} to {output_dir}")
asyncio.run(process_m3u(m3u_url, output_dir))