From 5de528034c6979fdbce9a4fe175a13ca33c32d1e Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Mon, 2 Sep 2013 16:30:53 +0200 Subject: [PATCH] First steps in making diaspy work with D* 0.2.0.0 --- Changelog.markdown | 2 ++ diaspy/connection.py | 29 +++++++++++++++++++++-------- diaspy/streams.py | 2 +- tests.py | 2 +- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/Changelog.markdown b/Changelog.markdown index f68ae1d..eaa2c1a 100644 --- a/Changelog.markdown +++ b/Changelog.markdown @@ -40,6 +40,7 @@ only token is left -- it can be obtained by calling `repr(Connection)`. * __new__: `downloadPhotos()` method in `diaspy.settings.Settings`, * __new__: `backtime` argument in `more()` method in `diaspy.streams.Generic`, * __new__: `DiaspyError` will be raised when connection is created with empty password and/or username, +* __new__: `getSessionToken()` method in `diaspy.connection.Connection`, * __upd__: if `Post()` is created with fetched comments, data will also be fetched as a dependency, * __upd__: `id` argument type is now `int` (`diaspy.models.Post.__init__()`), @@ -51,6 +52,7 @@ only token is left -- it can be obtained by calling `repr(Connection)`. * __fix__: fixed some bugs in regular expressions used by `diaspy` internals (html tag removal, so you get nicer notifications), +* __fix__: fixed authentication issues, ---- diff --git a/diaspy/connection.py b/diaspy/connection.py index 001e215..51f3449 100644 --- a/diaspy/connection.py +++ b/diaspy/connection.py @@ -17,6 +17,7 @@ class Connection(): """ _token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token') _userinfo_regex = re.compile(r'window.current_user_attributes = ({.*})') + _userinfo_regex_2 = re.compile(r'gon.user=({.*});gon.preloads') def __init__(self, pod, username='', password='', schema='https'): """ @@ -30,7 +31,9 @@ class Connection(): self.pod = pod self._session = requests.Session() self._login_data = {} + self._userdata = {} self._token = '' + self._diaspora_session = '' try: self._setlogin(username, password) except requests.exceptions.MissingSchema: @@ -120,9 +123,10 @@ class Connection(): """ request = self.post('users/sign_in', data=self._login_data, - headers={'accept': 'application/json'}) - if request.status_code != 201: + headers={'accept': 'application/json,text/html'}) + if request.status_code not in [200, 201]: raise errors.LoginError('{0}: login failed'.format(request.status_code)) + print(request.headers) def login(self, username='', password=''): """This function is used to log in to a pod. @@ -148,16 +152,20 @@ class Connection(): self._setlogin(username, password) self._login() - def getUserInfo(self): + def getUserInfo(self, fetch=False): """This function returns the current user's attributes. :returns: dict -- json formatted user info. """ - request = self.get('bookmarklet') - userdata = self._userinfo_regex.search(request.text) - if userdata is None: raise errors.DiaspyError('cannot find user data') - userdata = json.loads(userdata.group(1)) - return userdata + if self._userdata == {} or fetch: + request = self.get('bookmarklet') + userdata = self._userinfo_regex.search(request.text) + if userdata is None: userdata = self._userinfo_regex_2.search(request.text) + if userdata is None: raise errors.DiaspyError('cannot find user data') + userdata = userdata.group(1) + print(userdata) + self._userdata = json.loads(userdata) + return self._userdata def _fetchtoken(self): """This method tries to get token string needed for authentication on D*. @@ -183,3 +191,8 @@ class Connection(): finally: if not self._token: raise errors.TokenError('cannot obtain token and no previous token found for reuse') return self._token + + def getSessionToken(self): + """Returns session token string (_diaspora_session). + """ + return self._diaspora_session diff --git a/diaspy/streams.py b/diaspy/streams.py index 64cab0e..d6ffaed 100644 --- a/diaspy/streams.py +++ b/diaspy/streams.py @@ -58,7 +58,7 @@ class Generic(): if max_time: params['max_time'] = max_time params['_'] = int(time.time() * 1000) - request = self._connection.get(self._location, params=params) + request = self._connection.get(self._location, params=params, headers={'cookie': ''}) if request.status_code != 200: raise errors.StreamError('wrong status code: {0}'.format(request.status_code)) return [Post(self._connection, post['id']) for post in request.json()] diff --git a/tests.py b/tests.py index 593f075..500a62f 100644 --- a/tests.py +++ b/tests.py @@ -202,7 +202,7 @@ class PostTests(unittest.TestCase): class NotificationsTests(unittest.TestCase): - def testMarkgingRead(self): + def testMarkingRead(self): notifications = diaspy.notifications.Notifications(test_connection) notif = None for n in notifications: -- 2.25.1