From 4cde2ef3d53e7dd9998849a9c3d1b827af40cfa9 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Tue, 9 Feb 2010 23:58:47 +0100 Subject: [PATCH] exceptions, firefox_cookies and base page --- dlfp/__init__.py | 21 +++++++ dlfp/browser.py | 11 ---- dlfp/exceptions.py | 31 +++++++++++ dlfp/firefox_cookies.py | 120 ++++++++++++++++++++++++++++++++++++++++ dlfp/pages/__init__.py | 0 dlfp/pages/base.py | 29 ++++++++++ 6 files changed, 201 insertions(+), 11 deletions(-) create mode 100644 dlfp/__init__.py create mode 100644 dlfp/exceptions.py create mode 100644 dlfp/firefox_cookies.py create mode 100644 dlfp/pages/__init__.py create mode 100644 dlfp/pages/base.py diff --git a/dlfp/__init__.py b/dlfp/__init__.py new file mode 100644 index 0000000000..16b432bc20 --- /dev/null +++ b/dlfp/__init__.py @@ -0,0 +1,21 @@ +# -*- 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. + +""" + +from .browser import DLFP diff --git a/dlfp/browser.py b/dlfp/browser.py index 33bd761506..7b76a325c0 100644 --- a/dlfp/browser.py +++ b/dlfp/browser.py @@ -85,16 +85,6 @@ def inner(self, *args, **kwargs): def keepalive(self): self.home() - def change_location(func): - def inner(self, *args, **kwargs): - if args and args[0][0] == '/' and (not self.request or self.request.host != 'www.adopteunmec.com'): - args = ('http://www.adopteunmec.com%s' % args[0],) + args[1:] - print args - - return func(self, *args, **kwargs) - return inner - - @change_location def openurl(self, *args, **kwargs): try: return Browser.open(self, *args, **kwargs) @@ -130,7 +120,6 @@ def follow_link(self, *args, **kwargs): self.home() raise DLFPUnavailable() - @change_location def location(self, *args, **kwargs): keep_args = copy(args) keep_kwargs = kwargs.copy() diff --git a/dlfp/exceptions.py b/dlfp/exceptions.py new file mode 100644 index 0000000000..bb602f151b --- /dev/null +++ b/dlfp/exceptions.py @@ -0,0 +1,31 @@ +# -*- 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. + +""" + +class DLFPUnavailable(Exception): + pass + +class DLFPBanned(DLFPUnavailable): + pass + +class DLFPIncorrectPassword(Exception): + pass + +class DLFPRetry(Exception): + pass diff --git a/dlfp/firefox_cookies.py b/dlfp/firefox_cookies.py new file mode 100644 index 0000000000..75e477e1b9 --- /dev/null +++ b/dlfp/firefox_cookies.py @@ -0,0 +1,120 @@ +# -*- 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. + +""" + +try: + import sqlite3 as sqlite +except ImportError, e: + from pysqlite2 import dbapi2 as sqlite + +from mechanize import CookieJar, Cookie + +#import sys, logging +#logger = logging.getLogger("mechanize") +#logger.addHandler(logging.StreamHandler(sys.stdout)) +#logger.setLevel(logging.DEBUG) + +class FirefoxCookieJar(CookieJar): + + def __init__(self, sqlite_file=None, policy=None): + + CookieJar.__init__(self, policy) + + self.sqlite_file = sqlite_file + + def __connect(self): + try: + db = sqlite.connect(database=self.sqlite_file, timeout=10.0) + except sqlite.OperationalError, err: + print 'Unable to open %s database: %s' % (self.sqlite_file, err) + return None + + return db + + + def load(self): + + db = self.__connect() + if not db: return + + cookies = db.execute("""SELECT host, path, name, value, expiry, lastAccessed, isSecure + FROM moz_cookies + WHERE host LIKE '%linuxfr%'""") + + for entry in cookies: + + domain = entry[0] + initial_dot = domain.startswith(".") + domain_specified = initial_dot + path = entry[1] + name = entry[2] + value = entry[3] + expires = entry[4] + secure = entry[6] + + discard = False + + c = Cookie(0, name, value, + None, False, + domain, domain_specified, initial_dot, + path, False, + secure, + expires, + discard, + None, + None, + {}) + #if not ignore_discard and c.discard: + # continue + #if not ignore_expires and c.is_expired(now): + # continue + self.set_cookie(c) + + def save(self): + + db = self.__connect() + if not db: return + + db.execute("DELETE FROM moz_cookies WHERE host LIKE '%linuxfr%'") + for cookie in self: + if cookie.secure: secure = 1 + else: secure = 0 + if cookie.expires is not None: + expires = cookie.expires + else: + expires = 0 + + if cookie.value is None: + # cookies.txt regards 'Set-Cookie: foo' as a cookie + # with no name, whereas cookielib regards it as a + # cookie with no value. + name = "" + value = cookie.name + else: + name = cookie.name + value = cookie.value + + # XXX ugly hack to keep this cookie + if name == 'PHPSESSID': + expires = 1854242393 + + db.execute("""INSERT INTO moz_cookies (host, path, name, value, expiry, isSecure) + VALUES (?, ?, ?, ?, ?, ?)""", + (cookie.domain, cookie.path, name, value, int(expires), int(secure))) + db.commit() diff --git a/dlfp/pages/__init__.py b/dlfp/pages/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/dlfp/pages/base.py b/dlfp/pages/base.py new file mode 100644 index 0000000000..1d324dab7b --- /dev/null +++ b/dlfp/pages/base.py @@ -0,0 +1,29 @@ +# -*- 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. + +""" + +class PageBase: + + def __init__(self, dlfp, document, url=''): + self.dlfp = dlfp + self.document = document + self.url = url + + def loaded(self): + pass -- GitLab