Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F80806969
tequila.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Sep 2, 16:50
Size
2 KB
Mime Type
text/x-python
Expires
Wed, Sep 4, 16:50 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
20453279
Attached To
rPHAPI Phabricator API scripts
tequila.py
View Options
# -*- coding: utf-8 -*-
'''
This code is adapted from the conde of Antoine Albertelli, found here:
https://github.com/antoinealb/python-tequila
'''
import
getpass
import
requests
from
bs4
import
BeautifulSoup
import
logging
try
:
import
urlparse
except
ImportError
:
import
urllib.parse
as
urlparse
from
...utils
import
get_password
from
...
import
colored
from
...
import
color_code
__author__
=
"Nicolas Richart"
__copyright__
=
"Copyright (C) 2016, EPFL (Ecole Polytechnique Fédérale "
\
"de Lausanne) - SCITAS (Scientific IT and Application "
\
"Support)
\n\n
"
\
"Copyright (c) 2013, Antoine Albertelli"
__credits__
=
[
"Antoine Albertelli"
,
"Nicolas Richart"
]
__license__
=
"BSD"
__version__
=
"0.1"
__maintainer__
=
"Nicolas Richart"
__email__
=
"nicolas.richart@epfl.ch"
logging
.
getLogger
(
'requests'
)
.
setLevel
(
logging
.
WARNING
)
_logger
=
logging
.
getLogger
(
__name__
)
class
TequilaGet
(
object
):
"""Get url that needs tequila authentiication"""
TEQUILA_LOGIN_POST
=
"https://tequila.epfl.ch/cgi-bin/tequila/login"
__session
=
None
class
TequilaError
(
RuntimeError
):
"""
Exception thrown in case of Tequila error.
"""
pass
def
__init__
(
self
,
url
,
username
=
None
,
password
=
None
,
**
kwargs
):
"""
Explicitly login into the tequila service, this will create
a new tequila session.
:raise TequilaError:
"""
self
.
__session
=
requests
.
session
()
resp
=
self
.
__session
.
get
(
url
)
if
resp
.
status_code
!=
200
:
raise
TequilaGet
.
TequilaError
(
'Cannot access {0}'
.
format
(
url
))
parsed_url
=
urlparse
.
urlsplit
(
resp
.
url
)
dict_query
=
urlparse
.
parse_qs
(
parsed_url
.
query
)
sesskey
=
dict_query
[
'requestkey'
][
0
]
if
username
is
None
:
raise
TequilaGet
.
TequilaError
(
'No username was provided'
)
_logger
.
info
(
"Getting a Tequilla context for user
\'
{0}
\'
"
.
format
(
colored
(
username
,
color_code
[
'user'
])))
if
password
is
None
:
password
=
get_password
(
'tequila-epfl'
,
username
,
**
kwargs
)
payload
=
dict
()
payload
[
"requestkey"
]
=
sesskey
payload
[
"username"
]
=
username
payload
[
"password"
]
=
password
resp
=
self
.
__session
.
post
(
self
.
TEQUILA_LOGIN_POST
,
verify
=
True
,
data
=
payload
)
if
resp
.
status_code
!=
200
:
raise
TequilaGet
.
TequilaError
(
"Tequila didn't return a 200 code"
)
soup
=
BeautifulSoup
(
resp
.
text
,
'html.parser'
)
error
=
soup
.
find
(
'font'
,
color
=
'red'
,
size
=
'+1'
)
if
error
:
# Grab the tequila error if any
raise
TequilaGet
.
TequilaError
(
error
.
string
)
def
get
(
self
,
url
):
"""Get an url in a authenticated session"""
resp
=
self
.
__session
.
get
(
url
)
if
resp
.
status_code
!=
200
:
raise
TequilaGet
.
TequilaError
(
'Access restricted'
)
return
resp
Event Timeline
Log In to Comment