8 """This is the client class to connect to diaspora.
12 def __init__(self
, pod
, username
, password
):
14 :param pod: The complete url of the diaspora pod to use.
16 :param username: The username used to log in.
18 :param password: The password used to log in.
22 self
._token
_regex
= re
.compile(r
'content="(.*?)"\s+name="csrf-token')
24 self
.session
= requests
.Session()
25 self
._login
(username
, password
)
28 """This function gets a token needed for authentication in most cases
30 :returns: string -- token used to authenticate
34 r
= self
.session
.get(self
.pod
+ '/stream')
35 token
= self
._token
_regex
.search(r
.text
).group(1)
38 def _login(self
, username
, password
):
39 """This function is used to connect to the pod and log in.
41 This function shouldn't be called manually.
43 self
._username
= username
44 self
._password
= password
45 #r = self.session.get(self.pod + '/users/sign_in')
46 #token = self._token_regex.search(r.text).group(1)
48 data
= {'user[username]': self
._username
,
49 'user[password]': self
._password
,
50 'authenticity_token': self
.get_token()}
52 r
= self
.session
.post(self
.pod
+
55 headers
={'accept': 'application/json'})
57 if r
.status_code
!= 201:
58 raise Exception(str(r
.status_code
) + ': Login failed.')
60 def post(self
, text
, aspect_id
='public', photos
=None):
61 """This function sends a post to an aspect
63 :param text: Text to post.
65 :param aspect_id: Aspect id to send post to.
68 :returns: diaspy.models.Post -- the Post which has been created
71 data
= {'aspect_ids': aspect_id
,
72 'status_message': {'text': text
}}
75 data
['photos'] = photos
76 r
= self
.session
.post(self
.pod
+
78 data
=json
.dumps(data
),
79 headers
={'content-type': 'application/json',
80 'accept': 'application/json',
81 'x-csrf-token': self
.get_token()})
82 if r
.status_code
!= 201:
83 raise Exception(str(r
.status_code
) + ': Post could not be posted.')
85 return diaspy
.models
.Post(str(r
.json()['id']), self
)
87 def get_user_info(self
):
88 """This function returns the current user's attributes.
90 :returns: dict -- json formatted user info.
93 r
= self
.session
.get(self
.pod
+ '/bookmarklet')
94 regex
= re
.compile(r
'window.current_user_attributes = ({.*})')
95 userdata
= json
.loads(regex
.search(r
.text
).group(1))
98 def post_picture(self
, filename
):
99 aspects
= self
.get_user_info()['aspects']
101 params
['photo[pending]'] = 'true'
102 params
['set_profile_image'] = ''
103 params
['qqfile'] = filename
104 for i
, aspect
in enumerate(aspects
):
105 params
['photo[aspect_ids][%d]' % (i
)] = aspect
['id']
107 data
= open(filename
, 'rb')
109 headers
= {'content-type': 'application/octet-stream',
110 'x-csrf-token': self
.get_token(),
111 'x-file-name': filename
}
113 r
= self
.session
.post(self
.pod
+ '/photos',
114 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
147 data
= {'authenticity_token': self
.get_token()}
148 r
= self
.session
.get(self
.pod
+ "/notifications.json")
150 if r
.status_code
!= 200:
151 raise Exception('wrong status code: ' + str(r
.status_code
))
153 notifications
= r
.json()
156 def get_mentions(self
):
157 """This functions returns a list of
158 posts the current user is being mentioned in.
160 :returns: list -- list of Post objects
164 data
= {'authenticity_token': self
.get_token()}
165 r
= self
.session
.get(self
.pod
+ "/mentions.json")
167 if r
.status_code
!= 200:
168 raise Exception('wrong status code: ' + str(r
.status_code
))
174 for post
in mentions
:
175 posts
.append(diaspy
.models
.Post(str(post
['id']), self
))
179 def get_tag(self
, tag
):
180 """This functions returns a list of posts containing the tag.
181 :param tag: Name of the tag
184 :returns: list -- list of Post objects
188 data
= {'authenticity_token': self
.get_token()}
189 r
= self
.session
.get(self
.pod
+ '/tags/' + tag
+ '.json')
191 if r
.status_code
!= 200:
192 raise Exception('wrong status code: ' + str(r
.status_code
))
194 tagged_posts
= r
.json()
198 for post
in tagged_posts
:
199 posts
.append(diaspy
.models
.Post(str(post
['id']), self
))
203 def add_user_to_aspect(self
, user_id
, aspect_id
):
204 """ this function adds a user to an aspect.
206 :param user_id: User ID
208 :param aspect_id: Aspect ID
213 data
= {'authenticity_token': self
.get_token(),
214 'aspect_id': aspect_id
,
215 'person_id': user_id
}
217 r
= self
.session
.post(self
.pod
+ '/aspect_memberships.json',
220 if r
.status_code
!= 201:
221 raise Exception('wrong status code: ' + str(r
.status_code
))
224 def remove_user_from_aspect(self
, user_id
, aspect_id
):
225 """ this function removes a user from an aspect.
227 :param user_id: User ID
229 :param aspect_id: Aspect ID
234 data
= {'authenticity_token': self
.get_token(),
235 'aspect_id': aspect_id
,
236 'person_id': user_id
}
238 r
= self
.session
.delete(self
.pod
+ '/aspect_memberships/42.json',
241 if r
.status_code
!= 200:
242 raise Exception('wrong status code: ' + str(r
.status_code
))
246 def add_aspect(self
, aspect_name
, visible
=0):
247 """ This function adds a new aspect.
250 data
= {'authenticity_token': self
.get_token(),
251 'aspect[name]': aspect_name
,
252 'aspect[contacts_visible]': visible
}
254 r
= self
.session
.post(self
.pod
+ '/aspects',
257 if r
.status_code
!= 200:
258 raise Exception('wrong status code: ' + str(r
.status_code
))
260 def remove_aspect(self
, aspect_id
):
261 """ This function adds a new aspect.
264 data
= {'authenticity_token': self
.get_token()}
266 r
= self
.session
.delete(self
.pod
+ '/aspects/' + aspect_id
,
269 if r
.status_code
!= 404:
270 raise Exception('wrong status code: ' + str(r
.status_code
))