simplify and factorize code, remove dead code, follow conventions, use new-style classes

This commit is contained in:
Christophe Benz 2010-12-09 15:27:06 +01:00 committed by Romain Bignon
commit f1b3264a67
28 changed files with 202 additions and 251 deletions

View file

@ -36,7 +36,8 @@ class ArteBackend(BaseBackend, ICapVideo):
VERSION = '0.5' VERSION = '0.5'
DESCRIPTION = 'Arte french TV' DESCRIPTION = 'Arte french TV'
LICENSE = 'GPLv3' LICENSE = 'GPLv3'
CONFIG = ValuesDict(Value('lang', label='Lang of videos', choices={'fr': 'French', 'de': 'Deutsch', 'en': 'English'}, default='fr'), CONFIG = ValuesDict(Value('lang', label='Lang of videos',
choices={'fr': 'French', 'de': 'Deutsch', 'en': 'English'}, default='fr'),
Value('quality', label='Quality of videos', choices=['hd', 'sd'], default='hd')) Value('quality', label='Quality of videos', choices=['hd', 'sd'], default='hd'))
BROWSER = ArteBrowser BROWSER = ArteBrowser

View file

@ -25,7 +25,8 @@ class TileError(Exception):
Exception.__init__(self, msg) Exception.__init__(self, msg)
self.tile = tile self.tile = tile
class Captcha:
class Captcha(object):
def __init__(self, file): def __init__(self, file):
self.inim = Image.open(file) self.inim = Image.open(file)
self.nx,self.ny = self.inim.size self.nx,self.ny = self.inim.size
@ -77,7 +78,8 @@ class Captcha:
y += 25 y += 25
ty += 1 ty += 1
class Tile:
class Tile(object):
hash = {'b2d25ae11efaaaec6dd6a4c00f0dfc29': 1, hash = {'b2d25ae11efaaaec6dd6a4c00f0dfc29': 1,
'600873fa288e75ca6cca092ae95bf129': 2, '600873fa288e75ca6cca092ae95bf129': 2,
'da24ac28930feee169adcbc9bad4acaf': 3, 'da24ac28930feee169adcbc9bad4acaf': 3,
@ -117,4 +119,3 @@ class Tile:
for pxl in pxls: for pxl in pxls:
sys.stdout.write('%02d' % pxl) sys.stdout.write('%02d' % pxl)
sys.stdout.write('\n') sys.stdout.write('\n')

View file

@ -24,7 +24,6 @@ __all__ = ['AccountComing']
class AccountComing(BasePage): class AccountComing(BasePage):
def on_loaded(self): def on_loaded(self):
self.operations = [] self.operations = []

View file

@ -24,7 +24,6 @@ __all__ = ['AccountHistory']
class AccountHistory(BasePage): class AccountHistory(BasePage):
def on_loaded(self): def on_loaded(self):
self.operations = [] self.operations = []

View file

@ -24,6 +24,10 @@ from weboob.tools.browser import BasePage
from ..errors import PasswordExpired from ..errors import PasswordExpired
__all__ = ['AccountsList']
class AccountsList(BasePage): class AccountsList(BasePage):
LINKID_REGEXP = re.compile(".*ch4=(\w+).*") LINKID_REGEXP = re.compile(".*ch4=(\w+).*")

View file

@ -55,9 +55,11 @@ class LoginPage(BasePage):
self.browser['ch5'] = img.get_codes(password) self.browser['ch5'] = img.get_codes(password)
self.browser.submit() self.browser.submit()
class ConfirmPage(BasePage): class ConfirmPage(BasePage):
pass pass
class ChangePasswordPage(BasePage): class ChangePasswordPage(BasePage):
def change_password(self, current, new): def change_password(self, current, new):
img = Captcha(self.browser.openurl('/NSImgGrille')) img = Captcha(self.browser.openurl('/NSImgGrille'))

View file

@ -15,6 +15,7 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import re import re
from weboob.tools.browser import BasePage from weboob.tools.browser import BasePage
@ -56,6 +57,7 @@ class TransferPage(BasePage):
self.browser['T5'] = reason self.browser['T5'] = reason
self.browser.submit() self.browser.submit()
class TransferConfirmPage(BasePage): class TransferConfirmPage(BasePage):
def on_loaded(self): def on_loaded(self):
for td in self.document.getroot().cssselect('td#size2'): for td in self.document.getroot().cssselect('td#size2'):
@ -67,6 +69,7 @@ class TransferConfirmPage(BasePage):
self.browser.location('/NS_VIRDA?stp=%s' % m.group(1)) self.browser.location('/NS_VIRDA?stp=%s' % m.group(1))
return return
class TransferCompletePage(BasePage): class TransferCompletePage(BasePage):
def get_id(self): def get_id(self):
return self.group_dict['id'] return self.group_dict['id']

View file

@ -19,7 +19,6 @@
from __future__ import with_statement from __future__ import with_statement
from weboob.capabilities.messages import CantSendMessage, ICapMessages, ICapMessagesPost from weboob.capabilities.messages import CantSendMessage, ICapMessages, ICapMessagesPost
#from weboob.capabilities.account import ICapAccount, StatusField
from weboob.tools.backend import BaseBackend from weboob.tools.backend import BaseBackend
from weboob.tools.value import ValuesDict, Value from weboob.tools.value import ValuesDict, Value

View file

@ -23,6 +23,9 @@ from weboob.tools.value import ValuesDict, Value
from .browser import BPBrowser from .browser import BPBrowser
__all__ = ['BPBackend']
class BPBackend(BaseBackend, ICapBank): class BPBackend(BaseBackend, ICapBank):
NAME = 'bp' NAME = 'bp'
MAINTAINER = 'Nicolas Duhamel' MAINTAINER = 'Nicolas Duhamel'

View file

@ -25,6 +25,7 @@ from weboob.tools.value import ValuesDict, Value
from .browser import CanalplusBrowser from .browser import CanalplusBrowser
from .pages import CanalplusVideo from .pages import CanalplusVideo
__all__ = ['CanalplusBackend'] __all__ = ['CanalplusBackend']
@ -57,7 +58,6 @@ class CanalplusBackend(BaseBackend, ICapVideo):
if 'thumbnail' in fields: if 'thumbnail' in fields:
with self.browser: with self.browser:
video.thumbnail.data = self.browser.readurl(video.thumbnail.url) video.thumbnail.data = self.browser.readurl(video.thumbnail.url)
return video return video
OBJECTS = {CanalplusVideo: fill_video} OBJECTS = {CanalplusVideo: fill_video}

View file

@ -15,16 +15,21 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import urllib import urllib
import lxml.etree
from weboob.tools.browser import BaseBrowser from weboob.tools.browser import BaseBrowser
from weboob.tools.browser.decorators import id2url from weboob.tools.browser.decorators import id2url
from .pages import InitPage, CanalplusVideo, VideoPage from .pages import InitPage, CanalplusVideo, VideoPage
import lxml.etree
class XMLParser: __all__ = ['CanalplusBrowser']
class XMLParser(object):
def parse(self, data, encoding=None): def parse(self, data, encoding=None):
if encoding is None: if encoding is None:
parser = None parser = None
@ -33,22 +38,21 @@ class XMLParser:
return lxml.etree.XML(data.get_data(), parser) return lxml.etree.XML(data.get_data(), parser)
__all__ = ['CanalplusBrowser']
class CanalplusBrowser(BaseBrowser): class CanalplusBrowser(BaseBrowser):
DOMAIN = u'service.canal-plus.com' DOMAIN = u'service.canal-plus.com'
ENCODING = 'utf-8' ENCODING = 'utf-8'
PAGES = {r"http://service.canal-plus.com/video/rest/initPlayer/cplus/": InitPage, PAGES = {
r"http://service.canal-plus.com/video/rest/search/cplus/.*": VideoPage, r'http://service.canal-plus.com/video/rest/initPlayer/cplus/': InitPage,
r"http://service.canal-plus.com/video/rest/getVideosLiees/cplus/(?P<id>.+)": VideoPage, r'http://service.canal-plus.com/video/rest/search/cplus/.*': VideoPage,
} r'http://service.canal-plus.com/video/rest/getVideosLiees/cplus/(?P<id>.+)': VideoPage,
}
#We need lxml.etree.XMLParser for read CDATA #We need lxml.etree.XMLParser for read CDATA
PARSER = XMLParser() PARSER = XMLParser()
FORMATS = { 'sd': 'BAS_DEBIT', FORMATS = {
'hd': 'HD' 'sd': 'BAS_DEBIT',
} 'hd': 'HD',
}
def __init__(self, quality, *args, **kwargs): def __init__(self, quality, *args, **kwargs):
BaseBrowser.__init__(self, parser= self.PARSER, *args, **kwargs) BaseBrowser.__init__(self, parser= self.PARSER, *args, **kwargs)
@ -58,15 +62,13 @@ class CanalplusBrowser(BaseBrowser):
self.quality = 'HD' self.quality = 'HD'
def home(self): def home(self):
self.location("http://service.canal-plus.com/video/rest/initPlayer/cplus/") self.location('http://service.canal-plus.com/video/rest/initPlayer/cplus/')
def iter_search_results(self, pattern): def iter_search_results(self, pattern):
self.location("http://service.canal-plus.com/video/rest/search/cplus/" + urllib.quote_plus(pattern)) self.location('http://service.canal-plus.com/video/rest/search/cplus/' + urllib.quote_plus(pattern))
return self.page.iter_results() return self.page.iter_results()
@id2url(CanalplusVideo.id2url) @id2url(CanalplusVideo.id2url)
def get_video(self, url, video=None): def get_video(self, url, video=None):
self.location(url) self.location(url)
return self.page.get_video(video, self.quality) return self.page.get_video(video, self.quality)

View file

@ -15,26 +15,28 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.tools.browser import BasePage from weboob.tools.browser import BasePage
__all__ = ['InitPage'] __all__ = ['InitPage']
class InitPage(BasePage): class InitPage(BasePage):
def on_loaded(self): def on_loaded(self):
channels = [] channels = []
### Parse liste des channels ### Parse liste des channels
for elem in self.document[2].getchildren(): for elem in self.document[2].getchildren():
channel = {} channel = {}
for e in elem.getchildren(): for e in elem.getchildren():
subchannels = [] subchannels = []
if e.tag == "NOM": if e.tag == "NOM":
channel['nom'] = e.text channel['nom'] = e.text
elif e.tag == "SELECTIONS": elif e.tag == "SELECTIONS":
for select in e:
for select in e: subchannel = {}
subchannel = {} subchannel['id'] = select[0].text
subchannel['id'] = select[0].text subchannel['nom'] = select[1].text
subchannel['nom'] = select[1].text subchannels.append(subchannel)
subchannels.append(subchannel) channel['subchannels'] = subchannels
channel['subchannels'] = subchannels channels.append(channel)
channels.append(channel)

View file

@ -17,9 +17,14 @@
from datetime import datetime, date, time from datetime import datetime, date, time
from weboob.tools.browser import BaseBrowser from weboob.tools.browser import BaseBrowser
from weboob.tools.parsers.lxmlparser import SelectElementException
from weboob.tools.misc import to_unicode from weboob.tools.misc import to_unicode
from weboob.tools.parsers.lxmlparser import SelectElementException
__all__ = ['CanalTP']
class CanalTP(BaseBrowser): class CanalTP(BaseBrowser):
DOMAIN = 'widget.canaltp.fr' DOMAIN = 'widget.canaltp.fr'

View file

@ -34,7 +34,7 @@ class FourChanBackend(BaseBackend, ICapMessages):
EMAIL = 'romain@weboob.org' EMAIL = 'romain@weboob.org'
VERSION = '0.5' VERSION = '0.5'
LICENSE = 'GPLv3' LICENSE = 'GPLv3'
DESCRIPTION = "4chan website" DESCRIPTION = '4chan website'
CONFIG = ValuesDict(Value('boards', label='Boards to fetch')) CONFIG = ValuesDict(Value('boards', label='Boards to fetch'))
STORAGE = {'boards': {}} STORAGE = {'boards': {}}
BROWSER = FourChan BROWSER = FourChan

View file

@ -15,26 +15,27 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.tools.browser import BaseBrowser from weboob.tools.browser import BaseBrowser
from .pages.board import BoardPage from .pages.board import BoardPage
class FourChan(BaseBrowser): class FourChan(BaseBrowser):
DOMAIN = 'boards.4chan.org' DOMAIN = 'boards.4chan.org'
PAGES = {'http://boards.4chan.org/\w+/': BoardPage, PAGES = {
'http://boards.4chan.org/\w+/res/\d+': BoardPage, 'http://boards.4chan.org/\w+/': BoardPage,
} 'http://boards.4chan.org/\w+/res/\d+': BoardPage,
}
def is_logged(self): def is_logged(self):
return True return True
def get_threads(self, board): def get_threads(self, board):
self.location('http://boards.4chan.org/%s/' % board) self.location('http://boards.4chan.org/%s/' % board)
return self.page.articles return self.page.articles
def get_thread(self, board, id): def get_thread(self, board, id):
self.location('http://boards.4chan.org/%s/res/%d' % (board, long(id))) self.location('http://boards.4chan.org/%s/res/%d' % (board, long(id)))
assert len(self.page.articles) == 1 assert len(self.page.articles) == 1
return self.page.articles[0] return self.page.articles[0]

View file

@ -15,12 +15,17 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from datetime import datetime
import re import re
from datetime import datetime
from weboob.tools.browser import BasePage from weboob.tools.browser import BasePage
__all__ = ['BoardPage']
class Message(object): class Message(object):
def __init__(self, browser, board, id, filename=u'', url=u''): def __init__(self, browser, board, id, filename=u'', url=u''):
self.id = id self.id = id
@ -42,6 +47,7 @@ class Message(object):
def __repr__(self): def __repr__(self):
return '<Message id=%s filename=%s url=%s comments=%d>' % (self.id, self.filename, self.url, len(self.comments)) return '<Message id=%s filename=%s url=%s comments=%d>' % (self.id, self.filename, self.url, len(self.comments))
class BoardPage(BasePage): class BoardPage(BasePage):
URL_REGEXP = re.compile('http://boards.4chan.org/(\w+)/') URL_REGEXP = re.compile('http://boards.4chan.org/(\w+)/')

View file

@ -26,6 +26,7 @@ class IndexPage(BasePage):
def is_logged(self): def is_logged(self):
return 'id' in self.document.find('body').attrib return 'id' in self.document.find('body').attrib
class LoginPage(BasePage): class LoginPage(BasePage):
def login(self, login, password): def login(self, login, password):
self.browser.select_form(nr=0) self.browser.select_form(nr=0)

View file

@ -15,6 +15,7 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.capabilities.torrent import ICapTorrent from weboob.capabilities.torrent import ICapTorrent
from weboob.tools.backend import BaseBackend from weboob.tools.backend import BaseBackend
@ -31,10 +32,6 @@ class IsohuntBackend(BaseBackend, ICapTorrent):
VERSION = '0.5' VERSION = '0.5'
DESCRIPTION = 'isohunt.com bittorrent tracker' DESCRIPTION = 'isohunt.com bittorrent tracker'
LICENSE = 'GPLv3' LICENSE = 'GPLv3'
#CONFIG = ValuesDict(Value('domain', label='Domain (example "ssl.what.cd")'),
# Value('protocol', label='Protocol to use', choices=('http', 'https')),
# Value('username', label='Username'),
# Value('password', label='Password', masked=True))
BROWSER = IsohuntBrowser BROWSER = IsohuntBrowser
def create_default_browser(self): def create_default_browser(self):
@ -47,7 +44,6 @@ class IsohuntBackend(BaseBackend, ICapTorrent):
torrent = self.browser.get_torrent(id) torrent = self.browser.get_torrent(id)
if not torrent: if not torrent:
return None return None
return self.browser.openurl(torrent.url.encode('utf-8')).read() return self.browser.openurl(torrent.url.encode('utf-8')).read()
def iter_torrents(self, pattern): def iter_torrents(self, pattern):

View file

@ -30,24 +30,19 @@ class IsohuntBrowser(BaseBrowser):
ENCODING = 'utf-8' ENCODING = 'utf-8'
USER_AGENT = BaseBrowser.USER_AGENTS['wget'] USER_AGENT = BaseBrowser.USER_AGENTS['wget']
PAGES = { PAGES = {
'https://isohunt.com/torrents/.*iht=-1&ihp=1&ihs1=2&iho1=d' : TorrentsPage, 'https://isohunt.com/torrents/.*iht=-1&ihp=1&ihs1=2&iho1=d' : TorrentsPage,
'https://isohunt.com/torrent_details.*tab=summary' : TorrentPage 'https://isohunt.com/torrent_details.*tab=summary' : TorrentPage,
} }
def __init__(self, *args, **kwargs):
BaseBrowser.__init__(self, *args, **kwargs)
def home(self): def home(self):
return self.location('https://isohunt.com') return self.location('https://isohunt.com')
def iter_torrents(self, pattern): def iter_torrents(self, pattern):
self.location('https://isohunt.com/torrents/%s?iht=-1&ihp=1&ihs1=2&iho1=d' % pattern) self.location('https://isohunt.com/torrents/%s?iht=-1&ihp=1&ihs1=2&iho1=d' % pattern)
assert self.is_on_page(TorrentsPage) assert self.is_on_page(TorrentsPage)
return self.page.iter_torrents() return self.page.iter_torrents()
def get_torrent(self, id): def get_torrent(self, id):
self.location('https://isohunt.com/torrent_details/%s/?tab=summary' % id) self.location('https://isohunt.com/torrent_details/%s/?tab=summary' % id)
assert self.is_on_page(TorrentPage) assert self.is_on_page(TorrentPage)
return self.page.get_torrent(id) return self.page.get_torrent(id)

View file

@ -16,29 +16,18 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.tools.browser import BasePage
from weboob.capabilities.torrent import Torrent from weboob.capabilities.torrent import Torrent
from weboob.tools.browser import BasePage
from weboob.tools.misc import get_bytes_size
__all__ = ['TorrentsPage'] __all__ = ['TorrentsPage']
class TorrentsPage(BasePage): class TorrentsPage(BasePage):
def unit(self, n, u):
m = {'KB': 1024,
'MB': 1024*1024,
'GB': 1024*1024*1024,
'TB': 1024*1024*1024*1024,
}
return float(n*m[u])
def iter_torrents(self): def iter_torrents(self):
for tr in self.document.getiterator('tr'): for tr in self.document.getiterator('tr'):
if tr.attrib.get('class','') == 'hlRow': if tr.attrib.get('class', '') == 'hlRow':
# TODO à corriger # TODO à corriger
atitle = tr.getchildren()[2].getchildren()[1] atitle = tr.getchildren()[2].getchildren()[1]
title = atitle.text title = atitle.text
@ -57,41 +46,32 @@ class TorrentsPage(BasePage):
seed = tr.getchildren()[4].text seed = tr.getchildren()[4].text
leech = tr.getchildren()[5].text leech = tr.getchildren()[5].text
url = 'https://isohunt.com/download/%s/mon_joli_torrent.torrent' % idt url = 'https://isohunt.com/download/%s/mon_joli_torrent.torrent' % idt
yield Torrent(idt,
title,
url=url,
size=get_bytes_size(size, u),
seeders=int(seed),
leechers=int(leech))
torrent = Torrent(idt,
title,
url=url,
size=self.unit(size,u),
seeders=int(seed),
leechers=int(leech))
yield torrent
class TorrentPage(BasePage): class TorrentPage(BasePage):
def unit(self, n, u):
m = {'KB': 1024,
'MB': 1024*1024,
'GB': 1024*1024*1024,
'TB': 1024*1024*1024*1024,
}
return float(n*m[u])
def get_torrent(self, id): def get_torrent(self, id):
title = '' title = ''
url = 'https://isohunt.com/download/%s/%s.torrent' % (id , id) url = 'https://isohunt.com/download/%s/%s.torrent' % (id, id)
for a in self.document.getiterator('a'): for a in self.document.getiterator('a'):
if 'Search more torrents of' in a.attrib.get('title',''): if 'Search more torrents of' in a.attrib.get('title', ''):
title = a.tail title = a.tail
for span in self.document.getiterator('span'): for span in self.document.getiterator('span'):
if span.attrib.get('style','') == 'color:green;' and ('ShowTip' in span.attrib.get('onmouseover','')): if span.attrib.get('style', '') == 'color:green;' and ('ShowTip' in span.attrib.get('onmouseover', '')):
seed = span.tail.split(' ')[1] seed = span.tail.split(' ')[1]
tip_id = span.attrib.get('onmouseover','').split("'")[1] tip_id = span.attrib.get('onmouseover', '').split("'")[1]
for div in self.document.getiterator('div'): for div in self.document.getiterator('div'):
# find the corresponding super tip which appears on super mouse hover! # find the corresponding super tip which appears on super mouse hover!
if div.attrib.get('class','') == 'dirs ydsf' and tip_id in div.attrib.get('id',''): if div.attrib.get('class', '') == 'dirs ydsf' and tip_id in div.attrib.get('id', ''):
leech = div.getchildren()[0].getchildren()[1].tail.split(' ')[2] leech = div.getchildren()[0].getchildren()[1].tail.split(' ')[2]
# the <b> with the size in it doesn't have a distinction # the <b> with the size in it doesn't have a distinction
# have to get it by higher # have to get it by higher
elif div.attrib.get('id','') == 'torrent_details': elif div.attrib.get('id', '') == 'torrent_details':
size = div.getchildren()[6].getchildren()[0].getchildren()[0].text size = div.getchildren()[6].getchildren()[0].getchildren()[0].text
u = size[-2:] u = size[-2:]
size = float(size[:-3]) size = float(size[:-3])
@ -104,7 +84,7 @@ class TorrentPage(BasePage):
files = [] files = []
count_p_found = 0 count_p_found = 0
for p in self.document.getiterator('p'): for p in self.document.getiterator('p'):
if p.attrib.get('style','') == "line-height:1.2em;margin-top:1.8em": if p.attrib.get('style', '') == 'line-height:1.2em;margin-top:1.8em':
count_p_found += 1 count_p_found += 1
if count_p_found == 1: if count_p_found == 1:
description = p.getchildren()[1].tail description = p.getchildren()[1].tail
@ -115,24 +95,20 @@ class TorrentPage(BasePage):
files.append(p.getchildren()[0].tail.strip()) files.append(p.getchildren()[0].tail.strip())
for td in self.document.getiterator('td'): for td in self.document.getiterator('td'):
#print td.attrib.get('class') if td.attrib.get('class', '') == 'fileRows':
if td.attrib.get('class','') == 'fileRows':
filename = td.text filename = td.text
#print "len"+str(len(td.getchildren()))
for slash in td.getchildren(): for slash in td.getchildren():
filename += '/' filename += '/'
filename += slash.tail filename += slash.tail
files.append(filename) files.append(filename)
#--------------------------TODO #--------------------------TODO
torrent = Torrent(id, title) torrent = Torrent(id, title)
torrent.url = url torrent.url = url
torrent.size = self.unit(size,u) torrent.size = get_bytes_size(size, u)
torrent.seeders = int(seed) torrent.seeders = int(seed)
torrent.leechers = int(leech) torrent.leechers = int(leech)
torrent.description = description torrent.description = description
torrent.files = files torrent.files = files
return torrent return torrent

View file

@ -31,10 +31,6 @@ class KickassBackend(BaseBackend, ICapTorrent):
VERSION = '0.5' VERSION = '0.5'
DESCRIPTION = 'kickasstorrent.com bittorrent tracker' DESCRIPTION = 'kickasstorrent.com bittorrent tracker'
LICENSE = 'GPLv3' LICENSE = 'GPLv3'
#CONFIG = ValuesDict(Value('domain', label='Domain (example "ssl.what.cd")'),
# Value('protocol', label='Protocol to use', choices=('http', 'https')),
# Value('username', label='Username'),
# Value('password', label='Password', masked=True))
BROWSER = KickassBrowser BROWSER = KickassBrowser
def create_default_browser(self): def create_default_browser(self):

View file

@ -30,43 +30,19 @@ class KickassBrowser(BaseBrowser):
ENCODING = 'utf-8' ENCODING = 'utf-8'
USER_AGENT = BaseBrowser.USER_AGENTS['wget'] USER_AGENT = BaseBrowser.USER_AGENTS['wget']
PAGES = { PAGES = {
'http://fr.kickasstorrents.com/new/.*field=seeders&sorder=desc' : TorrentsPage, 'http://fr.kickasstorrents.com/new/.*field=seeders&sorder=desc': TorrentsPage,
'http://fr.kickasstorrents.com/.*.html' : TorrentPage 'http://fr.kickasstorrents.com/.*.html': TorrentPage,
} }
def __init__(self, *args, **kwargs):
#self.DOMAIN = domain
#self.PROTOCOL = protocol
#self.PAGES = {}
#for key, value in PiratebayBrowser.PAGES.iteritems():
# self.PAGES[key % domain] = value
BaseBrowser.__init__(self, *args, **kwargs)
#def login(self):
# if not self.is_on_page(LoginPage):
# self.home()
# self.page.login(self.username, self.password)
#def is_logged(self):
# if not self.page or self.is_on_page(LoginPage):
# return False
# if self.is_on_page(IndexPage):
# return self.page.is_logged()
# return True
def home(self): def home(self):
return self.location('http://kickasstorrents.com') return self.location('http://kickasstorrents.com')
def iter_torrents(self, pattern): def iter_torrents(self, pattern):
#self.location(self.buildurl('/torrents.php', searchstr=pattern))
self.location('http://fr.kickasstorrents.com/new/?q=%s&field=seeders&sorder=desc' % pattern) self.location('http://fr.kickasstorrents.com/new/?q=%s&field=seeders&sorder=desc' % pattern)
assert self.is_on_page(TorrentsPage) assert self.is_on_page(TorrentsPage)
return self.page.iter_torrents() return self.page.iter_torrents()
def get_torrent(self, id): def get_torrent(self, id):
self.location('http://fr.kickasstorrents.com/%s.html' % id) self.location('http://fr.kickasstorrents.com/%s.html' % id)
assert self.is_on_page(TorrentPage) assert self.is_on_page(TorrentPage)
return self.page.get_torrent(id) return self.page.get_torrent(id)

View file

@ -16,28 +16,18 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.tools.browser import BasePage
from weboob.capabilities.torrent import Torrent from weboob.capabilities.torrent import Torrent
from weboob.tools.browser import BasePage
from weboob.tools.misc import get_bytes_size
__all__ = ['TorrentsPage'] __all__ = ['TorrentsPage']
class TorrentsPage(BasePage): class TorrentsPage(BasePage):
def unit(self, n, u):
m = {'bytes': 1,
'KB': 1024,
'MB': 1024*1024,
'GB': 1024*1024*1024,
'TB': 1024*1024*1024*1024,
}
return float(n*m[u])
def iter_torrents(self): def iter_torrents(self):
for tr in self.document.getiterator('tr'): for tr in self.document.getiterator('tr'):
if tr.attrib.get('class','') == 'odd' or tr.attrib.get('class','') == ' even': if tr.attrib.get('class', '') == 'odd' or tr.attrib.get('class', '') == ' even':
title = tr.getchildren()[0].getchildren()[1].getchildren()[1].text title = tr.getchildren()[0].getchildren()[1].getchildren()[1].text
if not title: if not title:
title = '' title = ''
@ -46,78 +36,67 @@ class TorrentsPage(BasePage):
title += red.text title += red.text
if red.tail: if red.tail:
title += red.tail title += red.tail
idt = tr.getchildren()[0].getchildren()[1].getchildren()[1].attrib.get('href','').replace('/','').replace('.html','') idt = tr.getchildren()[0].getchildren()[1].getchildren()[1].attrib.get('href', '').replace('/', '') \
url = tr.getchildren()[0].getchildren()[0].getchildren()[0].getchildren()[0].attrib.get('href','') .replace('.html', '')
url = tr.getchildren()[0].getchildren()[0].getchildren()[0].getchildren()[0].attrib.get('href', '')
size = tr.getchildren()[1].text size = tr.getchildren()[1].text
u = tr.getchildren()[1].getchildren()[0].text u = tr.getchildren()[1].getchildren()[0].text
size = size = size.replace(',','.') size = size = size.replace(',', '.')
size = float(size) size = float(size)
seed = tr.getchildren()[4].text seed = tr.getchildren()[4].text
leech = tr.getchildren()[5].text leech = tr.getchildren()[5].text
torrent = Torrent(idt, yield Torrent(idt,
title, title,
url=url, url=url,
size=self.unit(size,u), size=get_bytes_size(size, u),
seeders=int(seed), seeders=int(seed),
leechers=int(leech)) leechers=int(leech))
yield torrent
class TorrentPage(BasePage): class TorrentPage(BasePage):
def unit(self, n, u):
m = {'bytes': 1,
'KB': 1024,
'MB': 1024*1024,
'GB': 1024*1024*1024,
'TB': 1024*1024*1024*1024,
}
return float(n*m[u])
def get_torrent(self, id): def get_torrent(self, id):
seed = 0 seed = 0
leech = 0 leech = 0
description = "No description" description = 'No description'
url = 'No Url found' url = 'No Url found'
for div in self.document.getiterator('div'): for div in self.document.getiterator('div'):
if div.attrib.get('id','') == 'desc': if div.attrib.get('id', '') == 'desc':
description = div.text.strip() description = div.text.strip()
for ch in div.getchildren(): for ch in div.getchildren():
if ch.tail != None: if ch.tail != None:
description += ' '+ch.tail.strip() description += ' '+ch.tail.strip()
if div.attrib.get('class','') == 'seedBlock': if div.attrib.get('class', '') == 'seedBlock':
seed = int(div.getchildren()[1].text) seed = int(div.getchildren()[1].text)
if div.attrib.get('class','') == 'leechBlock': if div.attrib.get('class', '') == 'leechBlock':
leech = int(div.getchildren()[1].text) leech = int(div.getchildren()[1].text)
for h in self.document.getiterator('h1'): for h in self.document.getiterator('h1'):
if h.attrib.get('class','') == 'torrentName': if h.attrib.get('class', '') == 'torrentName':
title = h.getchildren()[0].getchildren()[0].text title = h.getchildren()[0].getchildren()[0].text
for a in self.document.getiterator('a'): for a in self.document.getiterator('a'):
if ('Download' in a.attrib.get('title','')) and ('torrent file' in a.attrib.get('title','')): if ('Download' in a.attrib.get('title', '')) and ('torrent file' in a.attrib.get('title', '')):
url = a.attrib.get('href','') url = a.attrib.get('href', '')
size = 0 size = 0
for span in self.document.getiterator('span'): for span in self.document.getiterator('span'):
if span.attrib.get('class','') == "folder" or span.attrib.get('class','') == "folderopen": if span.attrib.get('class', '') == 'folder' or span.attrib.get('class', '') == 'folderopen':
size = span.getchildren()[1].tail size = span.getchildren()[1].tail
u = span.getchildren()[2].text u = span.getchildren()[2].text
size = float(size.split(': ')[1].replace(',','.')) size = float(size.split(': ')[1].replace(',', '.'))
files = [] files = []
for td in self.document.getiterator('td'): for td in self.document.getiterator('td'):
if td.attrib.get('class','') == 'torFileName': if td.attrib.get('class', '') == 'torFileName':
files.append(td.text) files.append(td.text)
torrent = Torrent(id, title) torrent = Torrent(id, title)
torrent = Torrent(id, title) torrent = Torrent(id, title)
torrent.url = url torrent.url = url
torrent.size = self.unit(size,u) torrent.size = get_bytes_size(size, u)
torrent.seeders = int(seed) torrent.seeders = int(seed)
torrent.leechers = int(leech) torrent.leechers = int(leech)
torrent.description = description torrent.description = description
torrent.files = files torrent.files = files
return torrent return torrent

View file

@ -30,10 +30,11 @@ class LCLBrowser(BaseBrowser):
DOMAIN = 'particuliers.secure.lcl.fr' DOMAIN = 'particuliers.secure.lcl.fr'
ENCODING = 'utf-8' ENCODING = 'utf-8'
USER_AGENT = BaseBrowser.USER_AGENTS['wget'] USER_AGENT = BaseBrowser.USER_AGENTS['wget']
PAGES = {'https://particuliers.secure.lcl.fr/index.html': LoginPage, PAGES = {
'https://particuliers.secure.lcl.fr/everest/UWBI/UWBIAccueil\?DEST=IDENTIFICATION': LoginErrorPage, 'https://particuliers.secure.lcl.fr/index.html': LoginPage,
'https://particuliers.secure.lcl.fr/outil/UWSP/Synthese/accesSynthese': AccountsPage 'https://particuliers.secure.lcl.fr/everest/UWBI/UWBIAccueil\?DEST=IDENTIFICATION': LoginErrorPage,
} 'https://particuliers.secure.lcl.fr/outil/UWSP/Synthese/accesSynthese': AccountsPage,
}
def __init__(self, agency, *args, **kwargs): def __init__(self, agency, *args, **kwargs):
self.agency = agency self.agency = agency

View file

@ -15,6 +15,7 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.capabilities.weather import ICapWeather from weboob.capabilities.weather import ICapWeather
from weboob.tools.backend import BaseBackend from weboob.tools.backend import BaseBackend
@ -31,10 +32,6 @@ class MeteofranceBackend(BaseBackend, ICapWeather):
VERSION = '0.5' VERSION = '0.5'
DESCRIPTION = 'Get forecasts from the MeteoFrance website' DESCRIPTION = 'Get forecasts from the MeteoFrance website'
LICENSE = 'GPLv3' LICENSE = 'GPLv3'
#CONFIG = ValuesDict(Value('domain', label='Domain (example "ssl.what.cd")'),
# Value('protocol', label='Protocol to use', choices=('http', 'https')),
# Value('username', label='Username'),
# Value('password', label='Password', masked=True))
BROWSER = MeteofranceBrowser BROWSER = MeteofranceBrowser
def create_default_browser(self): def create_default_browser(self):

View file

@ -31,14 +31,16 @@ class MeteofranceBrowser(BaseBrowser):
PROTOCOL = 'http' PROTOCOL = 'http'
ENCODING = 'utf-8' ENCODING = 'utf-8'
USER_AGENT = BaseBrowser.USER_AGENTS['wget'] USER_AGENT = BaseBrowser.USER_AGENTS['wget']
WEATHER_URL = "{0}://{1}/france/meteo?PREVISIONS_PORTLET.path=previsionsville/{{cityid}}".format(PROTOCOL, DOMAIN) WEATHER_URL = '{0}://{1}/france/meteo?PREVISIONS_PORTLET.path=previsionsville/{{cityid}}'.format(PROTOCOL, DOMAIN)
CITY_SEARCH_URL="{0}://{1}/france/accueil/resultat?RECHERCHE_RESULTAT_PORTLET.path=rechercheresultat&query={{city_pattern}}&type=PREV_FRANCE&satellite=france".format(PROTOCOL, DOMAIN) CITY_SEARCH_URL = '{0}://{1}/france/accueil/resultat?RECHERCHE_RESULTAT_PORTLET.path=rechercheresultat&' \
'query={{city_pattern}}&type=PREV_FRANCE&satellite=france'.format(PROTOCOL, DOMAIN)
PAGES = { PAGES = {
WEATHER_URL.format(cityid=".*") : WeatherPage, WEATHER_URL.format(cityid=".*"): WeatherPage,
CITY_SEARCH_URL.format(city_pattern=".*") : CityPage, CITY_SEARCH_URL.format(city_pattern=".*"): CityPage,
"http://france.meteofrance.com/france/accueil/resultat.*" : CityPage, 'http://france.meteofrance.com/france/accueil/resultat.*': CityPage,
"http://france.meteofrance.com/france/meteo.*" : WeatherPage 'http://france.meteofrance.com/france/meteo.*': WeatherPage,
} }
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
BaseBrowser.__init__(self, *args, **kwargs) BaseBrowser.__init__(self, *args, **kwargs)

View file

@ -22,6 +22,7 @@ from weboob.capabilities.weather import Forecast, Current, City
import datetime import datetime
__all__ = ['WeatherPage', 'CityPage'] __all__ = ['WeatherPage', 'CityPage']
@ -60,16 +61,15 @@ class WeatherPage(BasePage):
mdate = datetime.datetime.now() mdate = datetime.datetime.now()
return Current(mdate, temp, mtxt, "C") return Current(mdate, temp, mtxt, "C")
def get_city(self): def get_city(self):
"""Return the city from the forecastpage """
Return the city from the forecastpage.
""" """
for div in self.document.getiterator('div'): for div in self.document.getiterator('div'):
if div.attrib.has_key("class") and div.attrib.get("class") == "choix": if div.attrib.has_key("class") and div.attrib.get("class") == "choix":
for strong in div.getiterator("strong"): for strong in div.getiterator("strong"):
city_name=strong.text +" "+ strong.tail.replace("(","").replace(")","") city_name=strong.text +" "+ strong.tail.replace("(","").replace(")","")
city_id=self.url.split("/")[-1] city_id=self.url.split("/")[-1]
return City(city_id, city_name) return City(city_id, city_name)

View file

@ -16,14 +16,68 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from logging import warning
from dateutil import tz from dateutil import tz
from logging import warning
import sys import sys
import traceback import traceback
import types import types
__all__ = ['to_unicode', 'local2utc', 'html2text', 'get_backtrace', 'iter_fields'] __all__ = ['get_backtrace', 'get_bytes_size', 'html2text', 'iter_fields', 'local2utc', 'to_unicode', 'utc2local']
def get_backtrace(empty="Empty backtrace."):
"""
Try to get backtrace as string.
Returns "Error while trying to get backtrace" on failure.
"""
try:
info = sys.exc_info()
trace = traceback.format_exception(*info)
sys.exc_clear()
if trace[0] != "None\n":
return "".join(trace)
except:
# No i18n here (imagine if i18n function calls error...)
return "Error while trying to get backtrace"
return empty
def get_bytes_size(size, unit_name):
unit_data = {
'bytes': 1,
'KB': 1024,
'MB': 1024 * 1024,
'GB': 1024 * 1024 * 1024,
'TB': 1024 * 1024 * 1024 * 1024,
}
return float(size * unit_data[unit_name])
try:
import html2text as h2t
h2t.UNICODE_SNOB = 1
h2t.SKIP_INTERNAL_LINKS = True
html2text = h2t.html2text
except ImportError:
warning('python-html2text is not present. HTML pages will not be converted into text.')
def html2text(s):
return s
def iter_fields(obj):
for attribute_name in dir(obj):
if attribute_name.startswith('_'):
continue
attribute = getattr(obj, attribute_name)
if not isinstance(attribute, types.MethodType):
yield attribute_name, attribute
def local2utc(d):
d = d.replace(tzinfo=tz.tzlocal())
d = d.astimezone(tz.tzutc())
return d
def to_unicode(text): def to_unicode(text):
@ -47,57 +101,8 @@ def to_unicode(text):
except UnicodeError: except UnicodeError:
return unicode(text, 'windows-1252') return unicode(text, 'windows-1252')
def local2utc(d):
d = d.replace(tzinfo=tz.tzlocal())
d = d.astimezone(tz.tzutc())
return d
def utc2local(d): def utc2local(d):
d = d.replace(tzinfo=tz.tzutc()) d = d.replace(tzinfo=tz.tzutc())
d = d.astimezone(tz.tzlocal()) d = d.astimezone(tz.tzlocal())
return d return d
try:
import html2text as h2t
h2t.UNICODE_SNOB = 1
h2t.SKIP_INTERNAL_LINKS = True
html2text = h2t.html2text
except ImportError:
warning('python-html2text is not present. HTML pages will not be converted into text.')
def html2text(s):
return s
def get_backtrace(empty="Empty backtrace."):
"""
Try to get backtrace as string.
Returns "Error while trying to get backtrace" on failure.
"""
try:
info = sys.exc_info()
trace = traceback.format_exception(*info)
sys.exc_clear()
if trace[0] != "None\n":
return "".join(trace)
except:
# No i18n here (imagine if i18n function calls error...)
return "Error while trying to get backtrace"
return empty
def iter_fields(obj):
for attribute_name in dir(obj):
if attribute_name.startswith('_'):
continue
attribute = getattr(obj, attribute_name)
if not isinstance(attribute, types.MethodType):
yield attribute_name, attribute
def iternb(it, nb=0):
"""
Iter 'nb' times on the generator
"""
i = 0
for v in it:
if i >= nb:
raise StopIteration()
yield v
i += 1