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 = ({.*})')
30 self
._setlogin
(username
, password
)
32 def get(self
, string
):
33 """This method gets data from session.
34 Performs additional checks if needed.
37 To obtain 'foo' from pod one should call `get('foo')`.
39 :param string: URL to get without the pod's URL and slash eg. 'stream'.
42 return self
.session
.get('{0}/{1}'.format(self
.pod
, string
))
44 def post(self
, string
, data
, headers
={}, params
={}):
45 """This method posts data to session.
46 Performs additional checks if needed.
49 To post to 'foo' one should call `post('foo', data={})`.
51 :param string: URL to post without the pod's URL and slash eg. 'status_messages'.
53 :param data: Data to post.
54 :param headers: Headers (optional).
56 :param params: Parameters (optional).
59 string
= '{0}/{1}'.format(self
.pod
, string
)
60 request
= self
.session
.post(string
, data
, headers
=headers
, params
=params
)
63 def delete(self
, string
, data
, headers
={}):
64 """This method lets you send delete request to session.
65 Performs additional checks if needed.
67 :param string: URL to use.
69 :param data: Data to use.
70 :param headers: Headers to use (optional).
73 string
= '{0}/{1}'.format(self
.pod
, string
)
74 request
= self
.session
.delete(string
, data
=data
, headers
=headers
)
77 def _setlogin(self
, username
, password
):
78 """This function is used to set data for login.
80 It should be called before _login() function.
82 self
.username
, self
.password
= username
, password
83 self
.login_data
= {'user[username]': self
.username
,
84 'user[password]': self
.password
,
85 'authenticity_token': self
.get_token()}
88 """Handles actual login request.
89 Raises LoginError if login failed.
91 request
= self
.post('users/sign_in',
93 headers
={'accept': 'application/json'})
94 if request
.status_code
!= 201:
95 raise LoginError('{0}: Login failed.'.format(request
.status_code
))
97 def login(self
, username
='', password
=''):
98 """This function is used to log in to a pod.
99 Will raise LoginError if password or username was not specified.
101 if username
and password
: self
._setlogin
(username
, password
)
102 if not self
.username
or not self
.password
: raise LoginError('password or username not specified')
105 def podswitch(self
, pod
):
106 """Switches pod from current to another one.
111 def getUserInfo(self
):
112 """This function returns the current user's attributes.
114 :returns: dict -- json formatted user info.
116 request
= self
.get('bookmarklet')
117 userdata
= json
.loads(self
._userinfo
_regex
.search(request
.text
).group(1))
121 """This function returns a token needed for authentication in most cases.
123 :returns: string -- token used to authenticate
125 r
= self
.get('stream')
126 token
= self
._token
_regex
.search(r
.text
).group(1)