-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper_selector.py
54 lines (47 loc) · 2.17 KB
/
scraper_selector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import logging
import pytube
import streamlink
import subprocess
import m3u8
import re
def sanitize_filename(name):
return re.sub(r'[^a-zA-Z0-9_\-]', '_', name)
logger = logging.getLogger('scraper_selector')
def get_stream(url, disable_pytube=False, disable_streamlink=False, disable_ytdlp=False, disable_m3u8=False):
if not disable_pytube:
try:
yt = pytube.YouTube(url)
stream = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
logger.info(f"Pytube: Obtuvo el stream {stream.resolution} para {url}")
return stream.url, yt.title
except Exception as e:
logger.error(f"Pytube falló, intentando con Streamlink: {str(e)}")
if not disable_streamlink:
try:
streams = streamlink.streams(url)
if "best" in streams:
stream_url = streams["best"].url
logger.info(f"Streamlink: Obtuvo el stream 'best' para {url}")
return stream_url, "Streamlink_Video"
except Exception as e:
logger.error(f"Streamlink falló, intentando con yt-dlp: {str(e)}")
if not disable_ytdlp:
try:
result = subprocess.run(['yt-dlp', '-f', 'best', '-g', url], stdout=subprocess.PIPE, text=True)
stream_url = result.stdout.strip()
if stream_url:
logger.info(f"yt-dlp: Obtuvo el stream para {url}")
return stream_url, "YT-DLP_Video"
except Exception as e:
logger.error(f"yt-dlp falló al obtener el stream: {str(e)}")
if not disable_m3u8:
try:
playlist = m3u8.load(url)
if playlist.segments:
stream_url = playlist.segments[0].uri
logger.info(f"m3u8: Obtuvo el stream para {url}")
return stream_url, "M3U8_Video"
except Exception as e:
logger.error(f"m3u8 falló al obtener la lista de reproducción: {str(e)}")
logger.critical(f"No se pudo obtener el stream para {url} usando ninguna herramienta.")
raise ValueError("No se pudo obtener el stream.")