Commit | Line | Data |
---|---|---|
a993a4b6 MK |
1 | import requests |
2 | import re | |
3 | import json | |
95d2d310 | 4 | import diaspy.models |
a993a4b6 MK |
5 | |
6 | class Client: | |
7 | """This is the client class to connect to diaspora. | |
8 | ||
a993a4b6 MK |
9 | """ |
10 | ||
a7661afd MK |
11 | def __init__(self, pod, username, password): |
12 | """ | |
13 | :param pod: The complete url of the diaspora pod to use. | |
14 | :type pod: str | |
15 | :param username: The username used to log in. | |
16 | :type username: str | |
17 | :param password: The password used to log in. | |
18 | :type password: str | |
19 | ||
20 | """ | |
a993a4b6 MK |
21 | self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token') |
22 | self.pod = pod | |
23 | self.session = requests.Session() | |
a7661afd | 24 | self._login(username, password) |
a993a4b6 MK |
25 | |
26 | def get_token(self): | |
ae221396 MK |
27 | """This function gets a token needed for authentication in most cases |
28 | ||
29 | :returns: string -- token used to authenticate | |
30 | ||
31 | """ | |
32 | ||
a7661afd | 33 | r = self.session.get(self.pod + '/stream') |
a993a4b6 MK |
34 | token = self._token_regex.search(r.text).group(1) |
35 | return token | |
36 | ||
a7661afd | 37 | def _login(self, username, password): |
a993a4b6 | 38 | """This function is used to connect to the pod and log in. |
a7661afd MK |
39 | .. note:: |
40 | This function shouldn't be called manually. | |
a993a4b6 MK |
41 | """ |
42 | self._username = username | |
43 | self._password = password | |
0a6fdb79 MK |
44 | #r = self.session.get(self.pod + '/users/sign_in') |
45 | #token = self._token_regex.search(r.text).group(1) | |
a993a4b6 MK |
46 | |
47 | data = {'user[username]': self._username, | |
48 | 'user[password]': self._password, | |
0a6fdb79 | 49 | 'authenticity_token': self.get_token()} |
a993a4b6 | 50 | |
a7661afd MK |
51 | r = self.session.post(self.pod + |
52 | '/users/sign_in', | |
53 | data=data, | |
54 | headers={'accept': 'application/json'}) | |
55 | ||
56 | if r.status_code != 201: | |
57 | raise Exception(str(r.status_code) + ': Login failed.') | |
a993a4b6 | 58 | |
28ca595a | 59 | def post(self, text, aspect_id='public', photos=None): |
a993a4b6 MK |
60 | """This function sends a post to an aspect |
61 | ||
62 | :param text: Text to post. | |
63 | :type text: str | |
64 | :param aspect_id: Aspect id to send post to. | |
65 | :type aspect_id: str | |
66 | ||
95d2d310 MK |
67 | :returns: diaspy.models.Post -- the Post which has been created |
68 | ||
a993a4b6 MK |
69 | """ |
70 | data = {'aspect_ids': aspect_id, | |
28ca595a MK |
71 | 'status_message': {'text': text}} |
72 | ||
73 | if photos: | |
74 | data['photos'] = photos | |
a7661afd MK |
75 | r = self.session.post(self.pod + |
76 | "/status_messages", | |
28ca595a MK |
77 | data=json.dumps(data), |
78 | headers={'content-type': 'application/json', | |
79 | 'accept': 'application/json', | |
80 | 'x-csrf-token': self.get_token()}) | |
a7661afd MK |
81 | if r.status_code != 201: |
82 | raise Exception(str(r.status_code) + ': Post could not be posted.') | |
83 | ||
95d2d310 | 84 | return diaspy.models.Post(str(r.json()['id']), self) |
a993a4b6 MK |
85 | |
86 | def get_user_info(self): | |
87 | """This function returns the current user's attributes. | |
88 | ||
89 | :returns: dict -- json formatted user info. | |
90 | ||
91 | """ | |
28ca595a | 92 | r = self.session.get(self.pod + '/bookmarklet') |
a993a4b6 MK |
93 | regex = re.compile(r'window.current_user_attributes = ({.*})') |
94 | userdata = json.loads(regex.search(r.text).group(1)) | |
95 | return userdata | |
b356c9f9 | 96 | |
28ca595a MK |
97 | def post_picture(self, filename): |
98 | aspects = self.get_user_info()['aspects'] | |
99 | params = {} | |
100 | params['photo[pending]'] = 'true' | |
101 | params['set_profile_image'] = '' | |
102 | params['qqfile'] = filename | |
103 | for i, aspect in enumerate(aspects): | |
104 | params['photo[aspect_ids][%d]' % (i)] = aspect['id'] | |
105 | ||
106 | data = open(filename, 'rb') | |
107 | ||
108 | headers = {'content-type': 'application/octet-stream', | |
109 | 'x-csrf-token': self.get_token(), | |
110 | 'x-file-name': filename} | |
111 | ||
112 | r = self.session.post(self.pod + '/photos', params=params, data=data, headers=headers) | |
113 | ||
114 | return r | |
115 | ||
116 | ||
117 | ||
b356c9f9 MK |
118 | def get_stream(self): |
119 | """This functions returns a list of posts found in the stream. | |
120 | ||
121 | :returns: list -- list of Post objects. | |
122 | ||
123 | """ | |
124 | ||
125 | data = {'authenticity_token': self.get_token()} | |
126 | r = self.session.get(self.pod + "/stream.json") | |
127 | ||
128 | if r.status_code != 200: | |
129 | raise Exception('wrong status code: ' + str(r.status_code)) | |
130 | ||
131 | stream = r.json() | |
132 | ||
133 | posts = [] | |
134 | ||
135 | for post in stream: | |
136 | posts.append(diaspy.models.Post(str(post['id']), self)) | |
137 | ||
138 | return posts | |
33f21ecf MK |
139 | |
140 | def get_notifications(self): | |
141 | """This functions returns a list of notifications. | |
142 | ||
143 | :returns: list -- list of json formatted notifications | |
144 | ||
145 | """ | |
146 | ||
147 | ||
148 | data = {'authenticity_token': self.get_token()} | |
149 | r = self.session.get(self.pod + "/notifications.json") | |
150 | ||
151 | if r.status_code != 200: | |
152 | raise Exception('wrong status code: ' + str(r.status_code)) | |
153 | ||
154 | notifications = r.json() | |
155 | return notifications | |
3d3dff8f MK |
156 | |
157 | ||
158 | def get_mentions(self): | |
159 | """This functions returns a list of posts the current user is being mentioned in. | |
160 | ||
161 | :returns: list -- list of Post objects | |
162 | ||
163 | """ | |
164 | ||
165 | ||
166 | data = {'authenticity_token': self.get_token()} | |
167 | r = self.session.get(self.pod + "/mentions.json") | |
168 | ||
169 | if r.status_code != 200: | |
170 | raise Exception('wrong status code: ' + str(r.status_code)) | |
171 | ||
172 | mentions = r.json() | |
173 | ||
174 | posts = [] | |
175 | ||
176 | for post in mentions: | |
177 | posts.append(diaspy.models.Post(str(post['id']), self)) | |
178 | ||
179 | return posts | |
5c2b6162 MK |
180 | |
181 | def add_user_to_aspect(self, user_id, aspect_id): | |
182 | """ this function adds a user to an aspect. | |
183 | ||
184 | :param user_id: User ID | |
185 | :type user_id: str | |
186 | :param aspect_id: Aspect ID | |
187 | :type aspect_id: str | |
188 | ||
189 | """ | |
190 | ||
191 | data = {'authenticity_token': self.get_token(), | |
192 | 'aspect_id': aspect_id, | |
193 | 'person_id': user_id} | |
194 | ||
195 | r = self.session.post(self.pod + '/aspect_memberships.json', | |
196 | data=data) | |
197 | ||
198 | if r.status_code != 201: | |
199 | raise Exception('wrong status code: ' + str(r.status_code)) | |
200 | return r.json() | |
201 | ||
202 | def remove_user_from_aspect(self, user_id, aspect_id): | |
203 | """ this function removes a user from an aspect. | |
204 | ||
205 | :param user_id: User ID | |
206 | :type user_id: str | |
207 | :param aspect_id: Aspect ID | |
208 | :type aspect_id: str | |
209 | ||
210 | """ | |
211 | ||
212 | data = {'authenticity_token': self.get_token(), | |
213 | 'aspect_id': aspect_id, | |
214 | 'person_id': user_id} | |
215 | ||
216 | r = self.session.delete(self.pod + '/aspect_memberships/42.json', | |
217 | data=data) | |
218 | ||
219 | if r.status_code != 200: | |
220 | raise Exception('wrong status code: ' + str(r.status_code)) | |
221 | ||
222 | return r.json() | |
22cb1646 MK |
223 | |
224 | def add_aspect(self, aspect_name, visible=0): | |
225 | """ This function adds a new aspect. | |
226 | """ | |
227 | ||
228 | data = {'authenticity_token': self.get_token(), | |
229 | 'aspect[name]': aspect_name, | |
230 | 'aspect[contacts_visible]': visible} | |
231 | ||
232 | r = self.session.post(self.pod + '/aspects', | |
233 | data=data) | |
234 | ||
235 | if r.status_code != 200: | |
236 | raise Exception('wrong status code: ' + str(r.status_code)) | |
237 | ||
238 | def remove_aspect(self, aspect_id): | |
239 | """ This function adds a new aspect. | |
240 | """ | |
241 | ||
242 | data = {'authenticity_token': self.get_token()} | |
243 | ||
244 | r = self.session.delete(self.pod + '/aspects/' + aspect_id, | |
610a44df | 245 | data=data ) |
22cb1646 MK |
246 | |
247 | if r.status_code != 404: | |
248 | raise Exception('wrong status code: ' + str(r.status_code)) |