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
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
from svtplay_dl.log import log
|
2013-12-30 01:35:08 +01:00
|
|
|
from svtplay_dl.utils import get_http_data, is_py3, is_py2
|
2013-03-23 16:11:36 +01:00
|
|
|
from svtplay_dl.service import service_handler, Generic
|
2013-03-23 15:02:41 +01:00
|
|
|
|
|
|
|
|
2014-01-18 18:56:17 +01:00
|
|
|
__version__ = "0.9.2014.01.18"
|
2013-03-23 15:02:41 +01:00
|
|
|
|
|
|
|
class Options:
|
|
|
|
"""
|
|
|
|
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
|
2013-12-11 17:22:46 +01:00
|
|
|
self.quality = 0
|
2013-03-23 18:26:48 +01:00
|
|
|
self.flexibleq = None
|
2013-03-23 15:02:41 +01:00
|
|
|
self.hls = False
|
|
|
|
self.other = None
|
|
|
|
self.subtitle = False
|
|
|
|
self.username = None
|
|
|
|
self.password = None
|
|
|
|
|
|
|
|
def get_media(url, options):
|
2013-09-04 15:02:58 +02:00
|
|
|
url, stream = Generic().get(url)
|
|
|
|
if stream:
|
2013-03-23 16:11:36 +01:00
|
|
|
url = url.replace("&", "&")
|
2013-09-04 15:02:58 +02:00
|
|
|
if not stream:
|
|
|
|
stream = service_handler(url)
|
2013-03-23 15:02:41 +01:00
|
|
|
if not stream:
|
|
|
|
log.error("That site is not supported. Make a ticket or send a message")
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
if not options.output or os.path.isdir(options.output):
|
|
|
|
data = get_http_data(url)
|
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:
|
2013-12-30 01:12:38 +01:00
|
|
|
title_tag = re.sub(r'&[^\s]*;', '', match.group(1))
|
2013-12-30 01:35:08 +01:00
|
|
|
if is_py3:
|
2013-12-30 01:12:38 +01:00
|
|
|
title = re.sub(r'[^\w\s-]', '', title_tag).strip().lower()
|
2014-01-16 19:46:28 +01:00
|
|
|
tmp = re.sub(r'[-\s]+', '-', title)
|
2013-03-23 15:02:41 +01:00
|
|
|
else:
|
2013-12-30 01:12:38 +01:00
|
|
|
title = unicode(re.sub(r'[^\w\s-]', '', title_tag).strip().lower())
|
2014-01-16 20:57:30 +01:00
|
|
|
tmp = unicode(re.sub(r'[-\s]+', '-', title))
|
2014-01-16 19:46:28 +01:00
|
|
|
if options.output and os.path.isdir(options.output):
|
|
|
|
options.output += "/%s" % tmp
|
|
|
|
else:
|
|
|
|
options.output = tmp
|
2013-03-23 15:02:41 +01:00
|
|
|
|
|
|
|
stream.get(options, url)
|
|
|
|
|
2014-01-05 16:30:45 +01:00
|
|
|
def setup_log(silent, verbose=False):
|
|
|
|
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
|
|
|
|
fmt = logging.Formatter('[%(created).3f] %(pathname)s/%(funcName)s: %(levelname)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 """
|
|
|
|
usage = "usage: %prog [options] url"
|
|
|
|
parser = OptionParser(usage=usage, version=__version__)
|
|
|
|
parser.add_option("-o", "--output",
|
|
|
|
metavar="OUTPUT", help="Outputs to the given filename.")
|
|
|
|
parser.add_option("-r", "--resume",
|
|
|
|
action="store_true", dest="resume", default=False,
|
|
|
|
help="Resume a download")
|
|
|
|
parser.add_option("-l", "--live",
|
|
|
|
action="store_true", dest="live", default=False,
|
|
|
|
help="Enable for live streams")
|
|
|
|
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)
|
2013-03-23 18:26:48 +01:00
|
|
|
parser.add_option("-q", "--quality", default=0,
|
2013-03-23 15:02:41 +01:00
|
|
|
metavar="quality", help="Choose what format to download.\nIt will download the best format by default")
|
2013-03-23 18:26:48 +01:00
|
|
|
parser.add_option("-Q", "--flexible-quality", default=0,
|
|
|
|
metavar="amount", dest="flexibleq", help="Allow given quality (as above) to differ by an amount.")
|
2013-03-23 15:02:41 +01:00
|
|
|
parser.add_option("-H", "--hls",
|
|
|
|
action="store_true", dest="hls", default=False)
|
|
|
|
parser.add_option("-S", "--subtitle",
|
|
|
|
action="store_true", dest="subtitle", default=False,
|
|
|
|
help="Download subtitle from the site if available.")
|
|
|
|
parser.add_option("-u", "--username", default=None,
|
|
|
|
help="Username")
|
|
|
|
parser.add_option("-p", "--password", default=None,
|
|
|
|
help="Password")
|
|
|
|
(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:
|
|
|
|
parser.error("incorrect number of arguments")
|
|
|
|
|
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]
|
|
|
|
get_media(url, options)
|