2012-06-20 19:58:13 +00:00
#!/usr/bin/env python
2012-06-20 20:41:04 +00:00
# -*- coding: iso-8859-1 -*-
2012-06-20 19:58:13 +00:00
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
2013-01-23 18:34:11 +00:00
# Copyright (C) 2012-2013 Bastian Kleineidam
2013-03-07 17:19:20 +00:00
# ___
# / \___ ___ __ _ __ _ ___
# / /\ / _ \/ __|/ _` |/ _` |/ _ \
# / /_// (_) \__ \ (_| | (_| | __/
# /___,' \___/|___/\__,_|\__, |\___|
# |___/
2012-11-20 17:53:53 +00:00
from __future__ import print_function
2012-06-20 19:58:13 +00:00
import sys
import os
2013-02-22 17:29:58 +00:00
import argparse
2013-02-28 20:08:19 +00:00
import pydoc
2013-03-09 08:00:50 +00:00
from cStringIO import StringIO
2012-06-20 19:58:13 +00:00
from dosagelib import events, scraper
from dosagelib.output import out
2013-03-08 21:33:05 +00:00
from dosagelib.util import internal_error, getDirname, strlimit, getLangName
2012-12-12 16:41:29 +00:00
from dosagelib.ansicolor import get_columns
2012-11-20 17:53:53 +00:00
from dosagelib.configuration import App, Freeware, Copyright, SupportUrl
2012-06-20 19:58:13 +00:00
2013-02-28 20:08:19 +00:00
class ArgumentParser(argparse.ArgumentParser):
"""Custom argument parser."""
def print_help(self, file=None):
"""Paginate help message on TTYs."""
msg = self.format_help()
if file is None:
file = sys.stdout
if hasattr(file, "isatty") and file.isatty():
pydoc.pager(msg)
else:
print(msg, file=file)
Examples = """\
EXAMPLES
List available comics (ca. 3000 at the moment):
dosage -l
Get the latest comic of for example CalvinAndHobbes and save it in the "Comics"
directory:
dosage CalvinAndHobbes
If you already have downloaded several comics and want to get the latest
strips of all of them:
dosage --continue @
On Unix, xargs(1) can download several comic strips in parallel,
for example using up to 4 processes:
cd Comics && find . -type d | xargs -n1 -P4 dosage -b . -v
"""
2012-06-20 19:58:13 +00:00
def setupOptions():
2012-09-26 14:47:39 +00:00
"""Construct option parser.
@return: new option parser
2013-02-22 17:29:58 +00:00
@rtype argparse.ArgumentParser
2012-09-26 14:47:39 +00:00
"""
2013-02-28 20:08:19 +00:00
kwargs = dict(
2013-03-06 19:00:30 +00:00
description = "A commandline comic downloader and archiver.",
2013-02-28 20:08:19 +00:00
epilog = Examples,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
2013-02-22 17:29:58 +00:00
if sys.argv[0].endswith("mainline"):
out.warn("the 'mainline' program is deprecated, please use the new 'dosage' program")
kwargs["prog"] = "dosage"
2013-02-28 20:08:19 +00:00
parser = ArgumentParser(**kwargs)
2013-02-22 17:29:58 +00:00
parser.add_argument('-v', '--verbose', action='count', default=0, help='provides verbose output, use multiple times for more verbosity')
parser.add_argument('-n', '--numstrips', action='store', type=int, default=0, help='traverse and retrieve the given number of comic strips; use --all to retrieve all comic strips')
2013-03-04 18:10:27 +00:00
parser.add_argument('-a', '--all', action='store_true', help='traverse and retrieve all comic strips')
parser.add_argument('-c', '--continue', action='store_true', dest='cont', help='traverse and retrieve comic strips until an existing one is found')
2013-02-22 17:29:58 +00:00
parser.add_argument('-b', '--basepath', action='store', default='Comics', help='set the path to create invidivual comic directories in, default is Comics', metavar='PATH')
2013-03-04 18:10:27 +00:00
parser.add_argument('--baseurl', action='store', help='the base URL of your comics directory (for RSS, HTML, etc.); this should correspond to --base-path', metavar='PATH')
parser.add_argument('-l', '--list', action='store_true', help='list available comic modules')
parser.add_argument('--singlelist', action='store_true', help='list available comic modules in a single list')
2013-02-22 17:29:58 +00:00
parser.add_argument('--version', action='store_true', help='display the version number')
parser.add_argument('-m', '--modulehelp', action='store_true', help='display help for comic modules')
2013-03-04 18:10:27 +00:00
parser.add_argument('-t', '--timestamps', action='store_true', help='print timestamps for all output at any info level')
2013-03-11 16:33:59 +00:00
parser.add_argument('-o', '--output', action='append', dest='handler', choices=events.getHandlerNames(), help='sets output handlers for downloaded comics')
2013-03-04 18:10:27 +00:00
parser.add_argument('--adult', action='store_true', help='confirms that you are old enough to view adult content')
2013-03-07 17:19:20 +00:00
parser.add_argument('--multimatch', action='store_true', help=argparse.SUPPRESS)
2013-03-04 18:10:27 +00:00
parser.add_argument('comic', nargs='*', help='comic module name (including case insensitive substrings)')
2013-02-27 05:50:34 +00:00
try:
import argcomplete
argcomplete.autocomplete(parser)
2013-02-27 05:51:20 +00:00
except ImportError:
pass
2012-06-20 19:58:13 +00:00
return parser
2012-09-26 14:47:39 +00:00
def displayVersion():
"""Display application name, version, copyright and license."""
2012-11-20 17:53:53 +00:00
print(App)
print(Copyright)
print(Freeware)
print("For support see", SupportUrl)
2012-10-11 10:03:12 +00:00
return 0
2012-09-26 14:47:39 +00:00
2012-10-11 10:03:12 +00:00
def setOutputInfo(options):
"""Set global output level and timestamp option."""
out.level = 0
out.level += options.verbose
out.timestamps = options.timestamps
2012-06-20 19:58:13 +00:00
2012-10-11 16:08:18 +00:00
def saveComicStrip(strip, basepath):
2012-10-11 10:03:12 +00:00
"""Save a comic strip which can consist of multiple images."""
errors = 0
2012-10-11 17:53:37 +00:00
allskipped = True
2012-10-11 10:03:12 +00:00
for image in strip.getImages():
2012-06-20 19:58:13 +00:00
try:
2012-10-11 17:53:37 +00:00
filename, saved = image.save(basepath)
if saved:
allskipped = False
2013-03-08 21:33:05 +00:00
except Exception as msg:
2013-03-25 18:39:37 +00:00
out.exception('Could not save image at %s to %s: %s' % (image.referrer, image.filename, msg))
2012-10-11 10:03:12 +00:00
errors += 1
2012-10-11 17:53:37 +00:00
return errors, allskipped
2012-10-11 10:03:12 +00:00
2012-12-12 16:41:29 +00:00
def displayHelp(comics):
2012-10-11 10:03:12 +00:00
"""Print help for comic strips."""
2013-03-11 19:05:06 +00:00
try:
for scraperobj in getScrapers(comics):
displayComicHelp(scraperobj)
except ValueError as msg:
2013-03-25 18:39:37 +00:00
out.exception(msg)
2013-03-11 19:05:06 +00:00
return 1
2012-10-11 10:03:12 +00:00
return 0
2012-12-07 23:45:18 +00:00
2012-12-12 16:41:29 +00:00
def displayComicHelp(scraperobj):
"""Print description and help for a comic."""
2013-03-08 21:33:05 +00:00
out.context = getScraperName(scraperobj)
2012-12-12 16:41:29 +00:00
try:
if scraperobj.description:
2013-03-01 19:56:32 +00:00
out.info("Description: " + scraperobj.description)
2013-03-08 21:33:05 +00:00
if scraperobj.lang:
out.info("Language: " + getLangName(scraperobj.lang))
2012-12-12 16:41:29 +00:00
if scraperobj.help:
for line in scraperobj.help.splitlines():
out.info(line)
finally:
out.context = ''
2013-02-22 17:29:58 +00:00
def getComics(options):
"""Retrieve comics."""
2012-10-11 10:03:12 +00:00
errors = 0
2013-03-11 16:33:59 +00:00
if options.handler:
2013-03-25 18:39:37 +00:00
for name in set(options.handler):
2013-03-11 16:33:59 +00:00
events.addHandler(name, options.basepath, options.baseurl)
2012-10-12 20:07:50 +00:00
events.getHandler().start()
2012-12-12 16:41:29 +00:00
try:
2013-02-22 17:29:58 +00:00
for scraperobj in getScrapers(options.comic, options.basepath, options.adult, options.multimatch):
2012-12-12 16:41:29 +00:00
errors += getStrips(scraperobj, options)
2013-03-11 19:05:06 +00:00
except ValueError as msg:
2013-03-25 18:39:37 +00:00
out.exception(msg)
2013-03-11 19:05:06 +00:00
errors += 1
2012-12-12 16:41:29 +00:00
finally:
events.getHandler().end()
return errors
def getStrips(scraperobj, options):
"""Get all strips from a scraper."""
errors = 0
if options.all:
2013-03-06 19:00:30 +00:00
numstrips = None
2012-12-12 16:41:29 +00:00
elif options.numstrips:
2013-03-06 19:00:30 +00:00
numstrips = options.numstrips
2012-12-12 16:41:29 +00:00
else:
2013-03-06 19:00:30 +00:00
# get current strip
numstrips = 1
2012-12-12 16:41:29 +00:00
try:
2013-03-11 18:44:42 +00:00
out.context = scraperobj.getName()
2013-03-06 19:00:30 +00:00
for strip in scraperobj.getStrips(numstrips):
2012-12-12 16:41:29 +00:00
_errors, skipped = saveComicStrip(strip, options.basepath)
errors += _errors
2013-02-04 19:22:54 +00:00
if skipped and options.cont:
# stop when retrieval skipped an image for one comic strip
2012-12-12 16:41:29 +00:00
out.info("Stop retrieval because image file already exists")
break
2013-03-07 23:06:50 +00:00
except Exception as msg:
2013-03-25 18:39:37 +00:00
out.exception(msg)
2012-12-12 16:41:29 +00:00
errors += 1
2013-03-11 18:44:42 +00:00
finally:
out.context = ''
2012-10-11 10:03:12 +00:00
return errors
2013-02-22 17:29:58 +00:00
def run(options):
2012-10-11 10:03:12 +00:00
"""Execute comic commands."""
setOutputInfo(options)
if options.version:
return displayVersion()
if options.list:
2013-03-04 18:10:27 +00:00
return doList()
if options.singlelist:
2013-03-08 21:33:05 +00:00
return doList(columnList=False, verbose=options.verbose)
2013-03-04 18:10:27 +00:00
# after this a list of comic strips is needed
if not options.comic:
out.warn('No comics specified, bailing out!')
return 1
2013-02-22 17:29:58 +00:00
if options.modulehelp:
return displayHelp(options.comic)
return getComics(options)
2012-10-11 10:03:12 +00:00
2013-03-08 21:33:05 +00:00
def doList(columnList=True, verbose=False):
2012-10-11 10:03:12 +00:00
"""List available comics."""
2013-03-09 08:00:50 +00:00
page = hasattr(sys.stdout, "isatty") and sys.stdout.isatty()
if page:
fd = StringIO()
else:
fd = sys.stdout
out.setStream(fd)
2012-12-07 23:45:18 +00:00
out.info('Available comic scrapers:')
2013-03-18 17:16:05 +00:00
out.info('Comics tagged with [%s] require age confirmation with the --adult option.' % TAG_ADULT)
out.info('Non-english comics are tagged with [%s].' % TAG_LANG)
2013-03-06 19:00:30 +00:00
scrapers = sorted(getScrapers(['@@']), key=lambda s: s.getName())
2013-03-08 21:33:05 +00:00
if columnList:
num = doColumnList(scrapers)
else:
num = doSingleList(scrapers, verbose=verbose)
out.info('%d supported comics.' % num)
2013-03-09 08:00:50 +00:00
if page:
pydoc.pager(fd.getvalue())
2012-10-11 10:03:12 +00:00
return 0
2013-03-08 21:33:05 +00:00
def doSingleList(scrapers, verbose=False):
2012-10-11 10:03:12 +00:00
"""Get list of scraper names, one per line."""
2012-10-12 19:57:06 +00:00
for num, scraperobj in enumerate(scrapers):
2013-03-08 21:33:05 +00:00
if verbose:
displayComicHelp(scraperobj)
else:
2013-03-09 08:00:50 +00:00
out.info(getScraperName(scraperobj))
2012-10-12 19:57:06 +00:00
return num
2012-10-11 10:03:12 +00:00
def doColumnList(scrapers):
"""Get list of scraper names with multiple names per line."""
2012-10-12 19:57:06 +00:00
screenWidth = get_columns(sys.stdout)
2012-12-12 16:41:29 +00:00
# limit name length so at least two columns are there
limit = (screenWidth / 2) - 8
names = [getScraperName(scraperobj, limit=limit) for scraperobj in scrapers]
2012-10-12 19:57:06 +00:00
num = len(names)
2012-12-12 16:41:29 +00:00
maxlen = max(len(name) for name in names)
namesPerLine = max(int(screenWidth / (maxlen + 1)), 1)
2012-10-11 10:03:12 +00:00
while names:
2013-03-09 08:00:50 +00:00
out.info(''.join(name.ljust(maxlen) for name in names[:namesPerLine]))
2012-10-11 10:03:12 +00:00
del names[:namesPerLine]
2012-10-12 19:57:06 +00:00
return num
2012-10-11 10:03:12 +00:00
2013-03-18 17:16:05 +00:00
TAG_ADULT = "adult"
TAG_LANG = "lang"
2012-10-11 10:03:12 +00:00
2012-12-12 16:41:29 +00:00
def getScraperName(scraperobj, limit=None):
"""Get comic scraper name."""
2013-03-18 17:16:05 +00:00
tags = []
if scraperobj.adult:
tags.append(TAG_ADULT)
if scraperobj.lang != "en":
tags.append("%s:%s" % (TAG_LANG, scraperobj.lang))
if tags:
suffix = " [" + ", ".join(tags) + "]"
else:
suffix = ""
2013-03-06 19:00:30 +00:00
name = scraperobj.getName()
2012-12-12 16:41:29 +00:00
if limit is not None:
name = strlimit(name, limit)
return name + suffix
2013-02-13 21:18:05 +00:00
def getScrapers(comics, basepath=None, adult=True, multiple_allowed=False):
2012-10-11 10:03:12 +00:00
"""Get scraper objects for the given comics."""
if '@' in comics:
# only scrapers whose directory already exists
if len(comics) > 1:
2012-12-07 23:45:18 +00:00
out.warn("using '@' as comic name ignores all other specified comics.")
2013-02-13 18:59:13 +00:00
for scraperclass in scraper.get_scraperclasses():
2013-03-06 19:00:30 +00:00
dirname = getDirname(scraperclass.getName())
2012-10-11 10:03:12 +00:00
if os.path.isdir(os.path.join(basepath, dirname)):
2013-03-26 16:28:36 +00:00
if not adult and scraperclass.adult:
warn_adult(scraperclass)
continue
2012-10-11 10:03:12 +00:00
yield scraperclass()
elif '@@' in comics:
# all scrapers
2013-02-13 18:59:13 +00:00
for scraperclass in scraper.get_scraperclasses():
2012-12-08 20:29:41 +00:00
if not adult and scraperclass.adult:
2012-12-12 16:41:29 +00:00
warn_adult(scraperclass)
2012-12-08 20:29:41 +00:00
continue
2012-10-11 10:03:12 +00:00
yield scraperclass()
else:
2012-12-07 23:45:18 +00:00
# get only selected comic scrapers
2013-03-26 16:29:03 +00:00
# store them in a set to eliminate duplicates
scrapers = set()
2012-10-11 10:03:12 +00:00
for comic in comics:
2013-03-13 06:18:00 +00:00
if basepath and comic.startswith(basepath):
2013-03-13 17:31:50 +00:00
# make the following command work:
# find Comics -type d | xargs -n1 -P10 dosage -b Comics
2013-03-26 16:29:03 +00:00
comic = comic[len(basepath):].lstrip(os.sep)
2012-10-11 12:17:25 +00:00
if ':' in comic:
name, index = comic.split(':', 1)
2012-10-11 17:53:37 +00:00
indexes = index.split(',')
2012-06-20 19:58:13 +00:00
else:
2012-10-11 12:17:25 +00:00
name = comic
2012-10-11 17:53:37 +00:00
indexes = None
2013-03-11 19:05:06 +00:00
scraperclasses = scraper.find_scraperclasses(name, multiple_allowed=multiple_allowed)
2013-03-11 18:44:42 +00:00
for scraperclass in scraperclasses:
2013-02-13 21:18:05 +00:00
if not adult and scraperclass.adult:
warn_adult(scraperclass)
continue
scraperobj = scraperclass(indexes=indexes)
if scraperobj not in scrapers:
2013-03-26 16:29:03 +00:00
scrapers.add(scraperobj)
yield scraperobj
2012-12-07 23:45:18 +00:00
2012-06-20 19:58:13 +00:00
2012-12-12 16:41:29 +00:00
def warn_adult(scraperclass):
"""Print warning about adult content."""
2013-03-06 19:00:30 +00:00
out.warn("skipping adult comic %s; use the --adult option to confirm your age" % scraperclass.getName())
2012-12-12 16:41:29 +00:00
2012-06-20 19:58:13 +00:00
def main():
2012-09-26 14:47:39 +00:00
"""Parse options and execute commands."""
2012-06-20 19:58:13 +00:00
try:
parser = setupOptions()
2013-02-22 17:29:58 +00:00
res = run(parser.parse_args())
2012-06-20 19:58:13 +00:00
except KeyboardInterrupt:
2012-11-20 17:53:53 +00:00
print("Aborted.")
2012-06-20 19:58:13 +00:00
res = 1
except Exception:
internal_error()
res = 2
return res
2012-11-28 17:15:12 +00:00
def profile():
"""Profile the loading of all scrapers."""
import cProfile
2013-02-13 18:59:13 +00:00
cProfile.run("scraper.get_scraperclasses()", "dosage.prof")
2012-11-28 17:15:12 +00:00
def viewprof():
"""View profile stats."""
import pstats
stats = pstats.Stats("dosage.prof")
stats.strip_dirs().sort_stats("cumulative").print_stats(100)
2012-06-20 19:58:13 +00:00
if __name__ == '__main__':
sys.exit(main())
2012-11-28 17:15:12 +00:00
#profile()
#viewprof()