new API in BaseBackend to create browser
This commit is contained in:
parent
2b93eec3c9
commit
c1279aa496
12 changed files with 84 additions and 87 deletions
|
|
@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|||
"""
|
||||
|
||||
import re
|
||||
import os
|
||||
from threading import RLock
|
||||
|
||||
|
||||
|
|
@ -66,6 +67,8 @@ class BaseBackend(object):
|
|||
CONFIG = {}
|
||||
# Storage
|
||||
STORAGE = {}
|
||||
# Browser class
|
||||
BROWSER = None
|
||||
|
||||
class ConfigField(object):
|
||||
def __init__(self, default=None, is_masked=False, regexp=None, description=None):
|
||||
|
|
@ -89,6 +92,11 @@ class BaseBackend(object):
|
|||
self.weboob = weboob
|
||||
self.name = name
|
||||
self.lock = RLock()
|
||||
|
||||
# Private fields (which start with '_')
|
||||
self._private_config = dict([(key, value) for key, value in config.iteritems() if key.startswith('_')])
|
||||
|
||||
# Configuration of backend
|
||||
self.config = {}
|
||||
for name, field in self.CONFIG.iteritems():
|
||||
value = config.get(name, field.default)
|
||||
|
|
@ -110,6 +118,40 @@ class BaseBackend(object):
|
|||
self.storage = BackendStorage(self.name, storage)
|
||||
self.storage.load(self.STORAGE)
|
||||
|
||||
@property
|
||||
def browser(self):
|
||||
"""
|
||||
Attribute 'browser'. The browser is created at the first call
|
||||
of this attribute, to avoid useless pages access.
|
||||
|
||||
Note that the 'default_browser' method is called to create it.
|
||||
"""
|
||||
if not hasattr(self, '_browser'):
|
||||
self._browser = self.default_browser()
|
||||
return self._browser
|
||||
|
||||
def default_browser(self):
|
||||
"""
|
||||
Method to overload to build the default browser in
|
||||
attribute 'browser'.
|
||||
"""
|
||||
return self.build_browser()
|
||||
|
||||
def build_browser(self, *args, **kwargs):
|
||||
"""
|
||||
Build a browser from the BROWSER class attribute and the
|
||||
given arguments.
|
||||
"""
|
||||
if not self.BROWSER:
|
||||
return None
|
||||
|
||||
if '_proxy' in self._private_config:
|
||||
kwargs['proxy'] = self._private_config['_proxy']
|
||||
elif 'HTTP_PROXY' in os.environ:
|
||||
kwargs['proxy'] = os.environ['HTTP_PROXY']
|
||||
|
||||
return self.BROWSER(*args, **kwargs)
|
||||
|
||||
def has_caps(self, *caps):
|
||||
for c in caps:
|
||||
if isinstance(self, c):
|
||||
|
|
|
|||
|
|
@ -47,18 +47,14 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesReply, ICapDating, ICapC
|
|||
'password': BaseBackend.ConfigField(description='Password of account', is_masked=True),
|
||||
}
|
||||
STORAGE = {'profiles_walker': {'viewed': []} }
|
||||
BROWSER = AdopteUnMec
|
||||
|
||||
def default_browser(self):
|
||||
return self.build_browser(self.config['username'], self.config['password'])
|
||||
|
||||
# Private
|
||||
_browser = None
|
||||
_profiles_walker = None
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name == 'browser':
|
||||
if not self._browser:
|
||||
self._browser = AdopteUnMec(self.config['username'], self.config['password'])
|
||||
return self._browser
|
||||
raise AttributeError, name
|
||||
|
||||
def iter_messages(self, thread=None):
|
||||
for message in self._iter_messages(thread, False):
|
||||
yield message
|
||||
|
|
|
|||
|
|
@ -34,22 +34,15 @@ class BNPorcBackend(BaseBackend, ICapBank):
|
|||
CONFIG = {'login': BaseBackend.ConfigField(description='Account ID'),
|
||||
'password': BaseBackend.ConfigField(description='Password of account', is_masked=True)
|
||||
}
|
||||
browser = None
|
||||
BROWSER = BNPorc
|
||||
|
||||
def need_browser(func):
|
||||
def inner(self, *args, **kwargs):
|
||||
if not self.browser:
|
||||
self.browser = BNPorc(self.config['login'], self.config['password'])
|
||||
def default_browser(self):
|
||||
return self.build_browser(self.config['login'], self.config['password'])
|
||||
|
||||
return func(self, *args, **kwargs)
|
||||
return inner
|
||||
|
||||
@need_browser
|
||||
def iter_accounts(self):
|
||||
for account in self.browser.get_accounts_list():
|
||||
yield account
|
||||
|
||||
@need_browser
|
||||
def get_account(self, _id):
|
||||
try:
|
||||
_id = long(_id)
|
||||
|
|
@ -62,7 +55,6 @@ class BNPorcBackend(BaseBackend, ICapBank):
|
|||
else:
|
||||
raise AccountNotFound()
|
||||
|
||||
@need_browser
|
||||
def iter_operations(self, account):
|
||||
for coming in self.browser.get_coming_operations(account):
|
||||
yield coming
|
||||
|
|
|
|||
|
|
@ -30,15 +30,14 @@ class CanalTPBackend(BaseBackend, ICapTravel):
|
|||
VERSION = '1.0'
|
||||
LICENSE = 'GPLv3'
|
||||
DESCRIPTION = "French trains"
|
||||
BROWSER = CanalTP
|
||||
|
||||
def iter_station_search(self, pattern):
|
||||
canaltp = CanalTP()
|
||||
for _id, name in canaltp.iter_station_search(pattern):
|
||||
for _id, name in self.browser.iter_station_search(pattern):
|
||||
yield Station(_id, name)
|
||||
|
||||
def iter_station_departures(self, station_id, arrival_id=None):
|
||||
canaltp = CanalTP()
|
||||
for i, d in enumerate(canaltp.iter_station_departures(station_id, arrival_id)):
|
||||
for i, d in enumerate(self.browser.iter_station_departures(station_id, arrival_id)):
|
||||
departure = Departure(i, d['type'], d['time'])
|
||||
departure.departure_station = d['departure']
|
||||
departure.arrival_station = d['arrival']
|
||||
|
|
|
|||
|
|
@ -35,22 +35,15 @@ class CragrBackend(BaseBackend, ICapBank):
|
|||
'password': BaseBackend.ConfigField(description='Password of account', is_masked=True),
|
||||
'website': BaseBackend.ConfigField(description='What website to use', default='m.lefil.com'),
|
||||
}
|
||||
browser = None
|
||||
BROWSER = Cragr
|
||||
|
||||
def need_browser(func):
|
||||
def inner(self, *args, **kwargs):
|
||||
if not self.browser:
|
||||
self.browser = Cragr(self.config['website'], self.config['login'], self.config['password'])
|
||||
def default_browser(self):
|
||||
return self.build_browser(self.config['website'], self.config['login'], self.config['password'])
|
||||
|
||||
return func(self, *args, **kwargs)
|
||||
return inner
|
||||
|
||||
@need_browser
|
||||
def iter_accounts(self):
|
||||
for account in self.browser.get_accounts_list():
|
||||
yield account
|
||||
|
||||
@need_browser
|
||||
def get_account(self, _id):
|
||||
try:
|
||||
_id = long(_id)
|
||||
|
|
@ -63,7 +56,6 @@ class CragrBackend(BaseBackend, ICapBank):
|
|||
else:
|
||||
raise AccountNotFound()
|
||||
|
||||
@need_browser
|
||||
def iter_operations(self, account):
|
||||
""" Not supported yet """
|
||||
return iter([])
|
||||
|
|
|
|||
|
|
@ -39,14 +39,10 @@ class DLFPBackend(BaseBackend, ICapMessages, ICapMessagesReply):
|
|||
'get_telegrams': BaseBackend.ConfigField(default=False, description='Get telegrams'),
|
||||
}
|
||||
STORAGE = {'seen': {}}
|
||||
_browser = None
|
||||
BROWSER = DLFP
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name == 'browser':
|
||||
if not self._browser:
|
||||
self._browser = DLFP(self.config['username'], self.config['password'])
|
||||
return self._browser
|
||||
raise AttributeError, name
|
||||
def default_browser(self):
|
||||
return self.build_browser(self.config['username'], self.config['password'])
|
||||
|
||||
def iter_messages(self, thread=None):
|
||||
return self._iter_messages(thread, False)
|
||||
|
|
|
|||
|
|
@ -40,15 +40,11 @@ class GazelleBackend(BaseBackend, ICapTorrent):
|
|||
'protocol': BaseBackend.ConfigField(description='Protocol to use ("http" or "https")'),
|
||||
'domain': BaseBackend.ConfigField(description='Domain (example "ssl.what.cd")'),
|
||||
}
|
||||
_browser = None
|
||||
BROWSER = GazelleBrowser
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name == 'browser':
|
||||
if not self._browser:
|
||||
self._browser = GazelleBrowser(self.config['protocol'], self.config['domain'],
|
||||
self.config['username'], self.config['password'])
|
||||
return self._browser
|
||||
raise AttributeError, name
|
||||
def default_browser(self):
|
||||
return self.build_browser(self.config['protocol'], self.config['domain'],
|
||||
self.config['username'], self.config['password'])
|
||||
|
||||
def get_torrent(self, id):
|
||||
return self.browser.get_torrent(id)
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ class TransilienBackend(BaseBackend, ICapTravel):
|
|||
VERSION = '1.0'
|
||||
LICENSE = 'GPLv3'
|
||||
DESCRIPTION = "Transports in Paris"
|
||||
BROWSER = Transilien
|
||||
|
||||
def iter_station_search(self, pattern):
|
||||
pattern = pattern.lower()
|
||||
|
|
@ -39,8 +40,7 @@ class TransilienBackend(BaseBackend, ICapTravel):
|
|||
yield Station(_id, name)
|
||||
|
||||
def iter_station_departures(self, station_id, arrival_id=None):
|
||||
transilien = Transilien()
|
||||
for i, d in enumerate(transilien.iter_station_departures(station_id, arrival_id)):
|
||||
for i, d in enumerate(self.browser.iter_station_departures(station_id, arrival_id)):
|
||||
departure = Departure(i, d['type'], d['time'])
|
||||
departure.departure_station = d['departure']
|
||||
departure.arrival_station = d['arrival']
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright(C) 2010 Roger Philibert
|
||||
#
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, version 3 of the License.
|
||||
#
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
|
@ -35,16 +35,9 @@ class YoujizzBackend(BaseBackend, ICapVideoProvider):
|
|||
LICENSE = 'GPLv3'
|
||||
|
||||
CONFIG = {}
|
||||
_browser = None
|
||||
BROWSER = YoujizzBrowser
|
||||
domain = u'youjizz.com'
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name == 'browser':
|
||||
if not self._browser:
|
||||
self._browser = YoujizzBrowser()
|
||||
return self._browser
|
||||
raise AttributeError, name
|
||||
|
||||
@id2url(domain, YoujizzVideo.id2url)
|
||||
def get_video(self, _id):
|
||||
return self.browser.get_video(_id)
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright(C) 2010 Romain Bignon
|
||||
#
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, version 3 of the License.
|
||||
#
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
|
@ -35,16 +35,9 @@ class YoupornBackend(BaseBackend, ICapVideoProvider):
|
|||
LICENSE = 'GPLv3'
|
||||
|
||||
CONFIG = {}
|
||||
_browser = None
|
||||
BROWSER = YoupornBrowser
|
||||
domain = u'youporn.com'
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name == 'browser':
|
||||
if not self._browser:
|
||||
self._browser = YoupornBrowser()
|
||||
return self._browser
|
||||
raise AttributeError, name
|
||||
|
||||
@id2url(domain, YoupornVideo.id2url)
|
||||
def get_video(self, _id):
|
||||
return self.browser.get_video(_id)
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright(C) 2010 Christophe Benz, Romain Bignon
|
||||
#
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, version 3 of the License.
|
||||
#
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
|
@ -37,16 +37,9 @@ class YoutubeBackend(BaseBackend, ICapVideoProvider):
|
|||
LICENSE = 'GPLv3'
|
||||
|
||||
CONFIG = {}
|
||||
_browser = None
|
||||
BROWSER = YoutubeBrowser
|
||||
domain = u'youtube.com'
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name == 'browser':
|
||||
if not self._browser:
|
||||
self._browser = YoutubeBrowser()
|
||||
return self._browser
|
||||
raise AttributeError, name
|
||||
|
||||
@id2url(domain, YoutubeVideo.id2url)
|
||||
def get_video(self, _id):
|
||||
return self.browser.get_video(_id)
|
||||
|
|
|
|||
|
|
@ -148,7 +148,12 @@ class BaseBrowser(mechanize.Browser):
|
|||
|
||||
# Use a proxy
|
||||
if proxy:
|
||||
self.set_proxies({"http": proxy})
|
||||
proto = 'http'
|
||||
if proxy.find('://') >= 0:
|
||||
proto, domain = proxy.split('://', 1)
|
||||
else:
|
||||
domain = proxy
|
||||
self.set_proxies({proto: domain})
|
||||
|
||||
# Share cookies with firefox
|
||||
if firefox_cookies and HAVE_COOKIES:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue