b5910131979b68f0fd344ebabbaefa6928ee8770
2 # -*- coding: utf-8 -*-
5 """This module abstracts connection to pod.
14 from diaspy
import errors
21 """Object representing connection with the pod.
23 _token_regex
= re
.compile(r
'content="(.*?)"\s+name="csrf-token')
24 _userinfo_regex
= re
.compile(r
'window.current_user_attributes = ({.*})')
25 # this is for older version of D*
26 _userinfo_regex_2
= re
.compile(r
'gon.user=({.*});gon.preloads')
28 def __init__(self
, pod
, username
, password
, schema
='https'):
30 :param pod: The complete url of the diaspora pod to use.
32 :param username: The username used to log in.
34 :param password: The password used to log in.
38 self
._session
= requests
.Session()
39 self
._login
_data
= {'user[remember_me]': 1, 'utf8': '✓'}
42 self
._diaspora
_session
= ''
43 self
._cookies
= self
._fetchcookies
()
45 #self._setlogin(username, password)
46 self
._login
_data
= {'user[username]': username
,
47 'user[password]': password
,
48 'authenticity_token': self
._fetchtoken
()}
50 except requests
.exceptions
.MissingSchema
:
51 self
.pod
= '{0}://{1}'.format(schema
, self
.pod
)
52 warnings
.warn('schema was missing')
58 self
._login
_data
= {'user[username]': username
,
59 'user[password]': password
,
60 'authenticity_token': self
._fetchtoken
()}
61 except Exception as e
:
62 raise errors
.LoginError('cannot create login data (caused by: {0})'.format(e
))
64 def _fetchcookies(self
):
65 request
= self
.get('stream')
66 return request
.cookies
69 """Returns token string.
70 It will be easier to change backend if programs will just use:
72 instead of calling a specified method.
74 return self
._fetchtoken
()
76 def get(self
, string
, headers
={}, params
={}, direct
=False, **kwargs
):
77 """This method gets data from session.
78 Performs additional checks if needed.
81 To obtain 'foo' from pod one should call `get('foo')`.
83 :param string: URL to get without the pod's URL and slash eg. 'stream'.
85 :param direct: if passed as True it will not be expanded
88 if not direct
: url
= '{0}/{1}'.format(self
.pod
, string
)
90 return self
._session
.get(url
, params
=params
, headers
=headers
, **kwargs
)
92 def post(self
, string
, data
, headers
={}, params
={}, **kwargs
):
93 """This method posts data to session.
94 Performs additional checks if needed.
97 To post to 'foo' one should call `post('foo', data={})`.
99 :param string: URL to post without the pod's URL and slash eg. 'status_messages'.
101 :param data: Data to post.
102 :param headers: Headers (optional).
104 :param params: Parameters (optional).
107 string
= '{0}/{1}'.format(self
.pod
, string
)
108 request
= self
._session
.post(string
, data
, headers
=headers
, params
=params
, **kwargs
)
111 def put(self
, string
, data
=None, headers
={}, params
={}, **kwargs
):
112 """This method PUTs to session.
114 string
= '{0}/{1}'.format(self
.pod
, string
)
115 if data
is not None: request
= self
._session
.put(string
, data
, headers
=headers
, params
=params
, **kwargs
)
116 else: request
= self
._session
.put(string
, headers
=headers
, params
=params
, **kwargs
)
119 def delete(self
, string
, data
, headers
={}, **kwargs
):
120 """This method lets you send delete request to session.
121 Performs additional checks if needed.
123 :param string: URL to use.
125 :param data: Data to use.
126 :param headers: Headers to use (optional).
129 string
= '{0}/{1}'.format(self
.pod
, string
)
130 request
= self
._session
.delete(string
, data
=data
, headers
=headers
, **kwargs
)
133 def _setlogin(self
, username
, password
):
134 """This function is used to set data for login.
137 It should be called before _login() function.
139 self
._login
_data
= {'user[username]': username
,
140 'user[password]': password
,
141 'authenticity_token': self
._fetchtoken
()}
144 """Handles actual login request.
145 Raises LoginError if login failed.
147 request
= self
.post('users/sign_in',
148 data
=self
._login
_data
,
149 allow_redirects
=False)
150 if request
.status_code
!= 302:
151 raise errors
.LoginError('{0}: login failed'.format(request
.status_code
))
153 def login(self
, remember_me
=1):
154 """This function is used to log in to a pod.
155 Will raise LoginError if password or username was not specified.
157 if not self
._login
_data
['user[username]'] or not self
._login
_data
['user[password]']:
158 raise errors
.LoginError('username and/or password is not specified')
159 self
._login
_data
['user[remember_me]'] = remember_me
160 status
= self
._login
()
161 self
._login
_data
= {}
165 """Logs out from a pod.
166 When logged out you can't do anything.
168 self
.get('users/sign_out')
171 def podswitch(self
, pod
, username
, password
):
172 """Switches pod from current to another one.
175 self
._setlogin
(username
, password
)
178 def _fetchtoken(self
):
179 """This method tries to get token string needed for authentication on D*.
181 :returns: token string
183 request
= self
.get('stream')
184 token
= self
._token
_regex
.search(request
.text
).group(1)
188 def get_token(self
, fetch
=True):
189 """This function returns a token needed for authentication in most cases.
190 **Notice:** using repr() is recommended method for getting token.
192 Each time it is run a _fetchtoken() is called and refreshed token is stored.
194 It is more safe to use than _fetchtoken().
195 By setting new you can request new token or decide to get stored one.
196 If no token is stored new one will be fetched anyway.
198 :returns: string -- token used to authenticate
201 if fetch
or not self
._token
: self
._fetchtoken
()
202 except requests
.exceptions
.ConnectionError
as e
:
203 warnings
.warn('{0} was cought: reusing old token'.format(e
))
205 if not self
._token
: raise errors
.TokenError('cannot obtain token and no previous token found for reuse')
208 def getSessionToken(self
):
209 """Returns session token string (_diaspora_session).
211 return self
._diaspora
_session
213 def getUserData(self
):
214 """Returns user data.
216 request
= self
.get('bookmarklet')
217 userdata
= self
._userinfo
_regex
.search(request
.text
)
218 if userdata
is None: userdata
= self
._userinfo
_regex
_2.search(request
.text
)
219 if userdata
is None: raise errors
.DiaspyError('cannot find user data')
220 userdata
= userdata
.group(1)
221 return json
.loads(userdata
)