From f2eaa3c7bbb495584b5e3ee93da82073c4d3dc4e Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Wed, 1 May 2013 00:30:56 +0200 Subject: [PATCH] Client() rewritten to use Stream() object --- README.md | 36 +++++++++++++++++++++++++ diaspy/client.py | 69 +++++------------------------------------------- diaspy/models.py | 2 -- tests.py | 12 ++++++++- 4 files changed, 54 insertions(+), 65 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..213fab7 --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +#### Python API for Diaspora (unofficial) + +`diaspy` is a set of modules which form API for D\* social network. +The API is written in Python 3.x and is not Python 2.x compatible. + +Object oriented design of `diaspy` makes it easily reusable by other +developers who want to use only part of the API. + +---- + +#### Quick intro + +#### 1. Posting text to your stream + +You only need two objects to do this: `Stream()` and `Connection()`. + + >>> import diaspy + >>> c = diaspy.connection.Connection(pod='https://pod.example.com', + ... username='foo', + ... password='bar') + >>> c.login() + >>> stream = diaspy.models.Stream(c) + >>> stream.post('Your first post') + + +#### 2. More features + +There is a special `client` module in diaspy which is an example client +of D\* written using the `diapsy` API. It provides many features useful for +interactions with social network like messages, mentions, likes etc. +It is full of good, useful stuff. + +---- + +To get more information about how the code works read +documentation (`./doc/` directory) and manual (`./manual/` directory). diff --git a/diaspy/client.py b/diaspy/client.py index 0280172..3f15378 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -18,37 +18,7 @@ class Client: self.connection = diaspy.connection.Connection(pod, username, password) self.connection.login() self.pod = pod - - def _setpostdata(self, text, aspect_ids, photos): - """This function prepares data for posting. - - :param text: Text to post. - :type text: str - :param aspect_ids: Aspect ids to send post to. - :type aspect_ids: str - """ - data = {} - data['aspect_ids'] = aspect_ids - data['status_message'] = {'text': text} - if photos: - data['photos'] = photos - self._post_data = data - - def _post(self): - """Sends post to an aspect. - - :returns: diaspy.models.Post -- the Post which has been created - """ - r = self.connection.post('status_messages', - data=json.dumps(self._post_data), - headers={'content-type': 'application/json', - 'accept': 'application/json', - 'x-csrf-token': self.get_token()}) - if r.status_code != 201: - raise Exception('{0}: Post could not be posted.'.format( - r.status_code)) - - return diaspy.models.Post(str(r.json()['id']), self.connection) + self.stream = diaspy.models.Stream(self.connection) def post(self, text, aspect_ids='public', photos=None): """This function sends a post to an aspect @@ -60,49 +30,24 @@ class Client: :returns: diaspy.models.Post -- the Post which has been created """ - self._setpostdata(text, aspect_ids, photos) - post = self._post() - self._post_data = {} + post = self.stream.post(text, aspect_ids, photos) return post - def get_user_info(self): - """This function returns the current user's attributes. - - :returns: dict -- json formatted user info. - """ - return self.connection.getUserInfo() - def post_picture(self, filename): """This method posts a picture to D*. :param filename: Path to picture file. :type filename: str """ - aspects = self.get_user_info()['aspects'] - params = {} - params['photo[pending]'] = 'true' - params['set_profile_image'] = '' - params['qqfile'] = filename - for i, aspect in enumerate(aspects): - params['photo[aspect_ids][%d]' % (i)] = aspect['id'] - - data = open(filename, 'rb') - - headers = {'content-type': 'application/octet-stream', - 'x-csrf-token': self.get_token(), - 'x-file-name': filename} - - r = self.connection.post('photos', params=params, data=data, headers=headers) - return r + return self.stream.post_picture(filename) def get_stream(self): - """This functions returns a list of posts found in the stream. + """This functions returns user's stream. - :returns: list -- list of Post objects. + :returns: diaspy.models.Stream """ - stream = diaspy.models.Stream(self.connection) - stream.update() - return stream + self.stream.update() + return self.stream def get_notifications(self): """This functions returns a list of notifications. diff --git a/diaspy/models.py b/diaspy/models.py index 8bf71a9..c62d5c4 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -220,7 +220,6 @@ class Stream: request.status_code)) post = Post(str(request.json()['id']), self._connection) - self.update() return post def post_picture(self, filename): @@ -244,5 +243,4 @@ class Stream: 'x-file-name': filename} request = self._connection.post('photos', params=params, data=data, headers=headers) data.close() - self.update() return request diff --git a/tests.py b/tests.py index 4a2ec47..795712b 100644 --- a/tests.py +++ b/tests.py @@ -43,7 +43,7 @@ class StreamTest(unittest.TestCase): c = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__) c.login() stream = diaspy.models.Stream(c) - post = stream.post('`diaspy` test \n#diaspy') + post = stream.post('#diaspy test') self.assertEqual(diaspy.models.Post, type(post)) def testPostingImage(self): @@ -93,4 +93,14 @@ class ClientTests(unittest.TestCase): self.assertEqual(list, type(mailbox)) self.assertEqual(diaspy.conversations.Conversation, type(mailbox[0])) + def testPostingImage(self): + c = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__) + c.post_picture('./test-image.png') + + def testPostingText(self): + c = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__) + post = c.post('#diaspy test') + self.assertEqual(diaspy.models.Post, type(post)) + + if __name__ == '__main__': unittest.main() -- 2.25.1