dosage/dosagelib/helpers.py

48 lines
1.5 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
2013-02-11 18:43:46 +00:00
from .util import fetchUrl, getPageContent, 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."""
2013-03-07 17:22:49 +00:00
url = pageUrl if usePageUrl else imageUrl
2012-06-20 19:58:13 +00:00
return getQueryParams(url)[paramName][0]
return _namer
2013-03-07 17:22:49 +00:00
def regexNamer(regex, usePageUrl=False):
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."""
2013-03-07 17:22:49 +00:00
url = pageUrl if usePageUrl else imageUrl
mo = regex.search(url)
2012-11-13 18:12:28 +00:00
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-12 16:55:13 +00:00
data, baseUrl = getPageContent(url, cls.session)
2013-02-11 18:43:46 +00:00
url1 = fetchUrl(url, data, baseUrl, cls.prevSearch)
2013-02-12 16:55:13 +00:00
data, baseUrl = getPageContent(url1, cls.session)
2013-02-11 18:43:46 +00:00
return fetchUrl(url1, data, baseUrl, nextSearch)
2012-06-20 19:58:13 +00:00
return _starter
2013-02-11 18:43:46 +00:00
def indirectStarter(url, 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."""
2013-02-12 16:55:13 +00:00
data, baseUrl = getPageContent(url, cls.session)
2013-02-11 18:43:46 +00:00
return fetchUrl(url, data, baseUrl, latestSearch)
2012-06-20 19:58:13 +00:00
return _starter