1# -*- coding: utf-8 -*-
2# Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs
3# Copyright (C) 2012-2014 Bastian Kleineidam
4# Copyright (C) 2015-2020 Tobias Gruetzmacher
5# Copyright (C) 2019-2020 Daniel Ring
6
7from __future__ import absolute_import, division, print_function
8
9from .util import getQueryParams
10
11
12def queryNamer(param, use_page_url=False):
13    """Get name from URL query part."""
14    def _namer(self, image_url, page_url):
15        """Get URL query part."""
16        url = page_url if use_page_url else image_url
17        return getQueryParams(url)[param][0]
18    return _namer
19
20
21def regexNamer(regex, use_page_url=False):
22    """Get name from regular expression."""
23    def _namer(self, image_url, page_url):
24        """Get first regular expression group."""
25        url = page_url if use_page_url else image_url
26        mo = regex.search(url)
27        if mo:
28            return mo.group(1)
29    return _namer
30
31
32def joinPathPartsNamer(pageurlparts, imageurlparts=(-1,), joinchar='_'):
33    """Get name by mashing path parts together with underscores."""
34    def _namer(self, imageurl, pageurl):
35        # Split and drop host name
36        pageurlsplit = pageurl.split('/')[3:]
37        imageurlsplit = imageurl.split('/')[3:]
38        joinparts = ([pageurlsplit[i] for i in pageurlparts] +
39            [imageurlsplit[i] for i in imageurlparts])
40        return joinchar.join(joinparts)
41    return _namer
42
43
44def bounceStarter(self):
45    """Get start URL by "bouncing" back and forth one time.
46
47    This needs the url and nextSearch properties be defined on the class.
48    """
49    data = self.getPage(self.url)
50    prevurl = self.fetchUrl(self.url, data, self.prevSearch)
51    prevurl = self.link_modifier(self.url, prevurl)
52    data = self.getPage(prevurl)
53    nexturl = self.fetchUrl(prevurl, data, self.nextSearch)
54    return self.link_modifier(prevurl, nexturl)
55
56
57def indirectStarter(self):
58    """Get start URL by indirection.
59
60    This is useful for comics where the latest comic can't be reached at a
61    stable URL. If the class has an attribute 'startUrl', this page is fetched
62    first, otherwise the page at 'url' is fetched. After that, the attribute
63    'latestSearch' is used on the page content to find the latest strip."""
64    url = self.startUrl if hasattr(self, "startUrl") else self.url
65    data = self.getPage(url)
66    newurl = self.fetchUrl(url, data, self.latestSearch)
67    return self.link_modifier(url, newurl)
68
69
70def xpath_class(name):
71    """Returns an XPath expressions which finds a tag which has a specified
72    class."""
73    return 'contains(concat(" ", @class, " "), " %s ")' % name
74