From 65b1f0997d4f383962dd2c08a10bcc0b27d50837 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Thu, 4 Jul 2013 15:53:56 +0200 Subject: [PATCH] Improvements and changes in Aspect().getUsers() --- diaspy/errors.py | 6 ++++++ diaspy/models.py | 31 +++++++++++++++++++------------ diaspy/people.py | 3 ++- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/diaspy/errors.py b/diaspy/errors.py index e5a6167..90cc28d 100644 --- a/diaspy/errors.py +++ b/diaspy/errors.py @@ -16,6 +16,12 @@ class LoginError(DiaspyError): pass +class UserError(DiaspyError): + """Exception raised when something related to users goes wrong. + """ + pass + + def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError): """This method tries to decides how to react to a response code passed to it. If it's an diff --git a/diaspy/models.py b/diaspy/models.py index c412d51..0a7e3cd 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -49,25 +49,32 @@ class Aspect(): break return id - def getUsers(self): - """Returns list of GUIDs of users who are listed in this aspect. + def _getajax(self): + """Returns HTML returned when editing aspects via web UI. """ start_regexp = re.compile('') - ajax = ajax[begin:end] + return ajax[begin:end] + + def _extractusernames(self, ajax): + """Extracts usernames and GUIDs from ajax returned by Diaspora*. + Returns list of two-tuple: (guid, diaspora_name). + """ + userline_regexp = re.compile('[a-zA-Z0-9()@. _-]+') + return [(line[17:33], re.escape(line[35:-4])) for line in userline_regexp.findall(ajax)] + + def getUsers(self): + """Returns list of GUIDs of users who are listed in this aspect. + """ + personid_regexp = 'alt=["\']{0}["\'] class=["\']avatar["\'] data-person_id=["\'][0-9]+["\']' + method_regexp = 'data-method="delete" data-person_id="{0}"' + + ajax = self._getajax() + usernames = self._extractusernames(ajax) - usernames = [(line[17:33], line[35:-4]) for line in userline_regexp.findall(ajax)] - for i, data in enumerate(usernames): - guid, name = data - for char in ['(', ')', '.']: name = name.replace(char, '\\'+char) - usernames[i] = (guid, name) personids = [re.compile(personid_regexp.format(name)).search(ajax).group(0) for guid, name in usernames] for n, line in enumerate(personids): i, id = -2, '' diff --git a/diaspy/people.py b/diaspy/people.py index 8107ab2..2e94fab 100644 --- a/diaspy/people.py +++ b/diaspy/people.py @@ -1,6 +1,7 @@ import re from diaspy.streams import Outer from diaspy.models import Aspect +from diaspy import errors class User(): @@ -74,7 +75,7 @@ class User(): raise Exception('wrong error code: {0}'.format(request.status_code)) else: request = request.json() - if not len(request): raise Exception('Cannot extract user data: no posts to analyze') + if not len(request): raise errors.UserError('cannot extract user data: no posts to analyze') names = [('id', 'id'), ('diaspora_id', 'diaspora_id'), ('guid', 'guid'), -- 2.25.1