2013-03-23 15:02:41 +01:00
|
|
|
# ex:ts=4:sw=4:sts=4:et
|
|
|
|
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import logging
|
2014-02-18 18:56:28 +01:00
|
|
|
import copy
|
2014-03-24 21:54:03 +01:00
|
|
|
import platform
|
2013-03-23 15:02:41 +01:00
|
|
|
from optparse import OptionParser
|
|
|
|
|
2014-02-09 15:40:02 +01:00
|
|
|
from svtplay_dl.error import UIException
|
2013-03-23 15:02:41 +01:00
|
|
|
from svtplay_dl.log import log
|
2015-01-28 18:26:50 +01:00
|
|
|
from svtplay_dl.utils import decode_html_entities, filenamify, select_quality, list_quality
|
|
|
|
from svtplay_dl.utils.urllib import URLError
|
2013-03-23 16:11:36 +01:00
|
|
|
from svtplay_dl.service import service_handler, Generic
|
2014-04-21 20:05:36 +02:00
|
|
|
from svtplay_dl.fetcher import VideoRetriever
|
2014-08-19 15:21:27 +02:00
|
|
|
from svtplay_dl.subtitle import subtitle
|
2013-03-23 15:02:41 +01:00
|
|
|
|
2015-01-28 18:26:50 +01:00
|
|
|
from svtplay_dl.service.aftonbladet import Aftonbladet
|
|
|
|
from svtplay_dl.service.bambuser import Bambuser
|
|
|
|
from svtplay_dl.service.bigbrother import Bigbrother
|
|
|
|
from svtplay_dl.service.dbtv import Dbtv
|
|
|
|
from svtplay_dl.service.disney import Disney
|
|
|
|
from svtplay_dl.service.dr import Dr
|
|
|
|
from svtplay_dl.service.expressen import Expressen
|
|
|
|
from svtplay_dl.service.hbo import Hbo
|
|
|
|
from svtplay_dl.service.justin import Justin
|
|
|
|
from svtplay_dl.service.kanal5 import Kanal5
|
|
|
|
from svtplay_dl.service.lemonwhale import Lemonwhale
|
|
|
|
from svtplay_dl.service.mtvnn import Mtvnn
|
|
|
|
from svtplay_dl.service.mtvservices import Mtvservices
|
|
|
|
from svtplay_dl.service.nrk import Nrk
|
|
|
|
from svtplay_dl.service.oppetarkiv import OppetArkiv
|
|
|
|
from svtplay_dl.service.picsearch import Picsearch
|
|
|
|
from svtplay_dl.service.qbrick import Qbrick
|
|
|
|
from svtplay_dl.service.radioplay import Radioplay
|
|
|
|
from svtplay_dl.service.ruv import Ruv
|
|
|
|
from svtplay_dl.service.raw import Raw
|
|
|
|
from svtplay_dl.service.sr import Sr
|
|
|
|
from svtplay_dl.service.svtplay import Svtplay
|
|
|
|
from svtplay_dl.service.tv4play import Tv4play
|
|
|
|
from svtplay_dl.service.urplay import Urplay
|
|
|
|
from svtplay_dl.service.vg import Vg
|
|
|
|
from svtplay_dl.service.viaplay import Viaplay
|
|
|
|
from svtplay_dl.service.vimeo import Vimeo
|
|
|
|
from svtplay_dl.service.youplay import Youplay
|
|
|
|
|
2015-01-05 22:41:50 +01:00
|
|
|
__version__ = "0.10.2015.01.05"
|
2013-03-23 15:02:41 +01:00
|
|
|
|
2015-01-28 18:26:50 +01:00
|
|
|
sites = [
|
|
|
|
Aftonbladet,
|
|
|
|
Bambuser,
|
|
|
|
Bigbrother,
|
|
|
|
Dbtv,
|
|
|
|
Disney,
|
|
|
|
Dr,
|
|
|
|
Expressen,
|
|
|
|
Hbo,
|
|
|
|
Justin,
|
|
|
|
Lemonwhale,
|
|
|
|
Kanal5,
|
|
|
|
Mtvservices,
|
|
|
|
Mtvnn,
|
|
|
|
Nrk,
|
|
|
|
Qbrick,
|
|
|
|
Picsearch,
|
|
|
|
Ruv,
|
|
|
|
Radioplay,
|
|
|
|
Sr,
|
|
|
|
Svtplay,
|
|
|
|
OppetArkiv,
|
|
|
|
Tv4play,
|
|
|
|
Urplay,
|
|
|
|
Viaplay,
|
|
|
|
Vimeo,
|
|
|
|
Vg,
|
|
|
|
Youplay]
|
|
|
|
|
2014-08-12 00:08:51 +02:00
|
|
|
class Options(object):
|
2013-03-23 15:02:41 +01:00
|
|
|
"""
|
|
|
|
Options used when invoking the script from another Python script.
|
|
|
|
|
|
|
|
Simple container class used when calling get_media() from another Python
|
|
|
|
script. The variables corresponds to the command line parameters parsed
|
|
|
|
in main() when the script is called directly.
|
|
|
|
|
|
|
|
When called from a script there are a few more things to consider:
|
|
|
|
|
|
|
|
* Logging is done to 'log'. main() calls setup_log() which sets the
|
|
|
|
logging to either stdout or stderr depending on the silent level.
|
|
|
|
A user calling get_media() directly can either also use setup_log()
|
|
|
|
or configure the log manually.
|
|
|
|
|
|
|
|
* Progress information is printed to 'progress_stream' which defaults to
|
|
|
|
sys.stderr but can be changed to any stream.
|
|
|
|
|
|
|
|
* Many errors results in calls to system.exit() so catch 'SystemExit'-
|
|
|
|
Exceptions to prevent the entire application from exiting if that happens.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.output = None
|
|
|
|
self.resume = False
|
|
|
|
self.live = False
|
|
|
|
self.silent = False
|
2014-02-09 15:40:02 +01:00
|
|
|
self.force = False
|
2013-12-11 17:22:46 +01:00
|
|
|
self.quality = 0
|
2013-03-23 18:26:48 +01:00
|
|
|
self.flexibleq = None
|
2015-01-05 22:40:40 +01:00
|
|
|
self.list_quality = False
|
2013-03-23 15:02:41 +01:00
|
|
|
self.hls = False
|
|
|
|
self.other = None
|
|
|
|
self.subtitle = False
|
|
|
|
self.username = None
|
|
|
|
self.password = None
|
2014-01-19 14:26:48 +01:00
|
|
|
self.thumbnail = False
|
2014-02-18 18:56:28 +01:00
|
|
|
self.all_episodes = False
|
2014-12-26 00:39:54 +01:00
|
|
|
self.all_last = -1
|
2014-03-21 20:59:58 +01:00
|
|
|
self.force_subtitle = False
|
2014-05-01 17:13:46 +02:00
|
|
|
self.preferred = None
|
2014-08-11 21:00:19 +02:00
|
|
|
self.verbose = False
|
2014-08-11 21:24:35 +02:00
|
|
|
self.output_auto = False
|
2014-08-27 22:40:31 +02:00
|
|
|
self.service = None
|
2014-11-25 18:43:40 +01:00
|
|
|
self.cookies = None
|
2014-12-22 17:41:40 +01:00
|
|
|
self.exclude = None
|
2013-03-23 15:02:41 +01:00
|
|
|
|
|
|
|
def get_media(url, options):
|
2014-01-19 14:26:48 +01:00
|
|
|
|
2015-01-28 18:26:50 +01:00
|
|
|
stream = service_handler(sites, url)
|
2013-09-04 15:02:58 +02:00
|
|
|
if not stream:
|
2014-09-11 23:30:34 +02:00
|
|
|
try:
|
2015-01-28 18:26:50 +01:00
|
|
|
url, stream = Generic().get(sites, url)
|
2014-09-11 23:30:34 +02:00
|
|
|
except URLError as e:
|
|
|
|
log.error("Cant find that page: %s", e.reason)
|
|
|
|
return
|
2013-03-23 15:02:41 +01:00
|
|
|
if not stream:
|
2015-01-28 20:50:10 +01:00
|
|
|
if url.find(".f4m") > 0 or url.find(".m3u8") > 0:
|
2015-01-28 19:00:31 +01:00
|
|
|
stream = Raw(url)
|
|
|
|
if not stream:
|
|
|
|
log.error("That site is not supported. Make a ticket or send a message")
|
|
|
|
sys.exit(2)
|
2013-03-23 15:02:41 +01:00
|
|
|
|
2014-02-18 18:56:28 +01:00
|
|
|
if options.all_episodes:
|
2014-09-21 20:30:28 +02:00
|
|
|
if options.output and os.path.isfile(options.output):
|
2014-02-18 18:56:28 +01:00
|
|
|
log.error("Output must be a directory if used with --all-episodes")
|
|
|
|
sys.exit(2)
|
2014-09-21 20:30:28 +02:00
|
|
|
elif options.output and not os.path.exists(options.output):
|
|
|
|
try:
|
|
|
|
os.makedirs(options.output)
|
|
|
|
except OSError as e:
|
2015-01-05 21:52:34 +01:00
|
|
|
log.error("%s: %s" % (e.strerror, e.filename))
|
2014-09-21 20:30:28 +02:00
|
|
|
return
|
2014-02-18 18:56:28 +01:00
|
|
|
|
|
|
|
episodes = stream.find_all_episodes(options)
|
2014-12-25 23:55:53 +01:00
|
|
|
if episodes is None:
|
|
|
|
return
|
2014-02-18 18:56:28 +01:00
|
|
|
for idx, o in enumerate(episodes):
|
|
|
|
if o == url:
|
|
|
|
substream = stream
|
|
|
|
else:
|
2015-01-28 18:33:15 +01:00
|
|
|
substream = service_handler(sites, o)
|
2014-02-18 18:56:28 +01:00
|
|
|
|
2014-03-19 23:01:36 +01:00
|
|
|
log.info("Episode %d of %d", idx + 1, len(episodes))
|
2014-02-18 18:56:28 +01:00
|
|
|
|
2014-12-25 23:48:34 +01:00
|
|
|
try:
|
|
|
|
# get_one_media overwrites options.output...
|
|
|
|
get_one_media(substream, copy.copy(options))
|
|
|
|
except URLError as e:
|
|
|
|
log.error("Cant find that page: %s", e.reason)
|
|
|
|
return
|
2014-02-18 18:56:28 +01:00
|
|
|
else:
|
2014-12-25 23:48:34 +01:00
|
|
|
try:
|
|
|
|
get_one_media(stream, options)
|
|
|
|
except URLError as e:
|
|
|
|
log.error("Cant find that page: %s", e.reason)
|
|
|
|
sys.exit(2)
|
2014-02-18 18:56:28 +01:00
|
|
|
|
|
|
|
def get_one_media(stream, options):
|
2013-03-23 15:02:41 +01:00
|
|
|
if not options.output or os.path.isdir(options.output):
|
2014-12-08 23:07:02 +01:00
|
|
|
error, data = stream.get_urldata()
|
|
|
|
if error:
|
|
|
|
log.error("Cant find that page")
|
2014-09-11 23:30:34 +02:00
|
|
|
return
|
|
|
|
if data is None:
|
|
|
|
return
|
2014-01-04 22:17:21 +01:00
|
|
|
match = re.search(r"(?i)<title[^>]*>\s*(.*?)\s*</title>", data, re.S)
|
2013-03-23 15:02:41 +01:00
|
|
|
if match:
|
2014-08-11 21:24:35 +02:00
|
|
|
options.output_auto = True
|
2014-01-05 16:58:17 +01:00
|
|
|
title_tag = decode_html_entities(match.group(1))
|
2014-01-05 17:02:48 +01:00
|
|
|
if not options.output:
|
|
|
|
options.output = filenamify(title_tag)
|
2013-03-23 15:02:41 +01:00
|
|
|
else:
|
2014-01-05 17:02:48 +01:00
|
|
|
# output is a directory
|
2014-02-18 18:59:18 +01:00
|
|
|
options.output = os.path.join(options.output, filenamify(title_tag))
|
2013-03-23 15:02:41 +01:00
|
|
|
|
2014-03-24 21:54:03 +01:00
|
|
|
if platform.system() == "Windows":
|
|
|
|
# ugly hack. replace \ with / or add extra \ because c:\test\kalle.flv will add c:_tab_est\kalle.flv
|
2015-01-25 13:54:38 +01:00
|
|
|
if options.output and options.output.find("\\") > 0:
|
2014-03-24 21:54:03 +01:00
|
|
|
options.output = options.output.replace("\\", "/")
|
|
|
|
|
2014-04-21 20:05:36 +02:00
|
|
|
videos = []
|
|
|
|
subs = []
|
|
|
|
streams = stream.get(options)
|
2014-10-12 21:17:18 +02:00
|
|
|
for i in streams:
|
|
|
|
if isinstance(i, VideoRetriever):
|
|
|
|
if options.preferred:
|
2015-01-05 22:40:40 +01:00
|
|
|
if options.preferred.lower() == i.name():
|
2014-07-13 16:03:03 +02:00
|
|
|
videos.append(i)
|
2014-10-12 21:17:18 +02:00
|
|
|
else:
|
|
|
|
videos.append(i)
|
|
|
|
if isinstance(i, subtitle):
|
|
|
|
subs.append(i)
|
2014-06-18 21:10:05 +02:00
|
|
|
|
2014-10-12 21:17:18 +02:00
|
|
|
if options.subtitle and options.output != "-":
|
|
|
|
if subs:
|
2014-08-31 01:20:36 +02:00
|
|
|
subs[0].download()
|
2014-10-12 21:17:18 +02:00
|
|
|
if options.force_subtitle:
|
|
|
|
return
|
2014-06-18 21:10:05 +02:00
|
|
|
|
2014-10-12 21:17:18 +02:00
|
|
|
if len(videos) == 0:
|
2014-06-18 21:10:05 +02:00
|
|
|
log.error("Can't find any streams for that url")
|
2014-10-12 23:53:42 +02:00
|
|
|
else:
|
2015-01-05 22:40:40 +01:00
|
|
|
if options.list_quality:
|
|
|
|
list_quality(videos)
|
|
|
|
return
|
2014-10-12 23:53:42 +02:00
|
|
|
stream = select_quality(options, videos)
|
|
|
|
log.info("Selected to download %s, bitrate: %s",
|
2015-01-05 21:52:34 +01:00
|
|
|
stream.name(), stream.bitrate)
|
2014-10-12 23:53:42 +02:00
|
|
|
try:
|
|
|
|
stream.download()
|
|
|
|
except UIException as e:
|
|
|
|
if options.verbose:
|
|
|
|
raise e
|
|
|
|
log.error(e.message)
|
|
|
|
sys.exit(2)
|
2014-01-11 23:02:47 +01:00
|
|
|
|
2014-10-12 23:53:42 +02:00
|
|
|
if options.thumbnail and hasattr(stream, "get_thumbnail"):
|
|
|
|
if options.output != "-":
|
|
|
|
log.info("Getting thumbnail")
|
|
|
|
stream.get_thumbnail(options)
|
|
|
|
else:
|
|
|
|
log.warning("Can not get thumbnail when fetching to stdout")
|
2014-10-12 21:17:18 +02:00
|
|
|
|
2013-03-23 15:02:41 +01:00
|
|
|
|
2014-01-05 16:30:45 +01:00
|
|
|
def setup_log(silent, verbose=False):
|
2014-02-09 15:38:51 +01:00
|
|
|
fmt = logging.Formatter('%(levelname)s: %(message)s')
|
2013-03-23 15:02:41 +01:00
|
|
|
if silent:
|
|
|
|
stream = sys.stderr
|
|
|
|
level = logging.WARNING
|
2014-01-05 16:30:45 +01:00
|
|
|
elif verbose:
|
|
|
|
stream = sys.stderr
|
|
|
|
level = logging.DEBUG
|
2014-10-12 23:41:44 +02:00
|
|
|
fmt = logging.Formatter('%(levelname)s [%(created)s] %(pathname)s/%(funcName)s: %(message)s')
|
2013-03-23 15:02:41 +01:00
|
|
|
else:
|
|
|
|
stream = sys.stdout
|
|
|
|
level = logging.INFO
|
|
|
|
|
|
|
|
hdlr = logging.StreamHandler(stream)
|
|
|
|
hdlr.setFormatter(fmt)
|
|
|
|
|
|
|
|
log.addHandler(hdlr)
|
|
|
|
log.setLevel(level)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
""" Main program """
|
2014-11-23 10:31:33 +01:00
|
|
|
usage = "Usage: %prog [options] url"
|
2013-03-23 15:02:41 +01:00
|
|
|
parser = OptionParser(usage=usage, version=__version__)
|
|
|
|
parser.add_option("-o", "--output",
|
2014-11-23 10:31:33 +01:00
|
|
|
metavar="OUTPUT", help="outputs to the given filename")
|
2013-03-23 15:02:41 +01:00
|
|
|
parser.add_option("-r", "--resume",
|
|
|
|
action="store_true", dest="resume", default=False,
|
2014-11-23 10:31:33 +01:00
|
|
|
help="resume a download (RTMP based ones)")
|
2013-03-23 15:02:41 +01:00
|
|
|
parser.add_option("-l", "--live",
|
|
|
|
action="store_true", dest="live", default=False,
|
2014-11-23 10:31:33 +01:00
|
|
|
help="enable for live streams (RTMP based ones)")
|
2013-03-23 15:02:41 +01:00
|
|
|
parser.add_option("-s", "--silent",
|
|
|
|
action="store_true", dest="silent", default=False)
|
2014-01-05 16:30:45 +01:00
|
|
|
parser.add_option("-v", "--verbose",
|
|
|
|
action="store_true", dest="verbose", default=False)
|
2014-02-09 15:40:02 +01:00
|
|
|
parser.add_option("-f", "--force",
|
|
|
|
action="store_true", dest="force", default=False)
|
2013-03-23 18:26:48 +01:00
|
|
|
parser.add_option("-q", "--quality", default=0,
|
2014-11-25 14:34:43 +01:00
|
|
|
metavar="quality", help="choose what format to download based on bitrate / video resolution."
|
|
|
|
"it will download the best format by default")
|
2013-03-23 18:26:48 +01:00
|
|
|
parser.add_option("-Q", "--flexible-quality", default=0,
|
2014-11-23 10:31:33 +01:00
|
|
|
metavar="amount", dest="flexibleq", help="allow given quality (as above) to differ by an amount")
|
2015-01-05 22:40:40 +01:00
|
|
|
parser.add_option("--list-quality", dest="list_quality", action="store_true", default=False,
|
|
|
|
help="list the quality for a video")
|
2013-03-23 15:02:41 +01:00
|
|
|
parser.add_option("-H", "--hls",
|
2015-01-05 22:00:18 +01:00
|
|
|
action="store_true", dest="hls", default=False, help="obsolete use -P hls")
|
2013-03-23 15:02:41 +01:00
|
|
|
parser.add_option("-S", "--subtitle",
|
|
|
|
action="store_true", dest="subtitle", default=False,
|
2014-11-23 10:31:33 +01:00
|
|
|
help="download subtitle from the site if available")
|
2014-03-21 20:59:58 +01:00
|
|
|
parser.add_option("--force-subtitle", dest="force_subtitle", default=False,
|
2014-11-23 10:31:33 +01:00
|
|
|
action="store_true", help="download only subtitle if its used with -S")
|
2013-03-23 15:02:41 +01:00
|
|
|
parser.add_option("-u", "--username", default=None,
|
2014-11-23 10:31:33 +01:00
|
|
|
help="username")
|
2013-03-23 15:02:41 +01:00
|
|
|
parser.add_option("-p", "--password", default=None,
|
2014-11-23 10:31:33 +01:00
|
|
|
help="password")
|
2014-01-19 14:26:48 +01:00
|
|
|
parser.add_option("-t", "--thumbnail",
|
|
|
|
action="store_true", dest="thumbnail", default=False,
|
2014-11-23 10:31:33 +01:00
|
|
|
help="download thumbnail from the site if available")
|
2014-02-18 18:56:28 +01:00
|
|
|
parser.add_option("-A", "--all-episodes",
|
|
|
|
action="store_true", dest="all_episodes", default=False,
|
2014-11-23 10:31:33 +01:00
|
|
|
help="try to download all episodes")
|
2014-12-26 00:39:54 +01:00
|
|
|
parser.add_option("--all-last", dest="all_last", default=-1, type=int,
|
2014-12-21 13:01:09 +01:00
|
|
|
metavar="NN", help="get last NN episodes instead of all episodes")
|
2014-05-01 17:13:46 +02:00
|
|
|
parser.add_option("-P", "--preferred", default=None,
|
2014-07-13 16:06:35 +02:00
|
|
|
metavar="preferred", help="preferred download method (rtmp, hls or hds)")
|
2014-12-22 17:41:40 +01:00
|
|
|
parser.add_option("--exclude", dest="exclude", default=None,
|
|
|
|
metavar="WORD,WORD2", help="exclude videos with the WORD(s) in the filename. comma seperated.")
|
2013-03-23 15:02:41 +01:00
|
|
|
(options, args) = parser.parse_args()
|
2013-07-25 21:01:57 +02:00
|
|
|
if not args:
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit(0)
|
2013-03-23 15:02:41 +01:00
|
|
|
if len(args) != 1:
|
2014-11-23 10:31:33 +01:00
|
|
|
parser.error("Incorrect number of arguments")
|
2014-12-22 17:41:40 +01:00
|
|
|
if options.exclude:
|
|
|
|
options.exclude = options.exclude.split(",")
|
2015-01-18 16:39:55 +01:00
|
|
|
if options.force_subtitle:
|
|
|
|
options.subtitle = True
|
2014-08-12 00:15:46 +02:00
|
|
|
options = mergeParserOption(Options(), options)
|
2014-01-05 16:30:45 +01:00
|
|
|
setup_log(options.silent, options.verbose)
|
2013-03-23 15:02:41 +01:00
|
|
|
|
2013-03-23 18:26:48 +01:00
|
|
|
if options.flexibleq and not options.quality:
|
|
|
|
log.error("flexible-quality requires a quality")
|
|
|
|
sys.exit(4)
|
|
|
|
|
2013-03-23 15:02:41 +01:00
|
|
|
url = args[0]
|
2014-04-02 19:32:35 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
get_media(url, options)
|
|
|
|
except KeyboardInterrupt:
|
2014-05-02 12:06:53 +02:00
|
|
|
print("")
|
2014-08-11 21:00:19 +02:00
|
|
|
|
|
|
|
def mergeParserOption(options, parser):
|
|
|
|
options.output = parser.output
|
|
|
|
options.resume = parser.resume
|
|
|
|
options.live = parser.live
|
|
|
|
options.silent = parser.silent
|
|
|
|
options.force = parser.force
|
|
|
|
options.quality = parser.quality
|
|
|
|
options.flexibleq = parser.flexibleq
|
2015-01-05 22:40:40 +01:00
|
|
|
options.list_quality = parser.list_quality
|
2014-08-11 21:00:19 +02:00
|
|
|
options.hls = parser.hls
|
|
|
|
options.subtitle = parser.subtitle
|
|
|
|
options.username = parser.username
|
|
|
|
options.password = parser.password
|
|
|
|
options.thumbnail = parser.thumbnail
|
|
|
|
options.all_episodes = parser.all_episodes
|
2014-12-21 13:01:09 +01:00
|
|
|
options.all_last = parser.all_last
|
2014-08-11 21:00:19 +02:00
|
|
|
options.force_subtitle = parser.force_subtitle
|
|
|
|
options.preferred = parser.preferred
|
|
|
|
options.verbose = parser.verbose
|
2014-12-22 17:41:40 +01:00
|
|
|
options.exclude = parser.exclude
|
2014-08-11 21:00:19 +02:00
|
|
|
return options
|