8 """This is the client class to connect to diaspora.
11 def __init__(self
, pod
, username
, password
):
13 :param pod: The complete url of the diaspora pod to use.
15 :param username: The username used to log in.
17 :param password: The password used to log in.
21 self
._token
_regex
= re
.compile(r
'content="(.*?)"\s+name="csrf-token')
23 self
.session
= requests
.Session()
24 self
._setlogindata
(username
, password
)
28 """This function gets a token needed for authentication in most cases
30 :returns: string -- token used to authenticate
33 r
= self
.session
.get(self
.pod
+ '/stream')
34 token
= self
._token
_regex
.search(r
.text
).group(1)
37 def _setlogindata(self
, username
, password
):
38 """This function is used to set data for login.
41 It should be called before _login() function.
43 #r = self.session.get(self.pod + '/users/sign_in')
44 #token = self._token_regex.search(r.text).group(1)
45 self
._username
, self
._password
= username
, password
47 'user[username]': self
._username
,
48 'user[password]': self
._password
,
49 'authenticity_token': self
.get_token(),
53 """This function is used to connect to the pod and log in.
55 This function shouldn't be called manually.
57 r
= self
.session
.post(self
.pod
+
60 headers
={'accept': 'application/json'})
61 if r
.status_code
!= 201:
62 raise Exception(str(r
.status_code
) + ': Login failed.')
64 def post(self
, text
, aspect_id
='public', photos
=None):
65 """This function sends a post to an aspect
67 :param text: Text to post.
69 :param aspect_id: Aspect id to send post to.
72 :returns: diaspy.models.Post -- the Post which has been created
75 data
= {'aspect_ids': aspect_id
,
76 'status_message': {'text': text
}}
79 data
['photos'] = photos
80 r
= self
.session
.post(self
.pod
+
82 data
=json
.dumps(data
),
83 headers
={'content-type': 'application/json',
84 'accept': 'application/json',
85 'x-csrf-token': self
.get_token()})
86 if r
.status_code
!= 201:
87 raise Exception(str(r
.status_code
) + ': Post could not be posted.')
89 return diaspy
.models
.Post(str(r
.json()['id']), self
)
91 def get_user_info(self
):
92 """This function returns the current user's attributes.
94 :returns: dict -- json formatted user info.
97 r
= self
.session
.get(self
.pod
+ '/bookmarklet')
98 regex
= re
.compile(r
'window.current_user_attributes = ({.*})')
99 userdata
= json
.loads(regex
.search(r
.text
).group(1))
102 def post_picture(self
, filename
):
103 aspects
= self
.get_user_info()['aspects']
105 params
['photo[pending]'] = 'true'
106 params
['set_profile_image'] = ''
107 params
['qqfile'] = filename
108 for i
, aspect
in enumerate(aspects
):
109 params
['photo[aspect_ids][%d]' % (i
)] = aspect
['id']
111 data
= open(filename
, 'rb')
113 headers
= {'content-type': 'application/octet-stream',
114 'x-csrf-token': self
.get_token(),
115 'x-file-name': filename
}
117 r
= self
.session
.post(self
.pod
+ '/photos',
118 params
=params
, data
=data
, headers
=headers
)
122 def get_stream(self
):
123 """This functions returns a list of posts found in the stream.
125 :returns: list -- list of Post objects.
129 data
= {'authenticity_token': self
.get_token()}
130 r
= self
.session
.get(self
.pod
+ "/stream.json")
132 if r
.status_code
!= 200:
133 raise Exception('wrong status code: ' + str(r
.status_code
))
140 posts
.append(diaspy
.models
.Post(str(post
['id']), self
))
144 def get_notifications(self
):
145 """This functions returns a list of notifications.
147 :returns: list -- list of json formatted notifications
151 data
= {'authenticity_token': self
.get_token()}
152 r
= self
.session
.get(self
.pod
+ "/notifications.json")
154 if r
.status_code
!= 200:
155 raise Exception('wrong status code: ' + str(r
.status_code
))
157 notifications
= r
.json()
160 def get_mentions(self
):
161 """This functions returns a list of
162 posts the current user is being mentioned in.
164 :returns: list -- list of Post objects
168 data
= {'authenticity_token': self
.get_token()}
169 r
= self
.session
.get(self
.pod
+ "/mentions.json")
171 if r
.status_code
!= 200:
172 raise Exception('wrong status code: ' + str(r
.status_code
))
178 for post
in mentions
:
179 posts
.append(diaspy
.models
.Post(str(post
['id']), self
))
183 def get_tag(self
, tag
):
184 """This functions returns a list of posts containing the tag.
185 :param tag: Name of the tag
188 :returns: list -- list of Post objects
192 data
= {'authenticity_token': self
.get_token()}
193 r
= self
.session
.get(self
.pod
+ '/tags/' + tag
+ '.json')
195 if r
.status_code
!= 200:
196 raise Exception('wrong status code: ' + str(r
.status_code
))
198 tagged_posts
= r
.json()
202 for post
in tagged_posts
:
203 posts
.append(diaspy
.models
.Post(str(post
['id']), self
))
207 def add_user_to_aspect(self
, user_id
, aspect_id
):
208 """ this function adds a user to an aspect.
210 :param user_id: User ID
212 :param aspect_id: Aspect ID
217 data
= {'authenticity_token': self
.get_token(),
218 'aspect_id': aspect_id
,
219 'person_id': user_id
}
221 r
= self
.session
.post(self
.pod
+ '/aspect_memberships.json',
224 if r
.status_code
!= 201:
225 raise Exception('wrong status code: ' + str(r
.status_code
))
228 def remove_user_from_aspect(self
, user_id
, aspect_id
):
229 """ this function removes a user from an aspect.
231 :param user_id: User ID
233 :param aspect_id: Aspect ID
238 data
= {'authenticity_token': self
.get_token(),
239 'aspect_id': aspect_id
,
240 'person_id': user_id
}
242 r
= self
.session
.delete(self
.pod
+ '/aspect_memberships/42.json',
245 if r
.status_code
!= 200:
246 raise Exception('wrong status code: ' + str(r
.status_code
))
250 def add_aspect(self
, aspect_name
, visible
=0):
251 """ This function adds a new aspect.
254 data
= {'authenticity_token': self
.get_token(),
255 'aspect[name]': aspect_name
,
256 'aspect[contacts_visible]': visible
}
258 r
= self
.session
.post(self
.pod
+ '/aspects',
261 if r
.status_code
!= 200:
262 raise Exception('wrong status code: ' + str(r
.status_code
))
264 def remove_aspect(self
, aspect_id
):
265 """ This function adds a new aspect.
268 data
= {'authenticity_token': self
.get_token()}
270 r
= self
.session
.delete(self
.pod
+ '/aspects/' + aspect_id
,
273 if r
.status_code
!= 404:
274 raise Exception('wrong status code: ' + str(r
.status_code
))
276 def get_mailbox(self
):
277 """This functions returns a list of messages found in the conversation.
279 :returns: list -- list of Conversation objects.
283 data
= {'authenticity_token': self
.get_token()}
284 r
= self
.session
.get(self
.pod
+ "/conversations.json")
286 if r
.status_code
!= 200:
287 raise Exception('wrong status code: ' + str(r
.status_code
))
293 for conversation
in mailbox
:
294 conversations
.append(diaspy
.conversations
.Conversation(str(conversation
['conversation']['id']), self
))
298 def new_conversation(self
, contacts
, subject
, text
):
299 """ start a new conversation
301 :param contacts: recipients ids, no guids, comma sperated.
303 :param subject: subject of the message.
305 :param text: text of the message.
310 data
= {'contact_ids': contacts
,
311 'conversation[subject]': subject
,
312 'conversation[text]': text
,
314 'authenticity_token': self
.get_token()}
316 r
= self
.session
.post(self
.pod
+
319 headers
={'accept': 'application/json'})
320 if r
.status_code
!= 200:
321 raise Exception(str(r
.status_code
) +
322 ': Conversation could not be started.')