| 1 | import diaspy.models |
| 2 | import diaspy.streams |
| 3 | import diaspy.connection |
| 4 | |
| 5 | |
| 6 | class Client: |
| 7 | """This is the client class to connect to Diaspora. |
| 8 | """ |
| 9 | def __init__(self, pod, username, password): |
| 10 | """ |
| 11 | :param pod: The complete url of the diaspora pod to use. |
| 12 | :type pod: str |
| 13 | :param username: The username used to log in. |
| 14 | :type username: str |
| 15 | :param password: The password used to log in. |
| 16 | :type password: str |
| 17 | """ |
| 18 | self.connection = diaspy.connection.Connection(pod, username, password) |
| 19 | self.connection.login() |
| 20 | self.pod = pod |
| 21 | self.stream = diaspy.streams.Stream(self.connection, 'stream.json') |
| 22 | |
| 23 | def post(self, text, aspect_ids='public', photos=None): |
| 24 | """This function sends a post to an aspect |
| 25 | |
| 26 | :param text: Text to post. |
| 27 | :type text: str |
| 28 | :param aspect_ids: Aspect ids to send post to. |
| 29 | :type aspect_ids: str |
| 30 | |
| 31 | :returns: diaspy.models.Post -- the Post which has been created |
| 32 | """ |
| 33 | post = self.stream.post(text, aspect_ids, photos) |
| 34 | return post |
| 35 | |
| 36 | def post_picture(self, filename): |
| 37 | """This method posts a picture to D*. |
| 38 | |
| 39 | :param filename: Path to picture file. |
| 40 | :type filename: str |
| 41 | """ |
| 42 | return self.stream.post_picture(filename) |
| 43 | |
| 44 | def get_activity(self): |
| 45 | """This function returns activity stream. |
| 46 | |
| 47 | :returns: diaspy.streams.Activity |
| 48 | """ |
| 49 | activity = diaspy.streams.Activity(self.connection, 'activity.json') |
| 50 | return activity |
| 51 | |
| 52 | def get_stream(self): |
| 53 | """This functions returns stream. |
| 54 | |
| 55 | :returns: diaspy.streams.Stream |
| 56 | """ |
| 57 | self.stream.update() |
| 58 | return self.stream |
| 59 | |
| 60 | def get_aspects(self): |
| 61 | """Returns /aspects stream. |
| 62 | |
| 63 | :returns: diaspy.streams.Aspects |
| 64 | """ |
| 65 | return diaspy.streams.Aspects(self.connection) |
| 66 | |
| 67 | def get_mentions(self): |
| 68 | """Returns /mentions stream. |
| 69 | |
| 70 | :returns: diaspy.streams.Mentions |
| 71 | """ |
| 72 | return diaspy.streams.Mentions(self.connection) |
| 73 | |
| 74 | def get_followed_tags(self): |
| 75 | """Returns followed tags stream. |
| 76 | |
| 77 | :returns: diaspy.streams.FollowedTags |
| 78 | """ |
| 79 | return diaspy.streams.FollowedTags(self.connection) |
| 80 | |
| 81 | def get_tag(self, tag, stream=False): |
| 82 | """This functions returns a list of posts containing the tag. |
| 83 | :param tag: Name of the tag |
| 84 | :type tag: str |
| 85 | :param stream: specify wheter you want a stream object (True) or |
| 86 | normal list (False) |
| 87 | :type stream: bool |
| 88 | |
| 89 | :returns: list -- list of Post objects |
| 90 | """ |
| 91 | if stream: |
| 92 | tagged_posts = diaspy.streams.Generic(self.connection, location='tags/{0}.json'.format(tag)) |
| 93 | else: |
| 94 | r = self.connection.get('tags/{0}.json'.format(tag)) |
| 95 | if r.status_code != 200: |
| 96 | raise Exception('wrong status code: {0}'.format(r.status_code)) |
| 97 | tagged_posts = [diaspy.models.Post(str(post['id']), self.connection) for post in r.json()] |
| 98 | return tagged_posts |
| 99 | |
| 100 | def get_notifications(self): |
| 101 | """This functions returns a list of notifications. |
| 102 | |
| 103 | :returns: list -- list of json formatted notifications |
| 104 | """ |
| 105 | r = self.connection.get('notifications.json') |
| 106 | |
| 107 | if r.status_code != 200: |
| 108 | raise Exception('wrong status code: {0}'.format(r.status_code)) |
| 109 | |
| 110 | notifications = r.json() |
| 111 | return notifications |
| 112 | |
| 113 | def get_mailbox(self): |
| 114 | """This functions returns a list of messages found in the conversation. |
| 115 | |
| 116 | :returns: list -- list of Conversation objects. |
| 117 | """ |
| 118 | r = self.connection.get('conversations.json') |
| 119 | |
| 120 | if r.status_code != 200: |
| 121 | raise Exception('wrong status code: {0}'.format(r.status_code)) |
| 122 | |
| 123 | mailbox = r.json() |
| 124 | return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self.connection) |
| 125 | for conversation in mailbox] |
| 126 | |
| 127 | def add_user_to_aspect(self, user_id, aspect_id): |
| 128 | """ this function adds a user to an aspect. |
| 129 | |
| 130 | :param user_id: User ID |
| 131 | :type user_id: str |
| 132 | :param aspect_id: Aspect ID |
| 133 | :type aspect_id: str |
| 134 | |
| 135 | """ |
| 136 | data = {'authenticity_token': self.connection.getToken(), |
| 137 | 'aspect_id': aspect_id, |
| 138 | 'person_id': user_id} |
| 139 | |
| 140 | r = self.connection.post('aspect_memberships.json', data=data) |
| 141 | |
| 142 | if r.status_code != 201: |
| 143 | raise Exception('wrong status code: {0}'.format(r.status_code)) |
| 144 | return r.json() |
| 145 | |
| 146 | def add_aspect(self, aspect_name, visible=0): |
| 147 | """ This function adds a new aspect. |
| 148 | """ |
| 149 | aspects = diaspy.streams.Aspects(self.connection) |
| 150 | aspects.add(aspect_name, visible) |
| 151 | |
| 152 | def remove_user_from_aspect(self, user_id, aspect_id): |
| 153 | """ this function removes a user from an aspect. |
| 154 | |
| 155 | :param user_id: User ID |
| 156 | :type user_id: str |
| 157 | :param aspect_id: Aspect ID |
| 158 | :type aspect_id: str |
| 159 | |
| 160 | """ |
| 161 | data = {'authenticity_token': self.connection.getToken(), |
| 162 | 'aspect_id': aspect_id, |
| 163 | 'person_id': user_id} |
| 164 | |
| 165 | r = self.connection.delete('aspect_memberships/42.json', |
| 166 | data=data) |
| 167 | |
| 168 | if r.status_code != 200: |
| 169 | raise Exception('wrong status code: {0}'.format(r.status_code)) |
| 170 | |
| 171 | return r.json() |
| 172 | |
| 173 | def remove_aspect(self, aspect_id): |
| 174 | """ This function adds a new aspect. |
| 175 | """ |
| 176 | data = {'authenticity_token': self.connection.getToken()} |
| 177 | |
| 178 | r = self.connection.delete('aspects/{}'.format(aspect_id), |
| 179 | data=data) |
| 180 | |
| 181 | if r.status_code != 404: |
| 182 | raise Exception('wrong status code: {0}'.format(r.status_code)) |
| 183 | |
| 184 | def new_conversation(self, contacts, subject, text): |
| 185 | """Start a new conversation. |
| 186 | |
| 187 | :param contacts: recipients ids, no guids, comma sperated. |
| 188 | :type contacts: str |
| 189 | :param subject: subject of the message. |
| 190 | :type subject: str |
| 191 | :param text: text of the message. |
| 192 | :type text: str |
| 193 | """ |
| 194 | data = {'contact_ids': contacts, |
| 195 | 'conversation[subject]': subject, |
| 196 | 'conversation[text]': text, |
| 197 | 'utf8': '✓', |
| 198 | 'authenticity_token': self.connection.getToken()} |
| 199 | |
| 200 | r = self.connection.post('conversations/', |
| 201 | data=data, |
| 202 | headers={'accept': 'application/json'}) |
| 203 | if r.status_code != 200: |
| 204 | raise Exception('{0}: Conversation could not be started.' |
| 205 | .format(r.status_code)) |
| 206 | return r.json() |