From: Marek Marecki Date: Sun, 7 Jul 2013 14:37:36 +0000 (+0200) Subject: Moved `User().fetchprofile()` functionality to `Search().users()` X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=7c6fbe5b6cff3ddf1f3120fcda37fcc70675cdc5;p=diaspy.git Moved `User().fetchprofile()` functionality to `Search().users()` --- diff --git a/diaspy/errors.py b/diaspy/errors.py index 90cc28d..272e763 100644 --- a/diaspy/errors.py +++ b/diaspy/errors.py @@ -22,6 +22,10 @@ class UserError(DiaspyError): pass +class SearchError(DiaspyError): + 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/people.py b/diaspy/people.py index 0a73657..f3d0048 100644 --- a/diaspy/people.py +++ b/diaspy/people.py @@ -2,6 +2,7 @@ import re from diaspy.streams import Outer from diaspy.models import Aspect from diaspy import errors +from diaspy import search class User(): @@ -49,7 +50,7 @@ class User(): elif self['guid'] and not self['handle']: self.fetchguid() elif self['handle'] and not self['guid']: self.fetchhandle() elif fetch == 'data' and len(self['handle']): - self.fetchprofile() + self._postproc(search.Search(self._connection).users(query=handle)[0]) def _sephandle(self): """Separate D* handle into pod pod and user. @@ -62,7 +63,13 @@ class User(): pod, user = handle[1], handle[0] return (pod, user) - def _finalize_data(self, data, names): + def _finalize_data(self, data): + names = [('id', 'id'), + ('handle', 'diaspora_id'), + ('guid', 'guid'), + ('name', 'diaspora_name'), + ('avatar', 'image_urls'), + ] final = {} for d, f in names: final[f] = data[d] @@ -80,13 +87,7 @@ class User(): else: request = request.json() if not len(request): raise errors.UserError('cannot extract user data: no posts to analyze') - names = [('id', 'id'), - ('diaspora_id', 'diaspora_id'), - ('guid', 'guid'), - ('name', 'diaspora_name'), - ('avatar', 'image_urls'), - ] - self.data = self._finalize_data(request[0]['author'], names) + self.data = self._finalize_data(request[0]['author']) self.stream = Outer(self._connection, location='people/{0}.json'.format(self['guid'])) def fetchhandle(self, protocol='https'): @@ -102,23 +103,6 @@ class User(): request = self._connection.get('people/{0}.json'.format(self['guid'])) self._postproc(request) - def fetchprofile(self, protocol='https'): - """Fetch user data using Diaspora handle. - """ - request = self._connection.get('people.json?q={0}'.format(self['handle'])) - if request.status_code != 200: - raise Exception('wrong error code: {0}'.format(request.status_code)) - else: - request = request.json() - if len(request): - names = [('id', 'id'), - ('handle', 'diaspora_id'), - ('guid', 'guid'), - ('name', 'diaspora_name'), - ('avatar', 'image_urls'), - ] - self.data = self._finalize_data(request[0], names) - class Contacts(): """This class represents user's list of contacts. diff --git a/diaspy/search.py b/diaspy/search.py index 3fd0d08..3c66ec5 100644 --- a/diaspy/search.py +++ b/diaspy/search.py @@ -20,10 +20,14 @@ class Search(): request = self._connection.get('people', headers={'accept': 'text/html'}, params={'q': handle}) return request.status_code - def _query(self, query): - """Sends search query to pod. - - :param query: search query - :type query: str + def users(self, query): + """Searches for a user. + Will return list of dictionaries containing + data of found users. """ - pass + request = self._connection.get('people.json', params={'q': query, 'utf-8': ''}) + if request.status_code == 200: + result = request.json() + else: + raise errors.SearchError('wrong status code: {0}'.format(request.status_code)) + return result