2015-07-17 21:33:25 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-20 20:41:04 +00:00
|
|
|
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
|
2014-01-05 15:50:57 +00:00
|
|
|
# Copyright (C) 2012-2014 Bastian Kleineidam
|
2016-03-07 00:08:57 +00:00
|
|
|
# Copyright (C) 2015-2016 Tobias Gruetzmacher
|
|
|
|
|
2012-11-13 18:12:28 +00:00
|
|
|
import re
|
2012-11-26 17:44:31 +00:00
|
|
|
import os
|
2013-02-28 20:08:43 +00:00
|
|
|
import multiprocessing
|
2013-11-07 20:22:38 +00:00
|
|
|
try:
|
|
|
|
from urllib.parse import urlsplit
|
|
|
|
except ImportError:
|
|
|
|
from urlparse import urlsplit
|
2012-06-20 19:58:13 +00:00
|
|
|
|
|
|
|
|
2013-02-28 20:08:43 +00:00
|
|
|
def get_host(url):
|
|
|
|
"""Get host part of URL."""
|
2013-11-07 20:22:38 +00:00
|
|
|
return urlsplit(url)[1].lower()
|
2013-02-28 20:08:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Dictionary with per-host locks.
|
|
|
|
_locks = {}
|
|
|
|
# Allowed number of connections per host
|
|
|
|
MaxConnections = 4
|
2016-03-07 00:08:57 +00:00
|
|
|
# Maximum number of strips to get to test a comic
|
|
|
|
MaxStrips = 5
|
|
|
|
|
2013-02-28 20:08:43 +00:00
|
|
|
|
|
|
|
def get_lock(host):
|
|
|
|
"""Get bounded semphore for given host."""
|
|
|
|
if host not in _locks:
|
|
|
|
_locks[host] = multiprocessing.BoundedSemaphore(MaxConnections)
|
|
|
|
return _locks[host]
|
|
|
|
|
2015-07-17 21:33:25 +00:00
|
|
|
|
2016-04-13 20:05:44 +00:00
|
|
|
def _get_saved_images(outdir, scraperobj):
|
2015-07-17 21:33:25 +00:00
|
|
|
"""Get saved images."""
|
2016-04-13 20:05:44 +00:00
|
|
|
dirs = tuple(scraperobj.name.split('/'))
|
2016-03-07 00:08:57 +00:00
|
|
|
files = os.listdir(os.path.join(outdir, *dirs))
|
|
|
|
files = [x for x in files if not x.endswith(".txt")]
|
2015-07-17 21:33:25 +00:00
|
|
|
return files
|
|
|
|
|
2016-03-07 00:08:57 +00:00
|
|
|
|
2016-04-13 20:05:44 +00:00
|
|
|
def test_comicmodule(tmpdir, scraperobj):
|
2016-03-07 00:08:57 +00:00
|
|
|
'''Test a scraper. It must be able to traverse backward for at least 5
|
|
|
|
strips from the start, and find strip images on at least 4 pages.'''
|
2015-07-17 21:33:25 +00:00
|
|
|
# Limit number of connections to one host.
|
|
|
|
host = get_host(scraperobj.url)
|
|
|
|
try:
|
|
|
|
with get_lock(host):
|
2016-03-28 14:29:57 +00:00
|
|
|
_test_comic(str(tmpdir), scraperobj)
|
2015-07-17 21:33:25 +00:00
|
|
|
except OSError:
|
|
|
|
# interprocess lock not supported
|
2016-03-28 14:29:57 +00:00
|
|
|
_test_comic(str(tmpdir), scraperobj)
|
2015-07-17 21:33:25 +00:00
|
|
|
|
2016-03-07 00:08:57 +00:00
|
|
|
|
|
|
|
def _test_comic(outdir, scraperobj):
|
2015-07-17 21:33:25 +00:00
|
|
|
num_strips = 0
|
|
|
|
strip = None
|
2016-03-07 00:08:57 +00:00
|
|
|
for strip in scraperobj.getStrips(MaxStrips):
|
|
|
|
_check_strip(outdir, strip, scraperobj.multipleImagesPerStrip)
|
|
|
|
|
2016-04-15 23:14:26 +00:00
|
|
|
if num_strips > 0:
|
2016-03-07 00:08:57 +00:00
|
|
|
_check_stripurl(strip, scraperobj)
|
2015-07-17 21:33:25 +00:00
|
|
|
num_strips += 1
|
2016-03-07 00:08:57 +00:00
|
|
|
|
2015-07-17 21:33:25 +00:00
|
|
|
if scraperobj.prevSearch and not scraperobj.hitFirstStripUrl:
|
2016-03-07 00:08:57 +00:00
|
|
|
# subtract the number of skipped URLs with no image from the expected
|
|
|
|
# image number
|
|
|
|
num_strips_expected = MaxStrips - len(scraperobj.skippedUrls)
|
|
|
|
msg = 'Traversed %d strips instead of %d.' % (num_strips,
|
|
|
|
num_strips_expected)
|
2015-07-17 21:33:25 +00:00
|
|
|
if strip:
|
|
|
|
msg += " Check the prevSearch pattern at %s" % strip.stripUrl
|
|
|
|
assert num_strips == num_strips_expected, msg
|
|
|
|
if strip:
|
2016-03-07 00:08:57 +00:00
|
|
|
_check_scraperesult(outdir, num_strips_expected, strip, scraperobj)
|
|
|
|
|
|
|
|
|
|
|
|
def _check_strip(outdir, strip, multipleImagesPerStrip):
|
|
|
|
'''Check that a specific page yields images and the comic module correctly
|
|
|
|
declares if there are multiple images per page.'''
|
|
|
|
images = []
|
|
|
|
for image in strip.getImages():
|
|
|
|
images.append(image.url)
|
|
|
|
image.save(outdir)
|
|
|
|
assert images, 'failed to find images at %s' % strip.stripUrl
|
|
|
|
if not multipleImagesPerStrip:
|
|
|
|
assert len(images) == 1, 'found more than 1 image at %s: %s' % (
|
|
|
|
strip.stripUrl, images)
|
2015-07-17 21:33:25 +00:00
|
|
|
|
2016-03-07 00:08:57 +00:00
|
|
|
|
|
|
|
def _check_scraperesult(outdir, num_images_expected, strip, scraperobj):
|
|
|
|
'''Check that exactly or for multiple pages at least num_strips images are
|
|
|
|
saved. This checks saved files, ie. it detects duplicate filenames.'''
|
|
|
|
saved_images = _get_saved_images(outdir, scraperobj)
|
2015-07-17 21:33:25 +00:00
|
|
|
num_images = len(saved_images)
|
2016-03-07 00:08:57 +00:00
|
|
|
|
|
|
|
attrs = (num_images, saved_images, num_images_expected, outdir)
|
2015-07-17 21:33:25 +00:00
|
|
|
if scraperobj.multipleImagesPerStrip:
|
2016-03-07 00:08:57 +00:00
|
|
|
err = 'saved %d %s instead of at least %d images in %s' % attrs
|
|
|
|
assert num_images >= num_images_expected, err
|
2015-07-17 21:33:25 +00:00
|
|
|
else:
|
2016-03-07 00:08:57 +00:00
|
|
|
err = 'saved %d %s instead of %d images in %s' % attrs
|
|
|
|
assert num_images == num_images_expected, err
|
2015-07-17 21:33:25 +00:00
|
|
|
|
2016-03-07 00:08:57 +00:00
|
|
|
|
|
|
|
def _check_stripurl(strip, scraperobj):
|
2015-07-17 21:33:25 +00:00
|
|
|
if not scraperobj.stripUrl:
|
|
|
|
# no indexing support
|
|
|
|
return
|
|
|
|
# test that the stripUrl regex matches the retrieved strip URL
|
|
|
|
urlmatch = re.escape(scraperobj.stripUrl)
|
|
|
|
urlmatch = urlmatch.replace(r"\%s", r".+")
|
|
|
|
urlmatch = "^%s$" % urlmatch
|
|
|
|
ro = re.compile(urlmatch)
|
|
|
|
mo = ro.search(strip.stripUrl)
|
2016-03-07 00:08:57 +00:00
|
|
|
err = 'strip URL %r does not match stripUrl pattern %s' % (
|
|
|
|
strip.stripUrl, urlmatch)
|
|
|
|
assert mo is not None, err
|