`diaspy` ported to use `Connection()` object
[diaspy.git] / diaspy / client.py
CommitLineData
a993a4b6
MK
1import re
2import json
95d2d310 3import diaspy.models
8993810c 4import diaspy.connection
a993a4b6 5
4685fc31 6
a993a4b6 7class Client:
d930e127 8 """This is the client class to connect to Diaspora.
a993a4b6 9 """
a7661afd
MK
10 def __init__(self, pod, username, password):
11 """
12 :param pod: The complete url of the diaspora pod to use.
13 :type pod: str
14 :param username: The username used to log in.
15 :type username: str
16 :param password: The password used to log in.
17 :type password: str
a7661afd 18 """
8993810c
MM
19 self.connection = diaspy.connection.Connection(pod, username, password)
20 self.connection.login()
a993a4b6 21 self.pod = pod
eda2bceb 22
9088535d 23 def _setpostdata(self, text, aspect_ids, photos):
a1a09a06 24 """This function prepares data for posting.
9088535d 25
313c9233
MM
26 :param text: Text to post.
27 :type text: str
9088535d
MK
28 :param aspect_ids: Aspect ids to send post to.
29 :type aspect_ids: str
313c9233 30 """
a1a09a06 31 data = {}
9088535d 32 data['aspect_ids'] = aspect_ids
a1a09a06 33 data['status_message'] = {'text': text}
9088535d
MK
34 if photos:
35 data['photos'] = photos
313c9233
MM
36 self._post_data = data
37
a1a09a06
MM
38 def _post(self):
39 """Sends post to an aspect.
313c9233 40
a1a09a06 41 :returns: diaspy.models.Post -- the Post which has been created
313c9233 42 """
b95ffe83 43 r = self.connection.post('status_messages',
385e7ebe
MM
44 data=json.dumps(self._post_data),
45 headers={'content-type': 'application/json',
46 'accept': 'application/json',
47 'x-csrf-token': self.get_token()})
9088535d
MK
48 if r.status_code != 201:
49 raise Exception('{0}: Post could not be posted.'.format(
50 r.status_code))
313c9233 51
95d2d310 52 return diaspy.models.Post(str(r.json()['id']), self)
313c9233 53
9088535d 54 def post(self, text, aspect_ids='public', photos=None):
a993a4b6
MK
55 """This function sends a post to an aspect
56
57 :param text: Text to post.
58 :type text: str
9088535d
MK
59 :param aspect_ids: Aspect ids to send post to.
60 :type aspect_ids: str
a993a4b6 61
95d2d310 62 :returns: diaspy.models.Post -- the Post which has been created
a993a4b6 63 """
9088535d 64 self._setpostdata(text, aspect_ids, photos)
9fae7c88
MM
65 post = self._post()
66 self._post_data = {}
67 return post
a993a4b6
MK
68
69 def get_user_info(self):
70 """This function returns the current user's attributes.
71
72 :returns: dict -- json formatted user info.
a993a4b6 73 """
8993810c 74 r = self.connection.get('bookmarklet')
a993a4b6
MK
75 regex = re.compile(r'window.current_user_attributes = ({.*})')
76 userdata = json.loads(regex.search(r.text).group(1))
77 return userdata
b356c9f9 78
28ca595a 79 def post_picture(self, filename):
a1a09a06
MM
80 """This method posts a picture to D*.
81
82 :param filename: Path to picture file.
83 :type filename: str
84 """
28ca595a
MK
85 aspects = self.get_user_info()['aspects']
86 params = {}
87 params['photo[pending]'] = 'true'
88 params['set_profile_image'] = ''
89 params['qqfile'] = filename
90 for i, aspect in enumerate(aspects):
91 params['photo[aspect_ids][%d]' % (i)] = aspect['id']
92
93 data = open(filename, 'rb')
94
95 headers = {'content-type': 'application/octet-stream',
96 'x-csrf-token': self.get_token(),
97 'x-file-name': filename}
98
b95ffe83 99 r = self.connection.post('photos', params=params, data=data, headers=headers)
28ca595a
MK
100 return r
101
b356c9f9
MK
102 def get_stream(self):
103 """This functions returns a list of posts found in the stream.
104
105 :returns: list -- list of Post objects.
b356c9f9 106 """
8993810c 107 request = self.connection.get('stream.json')
b356c9f9 108
8993810c
MM
109 if request.status_code != 200:
110 raise Exception('wrong status code: {0}'.format(request.status_code))
b356c9f9 111
8993810c 112 stream = request.json()
385e7ebe 113 return [diaspy.models.Post(str(post['id']), self.connection) for post in stream]
33f21ecf
MK
114
115 def get_notifications(self):
116 """This functions returns a list of notifications.
117
118 :returns: list -- list of json formatted notifications
33f21ecf 119 """
b95ffe83 120 r = self.connection.get('notifications.json')
33f21ecf 121
88ec6cda 122 if r.status_code != 200:
d930e127 123 raise Exception('wrong status code: {0}'.format(r.status_code))
33f21ecf
MK
124
125 notifications = r.json()
126 return notifications
3d3dff8f 127
3d3dff8f 128 def get_mentions(self):
4685fc31
MK
129 """This functions returns a list of
130 posts the current user is being mentioned in.
3d3dff8f
MK
131
132 :returns: list -- list of Post objects
3d3dff8f 133 """
b95ffe83 134 r = self.connection.get('mentions.json')
3d3dff8f
MK
135
136 if r.status_code != 200:
d930e127 137 raise Exception('wrong status code: {0}'.format(r.status_code))
3d3dff8f
MK
138
139 mentions = r.json()
385e7ebe 140 return [diaspy.models.Post(str(post['id']), self.connection) for post in mentions]
5c2b6162 141
4685fc31
MK
142 def get_tag(self, tag):
143 """This functions returns a list of posts containing the tag.
144 :param tag: Name of the tag
145 :type tag: str
146
147 :returns: list -- list of Post objects
4685fc31 148 """
b95ffe83 149 r = self.connection.get('tags/{0}.json'.format(tag))
4685fc31
MK
150
151 if r.status_code != 200:
d930e127 152 raise Exception('wrong status code: {0}'.format(r.status_code))
4685fc31
MK
153
154 tagged_posts = r.json()
385e7ebe 155 return [diaspy.models.Post(str(post['id']), self.connection) for post in tagged_posts]
4685fc31 156
264336e2
MM
157 def get_mailbox(self):
158 """This functions returns a list of messages found in the conversation.
159
160 :returns: list -- list of Conversation objects.
161 """
b95ffe83 162 r = self.connection.get('conversations.json')
264336e2
MM
163
164 if r.status_code != 200:
165 raise Exception('wrong status code: {0}'.format(r.status_code))
166
167 mailbox = r.json()
385e7ebe 168 return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self.connection)
490a69d0 169 for conversation in mailbox]
264336e2 170
5c2b6162
MK
171 def add_user_to_aspect(self, user_id, aspect_id):
172 """ this function adds a user to an aspect.
173
174 :param user_id: User ID
175 :type user_id: str
176 :param aspect_id: Aspect ID
177 :type aspect_id: str
178
179 """
5c2b6162
MK
180 data = {'authenticity_token': self.get_token(),
181 'aspect_id': aspect_id,
182 'person_id': user_id}
183
b95ffe83 184 r = self.connection.post('aspect_memberships.json', data=data)
5c2b6162
MK
185
186 if r.status_code != 201:
d930e127 187 raise Exception('wrong status code: {0}'.format(r.status_code))
5c2b6162
MK
188 return r.json()
189
e475a9d0
MM
190 def add_aspect(self, aspect_name, visible=0):
191 """ This function adds a new aspect.
192 """
193
194 data = {'authenticity_token': self.get_token(),
195 'aspect[name]': aspect_name,
196 'aspect[contacts_visible]': visible}
197
b95ffe83 198 r = self.connection.post('aspects', data=data)
e475a9d0
MM
199
200 if r.status_code != 200:
201 raise Exception('wrong status code: {0}'.format(r.status_code))
202
5c2b6162
MK
203 def remove_user_from_aspect(self, user_id, aspect_id):
204 """ this function removes a user from an aspect.
205
206 :param user_id: User ID
207 :type user_id: str
208 :param aspect_id: Aspect ID
209 :type aspect_id: str
210
211 """
5c2b6162
MK
212 data = {'authenticity_token': self.get_token(),
213 'aspect_id': aspect_id,
214 'person_id': user_id}
215
b95ffe83 216 r = self.connection.delete('aspect_memberships/42.json',
385e7ebe 217 data=data)
5c2b6162
MK
218
219 if r.status_code != 200:
d930e127 220 raise Exception('wrong status code: {0}'.format(r.status_code))
5c2b6162
MK
221
222 return r.json()
22cb1646 223
22cb1646
MK
224 def remove_aspect(self, aspect_id):
225 """ This function adds a new aspect.
226 """
22cb1646
MK
227 data = {'authenticity_token': self.get_token()}
228
b95ffe83 229 r = self.connection.delete('aspects/{}'.format(aspect_id),
385e7ebe 230 data=data)
22cb1646
MK
231
232 if r.status_code != 404:
d930e127 233 raise Exception('wrong status code: {0}'.format(r.status_code))
91d2d5dc 234
91d2d5dc 235 def new_conversation(self, contacts, subject, text):
264336e2 236 """Start a new conversation.
91d2d5dc
B
237
238 :param contacts: recipients ids, no guids, comma sperated.
239 :type contacts: str
240 :param subject: subject of the message.
241 :type subject: str
242 :param text: text of the message.
243 :type text: str
91d2d5dc 244 """
91d2d5dc
B
245 data = {'contact_ids': contacts,
246 'conversation[subject]': subject,
247 'conversation[text]': text,
248 'utf8': '✓',
249 'authenticity_token': self.get_token()}
250
b95ffe83 251 r = self.connection.post('conversations/',
385e7ebe
MM
252 data=data,
253 headers={'accept': 'application/json'})
91d2d5dc 254 if r.status_code != 200:
9088535d
MK
255 raise Exception('{0}: Conversation could not be started.'
256 .format(r.status_code))
91d2d5dc 257 return r.json()