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
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('<ul +class=["\']contacts["\'] *>')
- userline_regexp = re.compile('<a href=["\']/people/[a-z0-9]{16,16}["\']>[a-zA-Z0-9()@. _-]+</a>')
- personid_regexp = 'alt=["\']{0}["\'] class=["\']avatar["\'] data-person_id=["\'][0-9]+["\']'
- method_regexp = 'data-method="delete" data-person_id="{0}"'
-
ajax = self._connection.get('aspects/{0}/edit'.format(self.id)).text
begin = ajax.find(start_regexp.search(ajax).group(0))
end = ajax.find('</ul>')
- 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 href=["\']/people/[a-z0-9]{16,16}["\']>[a-zA-Z0-9()@. _-]+</a>')
+ 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, ''
import re
from diaspy.streams import Outer
from diaspy.models import Aspect
+from diaspy import errors
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'),