0a916706f1c2f2ec1e72ec865bfd02100fb35062
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': token
,
53 r
= self
.session
.post(self
.pod
+
56 headers
={'accept': 'application/json'})
58 if r
.status_code
!= 201:
59 raise Exception(str(r
.status_code
) + ': Login failed.')
61 def post(self
, text
, aspect_id
='public'):
62 """This function sends a post to an aspect
64 :param text: Text to post.
66 :param aspect_id: Aspect id to send post to.
70 data
= {'aspect_ids': aspect_id
,
71 'status_message[text]': text
,
72 'authenticity_token': self
.get_token()}
73 r
= self
.session
.post(self
.pod
+
76 headers
={'accept': 'application/json'})
77 if r
.status_code
!= 201:
78 raise Exception(str(r
.status_code
) + ': Post could not be posted.')
82 def get_user_info(self
):
83 """This function returns the current user's attributes.
85 :returns: dict -- json formatted user info.
88 r
= self
.session
.get(self
.pod
+ '/stream')
89 regex
= re
.compile(r
'window.current_user_attributes = ({.*})')
90 userdata
= json
.loads(regex
.search(r
.text
).group(1))
94 """This functions returns a list of posts found in the stream.
96 :returns: list -- list of Post objects.
100 data
= {'authenticity_token': self
.get_token()}
101 r
= self
.session
.get(self
.pod
+ "/stream.json")
103 if r
.status_code
!= 200:
104 raise Exception('wrong status code: ' + str(r
.status_code
))
111 posts
.append(diaspy
.models
.Post(str(post
['id']), self
))
115 def get_notifications(self
):
116 """This functions returns a list of notifications.
118 :returns: list -- list of json formatted notifications
123 data
= {'authenticity_token': self
.get_token()}
124 r
= self
.session
.get(self
.pod
+ "/notifications.json")
126 if r
.status_code
!= 200:
127 raise Exception('wrong status code: ' + str(r
.status_code
))
129 notifications
= r
.json()