]> code.delx.au - webdl/blobdiff - common.py
Log to stdout instead of stderr
[webdl] / common.py
index f0f827bcd4e13e5e4801302e4199fa0f79c6e91b..cd4c266cb2c980c6b6a1d2d7ea700f718bd47d89 100644 (file)
--- a/common.py
+++ b/common.py
@@ -11,9 +11,11 @@ import requests_cache
 import shutil
 import signal
 import subprocess
+import sys
 import time
 import urllib.parse
 
+USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0"
 
 try:
     import autosocks
@@ -25,6 +27,7 @@ except ImportError:
 logging.basicConfig(
     format = "%(levelname)s %(message)s",
     level = logging.INFO if os.environ.get("DEBUG", None) is None else logging.DEBUG,
+    stream = sys.stdout,
 )
 
 CACHE_FILE = os.path.join(
@@ -50,6 +53,7 @@ class Node(object):
     def get_children(self):
         if not self.children:
             self.fill_children()
+            self.children = natural_sort(self.children, key=lambda node: node.title)
         return self.children
 
     def fill_children(self):
@@ -88,7 +92,7 @@ def ensure_scheme(url):
     return urllib.parse.urlunparse(parts)
 
 http_session = requests.Session()
-http_session.headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:21.0) Gecko/20100101 Firefox/21.0"
+http_session.headers["User-Agent"] = USER_AGENT
 
 def grab_text(url):
     logging.debug("grab_text(%r)", url)
@@ -149,25 +153,24 @@ def check_command_exists(cmd):
         return False
 
 def find_ffmpeg():
-    for ffmpeg in ["avconv", "ffmpeg"]:
-        if check_command_exists([ffmpeg, "--help"]):
-            return ffmpeg
+    if check_command_exists(["ffmpeg", "--help"]):
+        return "ffmpeg"
+
+    if check_command_exists(["avconv", "--help"]):
+        logging.warn("Detected libav-tools! ffmpeg is recommended")
+        return "avconv"
 
     raise Exception("You must install ffmpeg or libav-tools")
 
 def find_ffprobe():
-    for ffprobe in ["avprobe", "ffprobe"]:
-        if check_command_exists([ffprobe, "--help"]):
-            return ffprobe
-
-    raise Exception("You must install ffmpeg or libav-tools")
+    if check_command_exists(["ffprobe", "--help"]):
+        return "ffprobe"
 
-def find_streamlink():
-    for streamlink in ["streamlink", "livestreamer"]:
-        if check_command_exists([streamlink, "--help"]):
-            return streamlink
+    if check_command_exists(["avprobe", "--help"]):
+        logging.warn("Detected libav-tools! ffmpeg is recommended")
+        return "avprobe"
 
-    raise Exception("You must install streamlink or livestreamer")
+    raise Exception("You must install ffmpeg or libav-tools")
 
 def get_duration(filename):
     ffprobe = find_ffprobe()
@@ -265,8 +268,6 @@ def convert_to_mp4(filename):
 
 
 def download_hds(filename, video_url, pvswf=None):
-    streamlink = find_streamlink()
-
     filename = sanify_filename(filename)
     logging.info("Downloading: %s", filename)
 
@@ -277,9 +278,9 @@ def download_hds(filename, video_url, pvswf=None):
         param = video_url
 
     cmd = [
-        streamlink,
-        "-f",
-        "-o", filename,
+        "streamlink",
+        "--force",
+        "--output", filename,
         param,
         "best",
     ]
@@ -289,16 +290,15 @@ def download_hds(filename, video_url, pvswf=None):
         return False
 
 def download_hls(filename, video_url):
-    streamlink = find_streamlink()
-
     filename = sanify_filename(filename)
     video_url = "hlsvariant://" + video_url
     logging.info("Downloading: %s", filename)
 
     cmd = [
-        streamlink,
-        "-f",
-        "-o", filename,
+        "streamlink",
+        "--http-header", "User-Agent=" + USER_AGENT,
+        "--force",
+        "--output", filename,
         video_url,
         "best",
     ]
@@ -308,16 +308,14 @@ def download_hls(filename, video_url):
         return False
 
 def download_mpd(filename, video_url):
-    streamlink = find_streamlink()
-
     filename = sanify_filename(filename)
     video_url = "dash://" + video_url
     logging.info("Downloading: %s", filename)
 
     cmd = [
-        streamlink,
-        "-f",
-        "-o", filename,
+        "streamlink",
+        "--force",
+        "--output", filename,
         video_url,
         "best",
     ]