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': 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'):
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.
69 data
= {'aspect_ids': aspect_id
,
70 'status_message[text]': text
,
71 'authenticity_token': self
.get_token()}
72 r
= self
.session
.post(self
.pod
+
75 headers
={'accept': 'application/json'})
76 if r
.status_code
!= 201:
77 raise Exception(str(r
.status_code
) + ': Post could not be posted.')
81 def get_user_info(self
):
82 """This function returns the current user's attributes.
84 :returns: dict -- json formatted user info.
87 r
= self
.session
.get(self
.pod
+ '/stream')
88 regex
= re
.compile(r
'window.current_user_attributes = ({.*})')
89 userdata
= json
.loads(regex
.search(r
.text
).group(1))