8 class LoginError(Exception):
13 """Object representing connection with the server.
14 It is pushed around internally and is considered private.
16 def __init__(self
, pod
, username
='', password
=''):
18 :param pod: The complete url of the diaspora pod to use.
20 :param username: The username used to log in.
22 :param password: The password used to log in.
26 self
.session
= requests
.Session()
27 self
._token
_regex
= re
.compile(r
'content="(.*?)"\s+name="csrf-token')
28 self
._userinfo
_regex
= re
.compile(r
'window.current_user_attributes = ({.*})')
29 self
._setlogin
(username
, password
)
31 def get(self
, string
):
32 """This method gets data from session.
33 Performs additional checks if needed.
36 To obtain 'foo' from pod one should call `get('foo')`.
38 :param string: URL to get without the pod's URL and slash eg. 'stream'.
41 return self
.session
.get('{0}/{1}'.format(self
.pod
, string
))
43 def post(self
, string
, data
, headers
={}, params
={}):
44 """This method posts data to session.
45 Performs additional checks if needed.
48 To post to 'foo' one should call `post('foo', data={})`.
50 :param string: URL to post without the pod's URL and slash eg. 'status_messages'.
52 :param data: Data to post.
53 :param headers: Headers (optional).
55 :param params: Parameters (optional).
58 string
= '{0}/{1}'.format(self
.pod
, string
)
59 if headers
and params
:
60 request
= self
.session
.post(string
, data
=data
, headers
=headers
, params
=params
)
61 elif headers
and not params
:
62 request
= self
.session
.post(string
, data
=data
, headers
=headers
)
63 elif not headers
and params
:
64 request
= self
.session
.post(string
, data
=data
, params
=params
)
66 request
= self
.session
.post(string
, data
=data
)
69 def delete(self
, string
, data
, headers
={}):
70 """This method lets you send delete request to session.
71 Performs additional checks if needed.
73 :param string: URL to use.
75 :param data: Data to use.
76 :param headers: Headers to use (optional).
79 string
= '{0}/{1}'.format(self
.pod
, string
)
81 request
= self
.session
.delete(string
, data
=data
, headers
=headers
)
83 request
= self
.session
.delete(string
, data
=data
)
86 def _setlogin(self
, username
, password
):
87 """This function is used to set data for login.
89 It should be called before _login() function.
91 self
.username
, self
.password
= username
, password
92 self
.login_data
= {'user[username]': self
.username
,
93 'user[password]': self
.password
,
94 'authenticity_token': self
.getToken()}
97 """Handles actual login request.
98 Raises LoginError if login failed.
100 request
= self
.post('users/sign_in',
101 data
=self
.login_data
,
102 headers
={'accept': 'application/json'})
103 if request
.status_code
!= 201:
104 raise LoginError('{0}: Login failed.'.format(request
.status_code
))
106 def login(self
, username
='', password
=''):
107 """This function is used to log in to a pod.
108 Will raise LoginError if password or username was not specified.
110 if username
and password
: self
._setlogin
(username
, password
)
111 if not self
.username
or not self
.password
: raise LoginError('password or username not specified')
114 def podswitch(self
, pod
):
115 """Switches pod from current to another one.
120 def getUserInfo(self
):
121 """This function returns the current user's attributes.
123 :returns: dict -- json formatted user info.
125 request
= self
.get('bookmarklet')
126 userdata
= json
.loads(self
._userinfo
_regex
.search(request
.text
).group(1))
130 """This function returns a token needed for authentication in most cases.
132 :returns: string -- token used to authenticate
134 r
= self
.get('stream')
135 token
= self
._token
_regex
.search(r
.text
).group(1)