8 """This is the client class to connect to Diaspora.
10 def __init__(self
, pod
, username
, password
):
12 :param pod: The complete url of the diaspora pod to use.
14 :param username: The username used to log in.
16 :param password: The password used to log in.
19 self
._token
_regex
= re
.compile(r
'content="(.*?)"\s+name="csrf-token')
21 self
.session
= requests
.Session()
23 self
._setlogindata
(username
, password
)
26 def _sessionget(self
, string
):
27 """This method gets data from session.
28 Performs additional checks if needed.
31 To obtain 'foo' from pod one should call `_sessionget('foo')`.
33 :param string: URL to get without the pod's URL and slash eg. 'stream'.
36 return self
.session
.get('{0}/{1}'.format(self
.pod
, string
))
39 """This function gets a token needed for authentication in most cases
41 :returns: string -- token used to authenticate
43 r
= self
._sessionget
('stream')
44 token
= self
._token
_regex
.search(r
.text
).group(1)
47 def _setlogindata(self
, username
, password
):
48 """This function is used to set data for login.
51 It should be called before _login() function.
53 self
._username
, self
._password
= username
, password
55 'user[username]': self
._username
,
56 'user[password]': self
._password
,
57 'authenticity_token': self
.get_token(),
61 """This function is used to connect to the pod and log in.
63 r
= self
.session
.post('{0}/users/sign_in'.format(self
.pod
),
64 data
=self
._login
_data
,
65 headers
={'accept': 'application/json'})
67 if r
.status_code
!= 201: raise Exception('{0}: Login failed.'.format(r
.status_code
))
69 def _setpostdata(self
, text
, aspect_id
, photos
):
70 """This function prepares data for posting.
72 :param text: Text to post.
74 :param aspect_id: Aspect id to send post to.
78 data
['aspect_id'] = aspect_id
79 data
['status_message'] = {'text': text
}
80 if photos
: data
['photos'] = photos
81 self
._post
_data
= data
84 """Sends post to an aspect.
86 :returns: diaspy.models.Post -- the Post which has been created
88 r
= self
.session
.post('{0}/status_messages'.format(self
.pod
),
89 data
=json
.dumps(self
._post
_data
),
90 headers
={'content-type': 'application/json',
91 'accept': 'application/json',
92 'x-csrf-token': self
.get_token()})
93 if r
.status_code
!= 201: raise Exception('{0}: Post could not be posted.'.format(r
.status_code
))
95 return diaspy
.models
.Post(str(r
.json()['id']), self
)
97 def post(self
, text
, aspect_id
='public', photos
=None):
98 """This function sends a post to an aspect
100 :param text: Text to post.
102 :param aspect_id: Aspect id to send post to.
105 :returns: diaspy.models.Post -- the Post which has been created
107 self
._setpostdata
(text
, aspect_id
, photos
)
112 def get_user_info(self
):
113 """This function returns the current user's attributes.
115 :returns: dict -- json formatted user info.
117 r
= self
._sessionget
('bookmarklet')
118 regex
= re
.compile(r
'window.current_user_attributes = ({.*})')
119 userdata
= json
.loads(regex
.search(r
.text
).group(1))
122 def post_picture(self
, filename
):
123 """This method posts a picture to D*.
125 :param filename: Path to picture file.
128 aspects
= self
.get_user_info()['aspects']
130 params
['photo[pending]'] = 'true'
131 params
['set_profile_image'] = ''
132 params
['qqfile'] = filename
133 for i
, aspect
in enumerate(aspects
):
134 params
['photo[aspect_ids][%d]' % (i
)] = aspect
['id']
136 data
= open(filename
, 'rb')
138 headers
= {'content-type': 'application/octet-stream',
139 'x-csrf-token': self
.get_token(),
140 'x-file-name': filename
}
142 r
= self
.session
.post('{0}/photos'.format(self
.pod
),
143 params
=params
, data
=data
, headers
=headers
)
147 def get_stream(self
):
148 """This functions returns a list of posts found in the stream.
150 :returns: list -- list of Post objects.
152 r
= self
._sessionget
('stream.json')
154 if r
.status_code
!= 200:
155 raise Exception('wrong status code: {0}'.format(r
.status_code
))
158 return [ diaspy
.models
.Post(str(post
['id']), self
) for post
in stream
]
160 def get_notifications(self
):
161 """This functions returns a list of notifications.
163 :returns: list -- list of json formatted notifications
165 r
= self
._sessionget
('notifications.json')
167 if r
.status_code
!= 200:
168 raise Exception('wrong status code: {0}'.format(r
.status_code
))
170 notifications
= r
.json()
173 def get_mentions(self
):
174 """This functions returns a list of
175 posts the current user is being mentioned in.
177 :returns: list -- list of Post objects
179 r
= self
._sessionget
('mentions.json')
181 if r
.status_code
!= 200:
182 raise Exception('wrong status code: {0}'.format(r
.status_code
))
185 return [ diaspy
.models
.Post(str(post
['id']), self
) for post
in mentions
]
187 def get_tag(self
, tag
):
188 """This functions returns a list of posts containing the tag.
189 :param tag: Name of the tag
192 :returns: list -- list of Post objects
194 r
= self
._sessionget
('tags/{0}.json'.format(tag
))
196 if r
.status_code
!= 200:
197 raise Exception('wrong status code: {0}'.format(r
.status_code
))
199 tagged_posts
= r
.json()
200 return [ diaspy
.models
.Post(str(post
['id']), self
) for post
in tagged_posts
]
202 def get_mailbox(self
):
203 """This functions returns a list of messages found in the conversation.
205 :returns: list -- list of Conversation objects.
207 r
= self
._sessionget
('conversations.json')
209 if r
.status_code
!= 200:
210 raise Exception('wrong status code: {0}'.format(r
.status_code
))
213 return [ diaspy
.conversations
.Conversation(str(conversation
['conversation']['id']), self
) for conversation
in mailbox
]
215 def add_user_to_aspect(self
, user_id
, aspect_id
):
216 """ this function adds a user to an aspect.
218 :param user_id: User ID
220 :param aspect_id: Aspect ID
225 data
= {'authenticity_token': self
.get_token(),
226 'aspect_id': aspect_id
,
227 'person_id': user_id
}
229 r
= self
.session
.post('{0}/aspect_memberships.json'.format(self
.pod
),
232 if r
.status_code
!= 201:
233 raise Exception('wrong status code: {0}'.format(r
.status_code
))
236 def remove_user_from_aspect(self
, user_id
, aspect_id
):
237 """ this function removes a user from an aspect.
239 :param user_id: User ID
241 :param aspect_id: Aspect ID
246 data
= {'authenticity_token': self
.get_token(),
247 'aspect_id': aspect_id
,
248 'person_id': user_id
}
250 r
= self
.session
.delete('{0}/aspect_memberships/42.json'.format(self
.pod
),
253 if r
.status_code
!= 200:
254 raise Exception('wrong status code: {0}'.format(r
.status_code
))
258 def add_aspect(self
, aspect_name
, visible
=0):
259 """ This function adds a new aspect.
262 data
= {'authenticity_token': self
.get_token(),
263 'aspect[name]': aspect_name
,
264 'aspect[contacts_visible]': visible
}
266 r
= self
.session
.post('{0}/aspects'.format(self
.pod
),
269 if r
.status_code
!= 200:
270 raise Exception('wrong status code: {0}'.format(r
.status_code
))
272 def remove_aspect(self
, aspect_id
):
273 """ This function adds a new aspect.
275 data
= {'authenticity_token': self
.get_token()}
277 r
= self
.session
.delete('{0}/aspects/{1}'.format(self
.pod
, aspect_id
),
280 if r
.status_code
!= 404:
281 raise Exception('wrong status code: {0}'.format(r
.status_code
))
283 def new_conversation(self
, contacts
, subject
, text
):
284 """Start a new conversation.
286 :param contacts: recipients ids, no guids, comma sperated.
288 :param subject: subject of the message.
290 :param text: text of the message.
293 data
= {'contact_ids': contacts
,
294 'conversation[subject]': subject
,
295 'conversation[text]': text
,
297 'authenticity_token': self
.get_token()}
299 r
= self
.session
.post('{0}/conversations/'.format(self
.pod
),
301 headers
={'accept': 'application/json'})
302 if r
.status_code
!= 200:
303 raise Exception('{0}: Conversation could not be started.'.format(r
.status_code
))