[weboob] [PATCH 1/2] gdcvault: implement login

François Revol revol at free.fr
Sun Mar 17 22:30:51 CET 2013


* since there is a limited number of usable concurrent sessions
we force a logout on exit.

Signed-off-by: François Revol <revol at free.fr>
---
 modules/gdcvault/backend.py |   21 +++++++++++++++++-
 modules/gdcvault/browser.py |   51 ++++++++++++++++++++++++++++++++++++++++---
 modules/gdcvault/pages.py   |   14 ++++++++++--
 3 files changed, 80 insertions(+), 6 deletions(-)

diff --git a/modules/gdcvault/backend.py b/modules/gdcvault/backend.py
index f311420..cdb827b 100644
--- a/modules/gdcvault/backend.py
+++ b/modules/gdcvault/backend.py
@@ -22,8 +22,9 @@
 from __future__ import with_statement
 
 from weboob.capabilities.video import ICapVideo, BaseVideo
-from weboob.tools.backend import BaseBackend
+from weboob.tools.backend import BaseBackend, BackendConfig
 from weboob.capabilities.collection import ICapCollection, CollectionNotFound
+from weboob.tools.value import Value, ValueBackendPassword
 
 from .browser import GDCVaultBrowser
 from .video import GDCVaultVideo
@@ -40,6 +41,24 @@ class GDCVaultBackend(BaseBackend, ICapVideo, ICapCollection):
     DESCRIPTION = 'Game Developers Conferences Vault video streaming website'
     LICENSE = 'AGPLv3+'
     BROWSER = GDCVaultBrowser
+    CONFIG = BackendConfig(Value('username',                  label='Username', default=''),
+                           ValueBackendPassword('password',   label='Password', default=''))
+
+    def create_default_browser(self):
+        username = self.config['username'].get()
+        if len(username) > 0:
+            password = self.config['password'].get()
+        else:
+            password = None
+        return self.create_browser(username, password)
+
+    def deinit(self):
+        # don't need to logout if the browser hasn't been used.
+        if not self._browser:
+            return
+
+        with self.browser:
+            self.browser.close_session()
 
     def get_video(self, _id):
         with self.browser:
diff --git a/modules/gdcvault/browser.py b/modules/gdcvault/browser.py
index a4f5d97..2aeb224 100644
--- a/modules/gdcvault/browser.py
+++ b/modules/gdcvault/browser.py
@@ -18,11 +18,13 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with weboob. If not, see <http://www.gnu.org/licenses/>.
 
-from weboob.tools.browser import BaseBrowser
+import urllib
+
+from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword, BrowserUnavailable
 from weboob.tools.browser.decorators import id2url
 
 #from .pages.index import IndexPage
-from .pages import VideoPage
+from .pages import VideoPage, IndexPage
 from .video import GDCVaultVideo
 
 
@@ -31,13 +33,56 @@ __all__ = ['GDCVaultBrowser']
 
 class GDCVaultBrowser(BaseBrowser):
     DOMAIN = 'gdcvault.com'
-    ENCODING = None
+    ENCODING = 'utf-8'
     PAGES = {r'http://[w\.]*gdcvault.com/play/(?P<id>[\d]+)/?.*': VideoPage,
+             r'http://[w\.]*gdcvault.com/': IndexPage,
             }
 
+    def is_logged(self):
+        if self.password is None:
+            return True
+
+        if not self.page:
+            return False
+
+        obj = self.parser.select(self.page.document.getroot(), 'h3[id=welcome_user_name]', 1)
+        if obj is None:
+            return False
+
+        return obj.attrib.get('class','') != "hidden"
+
+    def login(self):
+        if self.password is None:
+            return
+
+        params = {'remember_me': 0,
+                  'email':       self.username,
+                  'password':    self.password,
+                 }
+
+        data = self.readurl('http://gdcvault.com/api/login.php', 
+                            urllib.urlencode(params))
+        # data is returned as JSON, not sure yet if it's useful
+
+        print data
+        if data is None:
+            raise BrowserBanned('Too many open sessions?')
+
+        self.location('/', no_login=True)
+
+        if not self.is_logged():
+            raise BrowserIncorrectPassword()
+
+    def close_session(self):
+        print "logging out..."
+        self.openurl('/logout', '')
+
     @id2url(GDCVaultVideo.id2url)
     def get_video(self, url, video=None):
         self.location(url)
+        # redirects to /login means the video is not public
+        if not isinstance(self.page, VideoPage):
+            raise BrowserUnavailable('Requires account')
         return self.page.get_video(video)
 
     # def search_videos(self, pattern, sortby):
diff --git a/modules/gdcvault/pages.py b/modules/gdcvault/pages.py
index 9df3663..8cff3c3 100644
--- a/modules/gdcvault/pages.py
+++ b/modules/gdcvault/pages.py
@@ -35,8 +35,18 @@ from .video import GDCVaultVideo
 #import lxml.etree
 
 
-__all__ = ['VideoPage']
-
+__all__ = ['IndexPage', 'VideoPage']
+
+
+class IndexPage(BasePage):
+    def iter_videos(self):
+        for a in self.parser.select(self.document.getroot(), 'section.conference ul.media_items li.featured a.session_item'):
+            print a
+            #m = re.match('id-(\d+)', a.attrib.get('class', ''))
+            #if not m:
+            #    continue
+            # FIXME
+            yield None
 
 class VideoPage(BasePage):
     def get_video(self, video=None):
-- 
1.7.10.4



More information about the weboob mailing list