From beaa09fbad6ec96fdde04e8bc8dc86bf42c2f445 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Fri, 3 May 2013 20:41:12 +0200 Subject: [PATCH] New `diaspy.people.User()` object (plus manual) --- Makefile | 6 ++++- diaspy/__init__.py | 7 +----- diaspy/people.py | 56 +++++++++++++++++++++++++++++++++++++++++++++ diaspy/streams.py | 13 +++++++++++ manual/people.mdown | 45 ++++++++++++++++++++++++++++++++++++ tests.py | 2 +- 6 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 diaspy/people.py create mode 100644 manual/people.mdown diff --git a/Makefile b/Makefile index c085927..34c92e1 100644 --- a/Makefile +++ b/Makefile @@ -7,4 +7,8 @@ test: python3 -m unittest --verbose --catch --failfast tests.py test-python2: - python3 -m unittest --verbose --catch --failfast tests.py + python2 -m unittest --verbose --catch --failfast tests.py + +clean: + rm -rv {*/,}__pycache__/ + rm -rv {*/,}*.pyc diff --git a/diaspy/__init__.py b/diaspy/__init__.py index c824fa5..ace13e2 100644 --- a/diaspy/__init__.py +++ b/diaspy/__init__.py @@ -2,9 +2,4 @@ import diaspy.models as models import diaspy.conversations as conversations import diaspy.streams as streams import diaspy.client as client -""" -from diaspy import client -from diaspy import models -from diaspy import conversations -from diaspy import streams -""" +import diaspy.people as people diff --git a/diaspy/people.py b/diaspy/people.py new file mode 100644 index 0000000..f3756e3 --- /dev/null +++ b/diaspy/people.py @@ -0,0 +1,56 @@ +import re +from diaspy.streams import Outer + + +class User: + """This class abstracts a D* user. + This object goes around the limitations of current D* API and will + extract user data using black magic. + However, no chickens are harmed when you use it. + """ + def __init__(self, connection): + self._connection = connection + self.data = {} + + def __getitem__(self, key): + return self.data[key] + + def _sephandle(self, handle): + """Separate D* handle into pod pod and user. + + :param handle: diaspora id: user@pod.example.com + :type handle: str + :returns: two-tuple (pod, user) + """ + if re.match('^[a-zA-Z]+[a-zA-Z0-9_-]*@[a-z0-9.]+\.[a-z]+$', handle) is None: + raise Exception('invalid handle: {0}'.format(handle)) + handle = handle.split('@') + pod, user = handle[1], handle[0] + return (pod, user) + + def _gethandle(self, diaspora_id, protocol='https'): + """Get user data using handle. + """ + pod, user = self._sephandle(diaspora_id) + request = self._connection.session.get('{0}://{1}/u/{2}.json'.format(protocol, pod, user)).json() + data = request[0]['author'] + self.data = data + self.stream = Outer(self._connection, location='people/{0}.json'.format(self.data['guid'])) + + def _getguid(self, guid): + """Get user data using guid. + """ + request = self._connection.get('people/{0}.json'.format(guid)).json() + data = request[0]['author'] + self.data = data + self.stream = Outer(self._connection, location='people/{0}.json'.format(self.data['guid'])) + + def fetchguid(self, guid): + """Fetch user data using guid. + """ + self._getguid(guid) + + def fetchhandle(self, diaspora_id, protocol='https'): + """Fetch user data using diaspora id. + """ + self._gethandle(diaspora_id, protocol) diff --git a/diaspy/streams.py b/diaspy/streams.py index c13e0d8..8eea26d 100644 --- a/diaspy/streams.py +++ b/diaspy/streams.py @@ -116,6 +116,19 @@ class Generic: self._stream = self._obtain() +class Outer(Generic): + """Object used by diaspy.models.User to represent + stream of other user. + """ + def _obtain(self): + """Obtains stream of other user. + """ + request = self._connection.get(self._location) + if request.status_code != 200: + raise Exception('wrong status code: {0}'.format(request.status_code)) + return [Post(str(post['id']), self._connection) for post in request.json()] + + class Stream(Generic): """The main stream containing the combined posts of the followed users and tags and the community spotlights posts diff --git a/manual/people.mdown b/manual/people.mdown new file mode 100644 index 0000000..4aa2178 --- /dev/null +++ b/manual/people.mdown @@ -0,0 +1,45 @@ +#### `User()` object + +This object is used to represent a D\* user. + +---- + +##### Getting user data + +You have to know either GUID or *diaspora_id* of a user. +Assume that *12345678abcdefgh* and *otheruser@pod.example.com* point to +the same user. + + + >>> c = diaspy.connection.Connection('https://pod.example.com', 'foo', 'bar') + >>> + >>> user_guid = diaspy.people.User(c) + >>> user_guid.fetchguid('12345678abcdefgh') + >>> + >>> user_handle = diaspy.people.User(c) + >>> user_handle.fetchhandle('otheruser@pod.example.com') + +Now, you have two `User()` objects containing the data of one user. + +The object is subscriptable so you can do like this: + + >>> user_guid['diaspora_id'] + 'otheruser@pod.example.com' + >>> + >>> user_handle['guid'] + '12345678abcdefgh' + + +User object contains following items: + +* `id`, `str`, id of the user; +* `guid`, `str`, guid of the user; +* `diaspora_id`, `str`, D\* id of the user; +* `name`, `str`, name of the user; +* `avatar`, `dict`, links to avatar of the user; +* `stream`, `diaspy.streams.Outer`, stream of the user (provides all methods of generic stream); + + +---- + +###### Manual for `diaspy`, written by Marek Marecki diff --git a/tests.py b/tests.py index aa07717..92b0f61 100644 --- a/tests.py +++ b/tests.py @@ -69,7 +69,7 @@ class StreamTest(unittest.TestCase): stream = diaspy.streams.Stream(test_connection) stream.post_picture('./test-image.png') - def testingAddingTag(self) + def testingAddingTag(self): ft = diaspy.streams.FollowedTags(test_connection) ft.add('test') -- 2.25.1