From e87c236c51ff79fdff0ff9fbf42eaeaab341aaf5 Mon Sep 17 00:00:00 2001 From: Johan Andersson Date: Thu, 1 May 2014 17:13:46 +0200 Subject: [PATCH] option to choose which download method is preferred. --- lib/svtplay_dl/__init__.py | 3 +++ lib/svtplay_dl/fetcher/__init__.py | 7 +++++-- lib/svtplay_dl/fetcher/hds.py | 3 +++ lib/svtplay_dl/fetcher/hls.py | 3 +++ lib/svtplay_dl/fetcher/http.py | 3 +++ lib/svtplay_dl/fetcher/rtmp.py | 3 +++ lib/svtplay_dl/utils/__init__.py | 9 +++++++-- 7 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/svtplay_dl/__init__.py b/lib/svtplay_dl/__init__.py index ee32cf0..0d8d1e5 100644 --- a/lib/svtplay_dl/__init__.py +++ b/lib/svtplay_dl/__init__.py @@ -56,6 +56,7 @@ class Options: self.thumbnail = False self.all_episodes = False self.force_subtitle = False + self.preferred = None def get_media(url, options): @@ -196,6 +197,8 @@ def main(): parser.add_option("-A", "--all-episodes", action="store_true", dest="all_episodes", default=False, help="Try to download all episodes.") + parser.add_option("-P", "--preferred", default=None, + metavar="preferred", help="preferred download method") (options, args) = parser.parse_args() if not args: parser.print_help() diff --git a/lib/svtplay_dl/fetcher/__init__.py b/lib/svtplay_dl/fetcher/__init__.py index 6efcd6a..86ecf64 100644 --- a/lib/svtplay_dl/fetcher/__init__.py +++ b/lib/svtplay_dl/fetcher/__init__.py @@ -2,5 +2,8 @@ class VideoRetriever: def __init__(self, options, url, bitrate, **kwargs): self.options = options self.url = url - self.bitrate = bitrate - self.kwargs = kwargs \ No newline at end of file + self.bitrate = int(bitrate) + self.kwargs = kwargs + + def name(self): + pass \ No newline at end of file diff --git a/lib/svtplay_dl/fetcher/hds.py b/lib/svtplay_dl/fetcher/hds.py index 49e5dc4..ba95936 100644 --- a/lib/svtplay_dl/fetcher/hds.py +++ b/lib/svtplay_dl/fetcher/hds.py @@ -61,6 +61,9 @@ def hdsparse(options, manifest): return streams class HDS(VideoRetriever): + def name(self): + return "hds" + def download(self): if self.options.live and not self.options.force: raise LiveHDSException(self.url) diff --git a/lib/svtplay_dl/fetcher/hls.py b/lib/svtplay_dl/fetcher/hls.py index 8c2a33c..441e545 100644 --- a/lib/svtplay_dl/fetcher/hls.py +++ b/lib/svtplay_dl/fetcher/hls.py @@ -52,6 +52,9 @@ def hlsparse(url): return streams class HLS(VideoRetriever): + def name(self): + return "hls" + def download(self): if self.options.live and not self.options.force: raise LiveHLSException(self.url) diff --git a/lib/svtplay_dl/fetcher/http.py b/lib/svtplay_dl/fetcher/http.py index 3a737db..b32c18d 100644 --- a/lib/svtplay_dl/fetcher/http.py +++ b/lib/svtplay_dl/fetcher/http.py @@ -12,6 +12,9 @@ from svtplay_dl.utils.urllib import urlopen, Request, HTTPError from svtplay_dl.fetcher import VideoRetriever class HTTP(VideoRetriever): + def name(self): + return "http" + def download(self): """ Get the stream from HTTP """ request = Request(self.url) diff --git a/lib/svtplay_dl/fetcher/rtmp.py b/lib/svtplay_dl/fetcher/rtmp.py index aadb8ba..ff3a4ad 100644 --- a/lib/svtplay_dl/fetcher/rtmp.py +++ b/lib/svtplay_dl/fetcher/rtmp.py @@ -11,6 +11,9 @@ from svtplay_dl.utils import is_py2 from svtplay_dl.fetcher import VideoRetriever class RTMP(VideoRetriever): + def name(self): + return "hds" + def download(self): """ Get the stream from RTMP """ args = [] diff --git a/lib/svtplay_dl/utils/__init__.py b/lib/svtplay_dl/utils/__init__.py index 5d602de..a58ea42 100644 --- a/lib/svtplay_dl/utils/__init__.py +++ b/lib/svtplay_dl/utils/__init__.py @@ -106,8 +106,13 @@ def check_redirect(url): return url def select_quality(options, streams): - available = sorted(int(x.bitrate) for x in streams) - + if options.preferred: + available = [] + data = sorted(streams, key=lambda x:(x.name()!= options.preferred, x.name()), reverse=True) + for i in data: + available.append(i.bitrate) + else: + available = sorted(int(x.bitrate) for x in streams) try: optq = int(options.quality) except ValueError: