8 class LoginError(Exception):
13 """Object representing connection with the server.
14 It is pushed around internally and is considered private.
16 _token_regex
= re
.compile(r
'content="(.*?)"\s+name="csrf-token')
17 _userinfo_regex
= re
.compile(r
'window.current_user_attributes = ({.*})')
20 def __init__(self
, pod
, username
='', password
=''):
22 :param pod: The complete url of the diaspora pod to use.
24 :param username: The username used to log in.
26 :param password: The password used to log in.
30 self
.session
= requests
.Session()
31 self
._setlogin
(username
, password
)
33 def get(self
, string
):
34 """This method gets data from session.
35 Performs additional checks if needed.
38 To obtain 'foo' from pod one should call `get('foo')`.
40 :param string: URL to get without the pod's URL and slash eg. 'stream'.
43 return self
.session
.get('{0}/{1}'.format(self
.pod
, string
))
45 def post(self
, string
, data
, headers
={}, params
={}):
46 """This method posts data to session.
47 Performs additional checks if needed.
50 To post to 'foo' one should call `post('foo', data={})`.
52 :param string: URL to post without the pod's URL and slash eg. 'status_messages'.
54 :param data: Data to post.
55 :param headers: Headers (optional).
57 :param params: Parameters (optional).
60 string
= '{0}/{1}'.format(self
.pod
, string
)
61 request
= self
.session
.post(string
, data
, headers
=headers
, params
=params
)
64 def delete(self
, string
, data
, headers
={}):
65 """This method lets you send delete request to session.
66 Performs additional checks if needed.
68 :param string: URL to use.
70 :param data: Data to use.
71 :param headers: Headers to use (optional).
74 string
= '{0}/{1}'.format(self
.pod
, string
)
75 request
= self
.session
.delete(string
, data
=data
, headers
=headers
)
78 def _setlogin(self
, username
, password
):
79 """This function is used to set data for login.
81 It should be called before _login() function.
83 self
.username
, self
.password
= username
, password
84 self
.login_data
= {'user[username]': self
.username
,
85 'user[password]': self
.password
,
86 'authenticity_token': self
.get_token()}
89 """Handles actual login request.
90 Raises LoginError if login failed.
92 request
= self
.post('users/sign_in',
94 headers
={'accept': 'application/json'})
95 if request
.status_code
!= 201:
96 raise LoginError('{0}: Login failed.'.format(request
.status_code
))
98 def login(self
, username
='', password
=''):
99 """This function is used to log in to a pod.
100 Will raise LoginError if password or username was not specified.
102 if username
and password
: self
._setlogin
(username
, password
)
103 if not self
.username
or not self
.password
: raise LoginError('password or username not specified')
107 """Logs out from a pod.
108 When logged out you can't do anything.
110 self
.get('users/sign_out')
112 def podswitch(self
, pod
):
113 """Switches pod from current to another one.
118 def getUserInfo(self
):
119 """This function returns the current user's attributes.
121 :returns: dict -- json formatted user info.
123 request
= self
.get('bookmarklet')
124 userdata
= json
.loads(self
._userinfo
_regex
.search(request
.text
).group(1))
128 """This function returns a token needed for authentication in most cases.
130 :returns: string -- token used to authenticate
132 r
= self
.get('stream')
133 token
= self
._token
_regex
.search(r
.text
).group(1)