5 import diaspy
.connection
9 """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.
20 self
.connection
= diaspy
.connection
.Connection(pod
, username
, password
)
21 self
.connection
.login()
25 def _sessionget(self
, string
):
26 """This method gets data from session.
27 Performs additional checks if needed.
30 To obtain 'foo' from pod one should call `_sessionget('foo')`.
32 :param string: URL to get without the pod's URL and slash eg. 'stream'.
35 request
= self
.connection
.get(string
)
38 def _sessionpost(self
, string
, data
, headers
={}, params
={}):
39 """This method posts data to session.
40 Performs additional checks if needed.
43 To post to 'foo' one should call `_sessionpost('foo', data={})`.
45 :param string: URL to post without the pod's URL and slash eg. 'status_messages'.
47 :param data: Data to post.
48 :param headers: Headers (optional).
50 :param params: Parameters (optional).
53 request
= self
.connection
.post(string
, data
, headers
, params
)
56 def _sessiondelete(self
, string
, data
, headers
={}):
57 """This method lets you send delete request to session.
58 Performs additional checks if needed.
60 :param string: URL to use.
62 :param data: Data to use.
63 :param headers: Headers to use (optional).
66 request
= self
.connection
.delete(string
, data
, headers
)
70 """This function gets a token needed for authentication in most cases
72 :returns: string -- token used to authenticate
74 r
= self
.connect
.get('stream')
75 token
= self
._token
_regex
.search(r
.text
).group(1)
78 def _setlogindata(self
, username
, password
):
79 """This function is used to set data for login.
81 It should be called before _login() function.
83 self
._username
, self
._password
= username
, password
84 self
._login
_data
= {'user[username]': self
._username
,
85 'user[password]': self
._password
,
86 'authenticity_token': self
.get_token()}
89 """This function is used to connect to the pod and log in.
91 r
= self
._sessionpost
('users/sign_in',
92 data
=self
._login
_data
,
93 headers
={'accept': 'application/json'})
94 if r
.status_code
!= 201:
95 raise Exception('{0}: Login failed.'.format(r
.status_code
))
97 def _setpostdata(self
, text
, aspect_ids
, photos
):
98 """This function prepares data for posting.
100 :param text: Text to post.
102 :param aspect_ids: Aspect ids to send post to.
103 :type aspect_ids: str
106 data
['aspect_ids'] = aspect_ids
107 data
['status_message'] = {'text': text
}
109 data
['photos'] = photos
110 self
._post
_data
= data
113 """Sends post to an aspect.
115 :returns: diaspy.models.Post -- the Post which has been created
117 r
= self
._sessionpost
('status_messages',
118 data
=json
.dumps(self
._post
_data
),
119 headers
={'content-type': 'application/json',
120 'accept': 'application/json',
121 'x-csrf-token': self
.get_token()})
122 if r
.status_code
!= 201:
123 raise Exception('{0}: Post could not be posted.'.format(
126 return diaspy
.models
.Post(str(r
.json()['id']), self
)
128 def post(self
, text
, aspect_ids
='public', photos
=None):
129 """This function sends a post to an aspect
131 :param text: Text to post.
133 :param aspect_ids: Aspect ids to send post to.
134 :type aspect_ids: str
136 :returns: diaspy.models.Post -- the Post which has been created
138 self
._setpostdata
(text
, aspect_ids
, photos
)
143 def get_user_info(self
):
144 """This function returns the current user's attributes.
146 :returns: dict -- json formatted user info.
148 r
= self
.connection
.get('bookmarklet')
149 regex
= re
.compile(r
'window.current_user_attributes = ({.*})')
150 userdata
= json
.loads(regex
.search(r
.text
).group(1))
153 def post_picture(self
, filename
):
154 """This method posts a picture to D*.
156 :param filename: Path to picture file.
159 aspects
= self
.get_user_info()['aspects']
161 params
['photo[pending]'] = 'true'
162 params
['set_profile_image'] = ''
163 params
['qqfile'] = filename
164 for i
, aspect
in enumerate(aspects
):
165 params
['photo[aspect_ids][%d]' % (i
)] = aspect
['id']
167 data
= open(filename
, 'rb')
169 headers
= {'content-type': 'application/octet-stream',
170 'x-csrf-token': self
.get_token(),
171 'x-file-name': filename
}
173 r
= self
._sessionpost
('photos', params
=params
, data
=data
, headers
=headers
)
176 def get_stream(self
):
177 """This functions returns a list of posts found in the stream.
179 :returns: list -- list of Post objects.
181 request
= self
.connection
.get('stream.json')
183 if request
.status_code
!= 200:
184 raise Exception('wrong status code: {0}'.format(request
.status_code
))
186 stream
= request
.json()
187 return [diaspy
.models
.Post(str(post
['id']), self
) for post
in stream
]
189 def get_notifications(self
):
190 """This functions returns a list of notifications.
192 :returns: list -- list of json formatted notifications
194 r
= self
._sessionget
('notifications.json')
196 if r
.status_code
!= 200:
197 raise Exception('wrong status code: {0}'.format(r
.status_code
))
199 notifications
= r
.json()
202 def get_mentions(self
):
203 """This functions returns a list of
204 posts the current user is being mentioned in.
206 :returns: list -- list of Post objects
208 r
= self
._sessionget
('mentions.json')
210 if r
.status_code
!= 200:
211 raise Exception('wrong status code: {0}'.format(r
.status_code
))
214 return [diaspy
.models
.Post(str(post
['id']), self
) for post
in mentions
]
216 def get_tag(self
, tag
):
217 """This functions returns a list of posts containing the tag.
218 :param tag: Name of the tag
221 :returns: list -- list of Post objects
223 r
= self
._sessionget
('tags/{0}.json'.format(tag
))
225 if r
.status_code
!= 200:
226 raise Exception('wrong status code: {0}'.format(r
.status_code
))
228 tagged_posts
= r
.json()
229 return [diaspy
.models
.Post(str(post
['id']), self
) for post
in tagged_posts
]
231 def get_mailbox(self
):
232 """This functions returns a list of messages found in the conversation.
234 :returns: list -- list of Conversation objects.
236 r
= self
._sessionget
('conversations.json')
238 if r
.status_code
!= 200:
239 raise Exception('wrong status code: {0}'.format(r
.status_code
))
242 return [diaspy
.conversations
.Conversation(str(conversation
['conversation']['id']), self
)
243 for conversation
in mailbox
]
245 def add_user_to_aspect(self
, user_id
, aspect_id
):
246 """ this function adds a user to an aspect.
248 :param user_id: User ID
250 :param aspect_id: Aspect ID
254 data
= {'authenticity_token': self
.get_token(),
255 'aspect_id': aspect_id
,
256 'person_id': user_id
}
258 r
= self
._sessionpost
('aspect_memberships.json', data
=data
)
260 if r
.status_code
!= 201:
261 raise Exception('wrong status code: {0}'.format(r
.status_code
))
264 def add_aspect(self
, aspect_name
, visible
=0):
265 """ This function adds a new aspect.
268 data
= {'authenticity_token': self
.get_token(),
269 'aspect[name]': aspect_name
,
270 'aspect[contacts_visible]': visible
}
272 r
= self
._sessionpost
('aspects', data
=data
)
274 if r
.status_code
!= 200:
275 raise Exception('wrong status code: {0}'.format(r
.status_code
))
277 def remove_user_from_aspect(self
, user_id
, aspect_id
):
278 """ this function removes a user from an aspect.
280 :param user_id: User ID
282 :param aspect_id: Aspect ID
286 data
= {'authenticity_token': self
.get_token(),
287 'aspect_id': aspect_id
,
288 'person_id': user_id
}
290 r
= self
._sessiondelete
('aspect_memberships/42.json',
293 if r
.status_code
!= 200:
294 raise Exception('wrong status code: {0}'.format(r
.status_code
))
298 def remove_aspect(self
, aspect_id
):
299 """ This function adds a new aspect.
301 data
= {'authenticity_token': self
.get_token()}
303 r
= self
._sessiondelete
('aspects/{}'.format(aspect_id
),
306 if r
.status_code
!= 404:
307 raise Exception('wrong status code: {0}'.format(r
.status_code
))
309 def new_conversation(self
, contacts
, subject
, text
):
310 """Start a new conversation.
312 :param contacts: recipients ids, no guids, comma sperated.
314 :param subject: subject of the message.
316 :param text: text of the message.
319 data
= {'contact_ids': contacts
,
320 'conversation[subject]': subject
,
321 'conversation[text]': text
,
323 'authenticity_token': self
.get_token()}
325 r
= self
._sessionpost
('conversations/',
327 headers
={'accept': 'application/json'})
328 if r
.status_code
!= 200:
329 raise Exception('{0}: Conversation could not be started.'
330 .format(r
.status_code
))