7 """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
._login
(username
, password
)
27 """This function gets a token needed for authentication in most cases
29 :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 _login(self
, username
, password
):
38 """This function is used to connect to the pod and log in.
40 This function shouldn't be called manually.
42 self
._username
= username
43 self
._password
= password
44 #r = self.session.get(self.pod + '/users/sign_in')
45 #token = self._token_regex.search(r.text).group(1)
47 data
= {'user[username]': self
._username
,
48 'user[password]': self
._password
,
49 'authenticity_token': self
.get_token()}
51 r
= self
.session
.post(self
.pod
+
54 headers
={'accept': 'application/json'})
56 if r
.status_code
!= 201:
57 raise Exception(str(r
.status_code
) + ': Login failed.')
59 def post(self
, text
, aspect_id
='public', photos
=None):
60 """This function sends a post to an aspect
62 :param text: Text to post.
64 :param aspect_id: Aspect id to send post to.
67 :returns: diaspy.models.Post -- the Post which has been created
70 data
= {'aspect_ids': aspect_id
,
71 'status_message': {'text': text
}}
74 data
['photos'] = photos
75 r
= self
.session
.post(self
.pod
+
77 data
=json
.dumps(data
),
78 headers
={'content-type': 'application/json',
79 'accept': 'application/json',
80 'x-csrf-token': self
.get_token()})
81 if r
.status_code
!= 201:
82 raise Exception(str(r
.status_code
) + ': Post could not be posted.')
84 return diaspy
.models
.Post(str(r
.json()['id']), self
)
86 def get_user_info(self
):
87 """This function returns the current user's attributes.
89 :returns: dict -- json formatted user info.
92 r
= self
.session
.get(self
.pod
+ '/bookmarklet')
93 regex
= re
.compile(r
'window.current_user_attributes = ({.*})')
94 userdata
= json
.loads(regex
.search(r
.text
).group(1))
97 def post_picture(self
, filename
):
98 aspects
= self
.get_user_info()['aspects']
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']
106 data
= open(filename
, 'rb')
108 headers
= {'content-type': 'application/octet-stream',
109 'x-csrf-token': self
.get_token(),
110 'x-file-name': filename
}
112 r
= self
.session
.post(self
.pod
+ '/photos', params
=params
, data
=data
, headers
=headers
)
118 def get_stream(self
):
119 """This functions returns a list of posts found in the stream.
121 :returns: list -- list of Post objects.
125 data
= {'authenticity_token': self
.get_token()}
126 r
= self
.session
.get(self
.pod
+ "/stream.json")
128 if r
.status_code
!= 200:
129 raise Exception('wrong status code: ' + str(r
.status_code
))
136 posts
.append(diaspy
.models
.Post(str(post
['id']), self
))
140 def get_notifications(self
):
141 """This functions returns a list of notifications.
143 :returns: list -- list of json formatted notifications
148 data
= {'authenticity_token': self
.get_token()}
149 r
= self
.session
.get(self
.pod
+ "/notifications.json")
151 if r
.status_code
!= 200:
152 raise Exception('wrong status code: ' + str(r
.status_code
))
154 notifications
= r
.json()
158 def get_mentions(self
):
159 """This functions returns a list of posts the current user is being mentioned in.
161 :returns: list -- list of Post objects
166 data
= {'authenticity_token': self
.get_token()}
167 r
= self
.session
.get(self
.pod
+ "/mentions.json")
169 if r
.status_code
!= 200:
170 raise Exception('wrong status code: ' + str(r
.status_code
))
176 for post
in mentions
:
177 posts
.append(diaspy
.models
.Post(str(post
['id']), self
))
181 def add_user_to_aspect(self
, user_id
, aspect_id
):
182 """ this function adds a user to an aspect.
184 :param user_id: User ID
186 :param aspect_id: Aspect ID
191 data
= {'authenticity_token': self
.get_token(),
192 'aspect_id': aspect_id
,
193 'person_id': user_id
}
195 r
= self
.session
.post(self
.pod
+ '/aspect_memberships.json',
198 if r
.status_code
!= 201:
199 raise Exception('wrong status code: ' + str(r
.status_code
))
202 def remove_user_from_aspect(self
, user_id
, aspect_id
):
203 """ this function removes a user from an aspect.
205 :param user_id: User ID
207 :param aspect_id: Aspect ID
212 data
= {'authenticity_token': self
.get_token(),
213 'aspect_id': aspect_id
,
214 'person_id': user_id
}
216 r
= self
.session
.delete(self
.pod
+ '/aspect_memberships/42.json',
219 if r
.status_code
!= 200:
220 raise Exception('wrong status code: ' + str(r
.status_code
))
224 def add_aspect(self
, aspect_name
, visible
=0):
225 """ This function adds a new aspect.
228 data
= {'authenticity_token': self
.get_token(),
229 'aspect[name]': aspect_name
,
230 'aspect[contacts_visible]': visible
}
232 r
= self
.session
.post(self
.pod
+ '/aspects',
235 if r
.status_code
!= 200:
236 raise Exception('wrong status code: ' + str(r
.status_code
))
238 def remove_aspect(self
, aspect_id
):
239 """ This function adds a new aspect.
242 data
= {'authenticity_token': self
.get_token()}
244 r
= self
.session
.delete(self
.pod
+ '/aspects/' + aspect_id
,
247 if r
.status_code
!= 404:
248 raise Exception('wrong status code: ' + str(r
.status_code
))