7 """This is the client class to connect to diaspora.
11 Before calling any other function
12 you have to call :func:`diaspy.Client.login`.
16 def __init__(self
, pod
):
17 self
._token
_regex
= re
.compile(r
'content="(.*?)"\s+name="csrf-token')
19 self
.session
= requests
.Session()
22 """This function gets a token needed for authentication in most cases
24 :returns: string -- token used to authenticate
28 r
= self
.session
.get(self
.pod
+ "/stream")
29 token
= self
._token
_regex
.search(r
.text
).group(1)
32 def login(self
, username
, password
):
33 """This function is used to connect to the pod and log in.
35 :param username: The username used to log in.
37 :param password: The password used to log in.
41 self
._username
= username
42 self
._password
= password
43 r
= self
.session
.get(self
.pod
+ "/users/sign_in")
44 token
= self
._token
_regex
.search(r
.text
).group(1)
46 data
= {'user[username]': self
._username
,
47 'user[password]': self
._password
,
48 'authenticity_token': token
,
51 r
= self
.session
.post(self
.pod
+ "/users/sign_in", data
=data
)
53 def post(self
, text
, aspect_id
='public'):
54 """This function sends a post to an aspect
56 :param text: Text to post.
58 :param aspect_id: Aspect id to send post to.
62 data
= {'aspect_ids': aspect_id
,
63 'status_message[text]': text
,
64 'authenticity_token': self
.get_token()}
65 r
= self
.session
.post(self
.pod
+ "/status_messages", data
=data
)
67 def get_user_info(self
):
68 """This function returns the current user's attributes.
70 :returns: dict -- json formatted user info.
73 r
= self
.session
.get(self
.pod
+ "/stream")
74 regex
= re
.compile(r
'window.current_user_attributes = ({.*})')
75 userdata
= json
.loads(regex
.search(r
.text
).group(1))