Fetching users from aspect works
authorMarek Marecki <marekjm@ozro.pw>
Sun, 23 Apr 2017 14:38:34 +0000 (16:38 +0200)
committerMarek Marecki <marekjm@ozro.pw>
Sun, 23 Apr 2017 14:38:34 +0000 (16:38 +0200)
diaspy/models.py

index c58f38bc52660c5c9e513616dfcbfb29f4334684..e8396b3c3307509e475e9cf9fb76432b6582a636 100644 (file)
@@ -22,63 +22,15 @@ class Aspect():
     def __init__(self, connection, id, name=None):
         self._connection = connection
         self.id, self.name = id, name
+        self._cached = []
 
-    def _getajax(self):
-        """Returns HTML returned when editing aspects via web UI.
-        """
-        start_regexp = re.compile('<ul +class=["\']contacts["\'] *>')
-        ajax = self._connection.get('aspects/{0}/edit'.format(self.id)).text
-
-        begin = ajax.find(start_regexp.search(ajax).group(0))
-        end = ajax.find('</ul>')
-        return ajax[begin:end]
-
-    def _extractusernames(self, ajax):
-        """Extracts usernames and GUIDs from ajax returned by Diaspora*.
-        Returns list of two-tuples: (guid, diaspora_name).
-        """
-        userline_regexp = re.compile('<a href=["\']/people/[a-z0-9]{16,16}["\']>[\w()*@. -]+</a>')
-        return [(line[17:33], re.escape(line[35:-4])) for line in userline_regexp.findall(ajax)]
-
-    def _extractpersonids(self, ajax, usernames):
-        """Extracts `person_id`s and usernames from ajax and list of usernames.
-        Returns list of two-tuples: (username, id)
-        """
-        personid_regexp = 'alt=["\']{0}["\'] class=["\']avatar["\'] data-person_id=["\'][0-9]+["\']'
-        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, ''
-            while line[i].isdigit():
-                id = line[i] + id
-                i -= 1
-            personids[n] = (usernames[n][1], id)
-        return personids
-
-    def _defineusers(self, ajax, personids):
-        """Gets users contained in this aspect by getting users who have `delete` method.
-        """
-        method_regexp = 'data-method="delete" data-person_id="{0}"'
-        users = []
-        for name, id in personids:
-            if re.compile(method_regexp.format(id)).search(ajax): users.append(name)
-        return users
-
-    def _getguids(self, users_in_aspect, usernames):
-        """Defines users contained in this aspect.
-        """
-        guids = []
-        for guid, name in usernames:
-            if name in users_in_aspect: guids.append(guid)
-        return guids
-
-    def getUsers(self):
+    def getUsers(self, fetch = True):
         """Returns list of GUIDs of users who are listed in this aspect.
         """
-        ajax = self._getajax()
-        usernames = self._extractusernames(ajax)
-        personids = self._extractpersonids(ajax, usernames)
-        users_in_aspect = self._defineusers(ajax, personids)
-        return self._getguids(users_in_aspect, usernames)
+        if fetch:
+            request = self._connection.get('contacts.json?a_id={}'.format(self.id))
+            self._cached = request.json()
+        return self._cached
 
     def addUser(self, user):
         """Add user to current aspect.