X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=diaspy%2Fclient.py;h=ce07990a70cf33be9fbcfaf94e89de21b47d9a44;hb=b23e2ccff08d326499339d8a70d93c76b8a861d0;hp=2dc088e5942e580c94d6403dfa03d9068f0196bc;hpb=75d38d3c11e87bab6f3226766e5cd1188716b488;p=diaspy.git diff --git a/diaspy/client.py b/diaspy/client.py index 2dc088e..ce07990 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -1,202 +1,123 @@ -import requests -import re -import json import diaspy.models +import diaspy.streams +import diaspy.connection +from diaspy import notifications class Client: - """This is the client class to connect to Diaspora. - + """This is the client class used to interact with Diaspora. + It can be used as a reference implementation of client using diaspy. """ - def __init__(self, pod, username, password): + def __init__(self, pod, username='', password=''): """ - :param pod: The complete url of the diaspora pod to use. + `pod` can also be a diaspy.connection.Connection type and + Client() will detect it. When giving a connection there is no need + to pass username and password. + + :param pod: The complete url of the diaspora pod to use + (or Connection() object). :type pod: str :param username: The username used to log in. :type username: str :param password: The password used to log in. :type password: str - """ - self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token') - self.pod = pod - self.session = requests.Session() - self._setlogindata(username, password) - - def get_token(self): - """This function gets a token needed for authentication in most cases + if type(pod) == diaspy.connection.Connection: + self.connection = pod + else: + self.connection = diaspy.connection.Connection(pod, username, password) + self.connection.login() + self.stream = diaspy.streams.Stream(self.connection, 'stream.json') - :returns: string -- token used to authenticate - - """ - r = self.session.get('{0}/stream'.format(self.pod)) - token = self._token_regex.search(r.text).group(1) - return token - - def _setlogindata(self, username, password): - """This function is used to set data for login. - - .. note:: - It should be called before _login() function. - """ - #r = self.session.get(self.pod + '/users/sign_in') - #token = self._token_regex.search(r.text).group(1) - self._username, self._password = username, password - self._login_data = { - 'user[username]': self._username, - 'user[password]': self._password, - 'authenticity_token': self.get_token(), - } - - def _login(self): - """This function is used to connect to the pod and log in. - """ - r = self.session.post('{0}/users/sign_in'.format(self.pod), - data=self._login_data, - headers={'accept': 'application/json'}) - - if r.status_code != 201: raise Exception('{0}: Login failed.'.format(r.status_code)) - - def post(self, text, aspect_id='public', photos=None): + def post(self, text, aspect_ids='public', photos=None, photo=''): """This function sends a post to an aspect - :param text: Text to post. + :param text: text to post :type text: str - :param aspect_id: Aspect id to send post to. - :type aspect_id: str - + :param aspect_ids: Aspect ids to send post to. + :type aspect_ids: str + :param photo: path to picture file + :type photo: str :returns: diaspy.models.Post -- the Post which has been created - """ - data = {'aspect_ids': aspect_id, - 'status_message': {'text': text}} - - if photos: - data['photos'] = photos - r = self.session.post('{0}/status_messages'.format(self.pod), - data=json.dumps(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) - - def get_user_info(self): - """This function returns the current user's attributes. + post = self.stream.post(text, aspect_ids, photos, photo) + return post - :returns: dict -- json formatted user info. + def get_activity(self): + """This function returns activity stream. + :returns: diaspy.streams.Activity """ - r = self.session.get('{0}/bookmarklet'.format(self.pod)) - regex = re.compile(r'window.current_user_attributes = ({.*})') - userdata = json.loads(regex.search(r.text).group(1)) - return userdata - - def post_picture(self, filename): - 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.session.post('{0}/photos'.format(self.pod), - params=params, data=data, headers=headers) - - return r + return diaspy.streams.Activity(self.connection, 'activity.json') def get_stream(self): - """This functions returns a list of posts found in the stream. - - :returns: list -- list of Post objects. + """This functions returns stream. + :returns: diaspy.streams.Stream """ + self.stream.update() + return self.stream - data = {'authenticity_token': self.get_token()} - r = self.session.get('{0}/stream.json'.format(self.pod)) - - if r.status_code != 200: - raise Exception('wrong status code: {0}'.format(r.status_code)) - - stream = r.json() - - posts = [] - - for post in stream: - posts.append(diaspy.models.Post(str(post['id']), self)) - - return posts - - def get_notifications(self): - """This functions returns a list of notifications. - - :returns: list -- list of json formatted notifications + def get_aspects(self): + """Returns aspects stream. + :returns: diaspy.streams.Aspects """ - - data = {'authenticity_token': self.get_token()} - r = self.session.get('{0}/notifications.json'.format(self.pod)) - - if r.status_code != 200: - raise Exception('wrong status code: {0}'.format(r.status_code)) - - notifications = r.json() - return notifications + return diaspy.streams.Aspects(self.connection) def get_mentions(self): - """This functions returns a list of - posts the current user is being mentioned in. - - :returns: list -- list of Post objects + """Returns /mentions stream. + :returns: diaspy.streams.Mentions """ + return diaspy.streams.Mentions(self.connection) - data = {'authenticity_token': self.get_token()} - r = self.session.get('/mentions.json'.format(self.pod)) - - if r.status_code != 200: - raise Exception('wrong status code: {0}'.format(r.status_code)) + def get_followed_tags(self): + """Returns followed tags stream. - mentions = r.json() - - posts = [] - - for post in mentions: - posts.append(diaspy.models.Post(str(post['id']), self)) - - return posts + :returns: diaspy.streams.FollowedTags + """ + return diaspy.streams.FollowedTags(self.connection) def get_tag(self, tag): """This functions returns a list of posts containing the tag. :param tag: Name of the tag :type tag: str - :returns: list -- list of Post objects + :returns: diaspy.streams.Generic -- stream containg posts with given tag + """ + return diaspy.streams.Generic(self.connection, location='tags/{0}.json'.format(tag)) + def get_notifications(self): + """This functions returns a list of notifications. + + :returns: list -- list of json formatted notifications """ + return notifications.Notifications(self.connection) - data = {'authenticity_token': self.get_token()} - r = self.session.get('{0}/tags/{1}.json'.format(self.pod, tag)) + def get_mailbox(self): + """This functions returns a list of messages found in the conversation. + + :returns: list -- list of Conversation objects. + """ + r = self.connection.get('conversations.json') if r.status_code != 200: raise Exception('wrong status code: {0}'.format(r.status_code)) - tagged_posts = r.json() - - posts = [] + mailbox = r.json() + return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self.connection) + for conversation in mailbox] - for post in tagged_posts: - posts.append(diaspy.models.Post(str(post['id']), self)) + def add_aspect(self, aspect_name, visible=0): + """This function adds a new aspect. + """ + diaspy.streams.Aspects(self.connection).add(aspect_name, visible) - return posts + def remove_aspect(self, aspect_id): + """This function removes an aspect. + """ + diaspy.streams.Aspects(self.connection).remove(aspect_id) def add_user_to_aspect(self, user_id, aspect_id): """ this function adds a user to an aspect. @@ -207,17 +128,7 @@ class Client: :type aspect_id: str """ - - data = {'authenticity_token': self.get_token(), - 'aspect_id': aspect_id, - 'person_id': user_id} - - r = self.session.post('{0}/aspect_memberships.json'.format(self.pod), - data=data) - - if r.status_code != 201: - raise Exception('wrong status code: {0}'.format(r.status_code)) - return r.json() + return diaspy.models.Aspect(self.connection, aspect_id).addUser(user_id) def remove_user_from_aspect(self, user_id, aspect_id): """ this function removes a user from an aspect. @@ -228,69 +139,10 @@ class Client: :type aspect_id: str """ - - data = {'authenticity_token': self.get_token(), - 'aspect_id': aspect_id, - 'person_id': user_id} - - r = self.session.delete('{0}/aspect_memberships/42.json'.format(self.pod), - data=data) - - if r.status_code != 200: - raise Exception('wrong status code: {0}'.format(r.status_code)) - - return r.json() - - def add_aspect(self, aspect_name, visible=0): - """ This function adds a new aspect. - """ - - data = {'authenticity_token': self.get_token(), - 'aspect[name]': aspect_name, - 'aspect[contacts_visible]': visible} - - r = self.session.post('{0}/aspects'.format(self.pod), - data=data) - - if r.status_code != 200: - raise Exception('wrong status code: {0}'.format(r.status_code)) - - def remove_aspect(self, aspect_id): - """ This function adds a new aspect. - """ - - data = {'authenticity_token': self.get_token()} - - r = self.session.delete('{0}/aspects/{1}'.format(self.pod, aspect_id), - data=data) - - if r.status_code != 404: - raise Exception('wrong status code: {0}'.format(r.status_code)) - - def get_mailbox(self): - """This functions returns a list of messages found in the conversation. - - :returns: list -- list of Conversation objects. - - """ - - data = {'authenticity_token': self.get_token()} - r = self.session.get('{0}/conversations.json'.format(self.pod)) - - if r.status_code != 200: - raise Exception('wrong status code: {0}'.format(r.status_code)) - - mailbox = r.json() - - conversations = [] - - for conversation in mailbox: - conversations.append(diaspy.conversations.Conversation(str(conversation['conversation']['id']), self)) - - return conversations + return diaspy.models.Aspect(self.connection, aspect_id).removeUser(user_id) def new_conversation(self, contacts, subject, text): - """ start a new conversation + """Start a new conversation. :param contacts: recipients ids, no guids, comma sperated. :type contacts: str @@ -298,19 +150,17 @@ class Client: :type subject: str :param text: text of the message. :type text: str - """ - data = {'contact_ids': contacts, 'conversation[subject]': subject, 'conversation[text]': text, 'utf8': '✓', - 'authenticity_token': self.get_token()} + 'authenticity_token': self.connection.get_token()} - r = self.session.post('{0}/conversations/'.format(self.pod), - data=data, - headers={'accept': 'application/json'}) + r = self.connection.post('conversations/', + data=data, + headers={'accept': 'application/json'}) if r.status_code != 200: - raise Exception('{0}: Conversation could not be started.'.format(r.status_code)) - + raise Exception('{0}: Conversation could not be started.' + .format(r.status_code)) return r.json()