dosage/dosagelib/helpers.py

53 lines
1.7 KiB
Python
Raw Normal View History

# -*- coding: iso-8859-1 -*-
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
2013-02-05 18:52:10 +00:00
# Copyright (C) 2012-2013 Bastian Kleineidam
2012-10-11 10:03:12 +00:00
from .util import fetchUrl, getQueryParams
2012-06-20 19:58:13 +00:00
def queryNamer(paramName, usePageUrl=False):
2012-09-26 14:47:39 +00:00
"""Get name from URL query part."""
2012-12-12 16:41:29 +00:00
@classmethod
def _namer(cls, imageUrl, pageUrl):
"""Get URL query part."""
2012-06-20 19:58:13 +00:00
url = (imageUrl, pageUrl)[usePageUrl]
return getQueryParams(url)[paramName][0]
return _namer
def regexNamer(regex):
2012-09-26 14:47:39 +00:00
"""Get name from regular expression."""
2012-12-12 16:41:29 +00:00
@classmethod
def _namer(cls, imageUrl, pageUrl):
"""Get first regular expression group."""
2012-11-13 18:12:28 +00:00
mo = regex.search(imageUrl)
if mo:
return mo.group(1)
2012-06-20 19:58:13 +00:00
return _namer
2013-02-05 18:51:46 +00:00
def bounceStarter(url, nextSearch):
2012-09-26 14:47:39 +00:00
"""Get start URL by "bouncing" back and forth one time."""
2012-06-20 19:58:13 +00:00
@classmethod
def _starter(cls):
2012-12-12 16:41:29 +00:00
"""Get bounced start URL."""
2013-02-05 18:51:46 +00:00
url1 = fetchUrl(url, cls.prevSearch, session=cls.session)
if not url1:
raise ValueError("could not find prevSearch pattern %r in %s" % (cls.prevSearch.pattern, url))
url2 = fetchUrl(url1, nextSearch, session=cls.session)
2012-12-12 16:41:29 +00:00
if not url2:
2013-02-05 18:51:46 +00:00
raise ValueError("could not find nextSearch pattern %r in %s" % (nextSearch.pattern, url1))
2012-12-12 16:41:29 +00:00
return url2
2012-06-20 19:58:13 +00:00
return _starter
def indirectStarter(baseUrl, latestSearch):
2012-09-26 14:47:39 +00:00
"""Get start URL by indirection."""
2012-12-12 16:41:29 +00:00
@classmethod
def _starter(cls):
"""Get indirect start URL."""
url = fetchUrl(baseUrl, latestSearch, session=cls.session)
2012-11-21 20:57:26 +00:00
if not url:
raise ValueError("could not find latestSearch pattern %r in %s" % (latestSearch.pattern, baseUrl))
return url
2012-06-20 19:58:13 +00:00
return _starter