Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
webarchive-api
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
labs-team
webarchive-api
Commits
cede5df7
Commit
cede5df7
authored
3 years ago
by
onbpre
Committed by
csteindl
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
api version 0.2.0
parent
8a329db6
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
webarchiv.py
+30
-27
30 additions, 27 deletions
webarchiv.py
with
30 additions
and
27 deletions
webarchiv.py
+
30
−
27
View file @
cede5df7
import
sys
import
sys
import
time
import
requests
import
hashlib
...
...
@@ -22,6 +23,7 @@ SEARCHMODE_WEBARCHIV = 1
SEARCHMODE_INTERNETARCHIVE
=
2
SEARCHMODE_WEBARCHIV_INTERNETARCHIVE
=
3
class
SessionTimeoutError
(
Exception
):
pass
...
...
@@ -32,7 +34,7 @@ class WebarchivSession:
"""
Current protocol version
"""
return
'
0.
1
.0
'
return
'
0.
2
.0
'
@property
def
api_path
(
self
):
...
...
@@ -93,7 +95,8 @@ class WebarchivSession:
}}
'''
.
format
(
api_key
=
self
.
api_key
,
version
=
self
.
version
,
fingerprint
=
fingerprint
),
headers
=
{
'
content-type
'
:
'
application/json
'
,
'
accept
'
:
'
application/ld+json
'
'
accept
'
:
'
application/ld+json
'
,
'
X-API-VERSION
'
:
self
.
version
}
)
if
r
.
status_code
==
201
:
...
...
@@ -101,17 +104,6 @@ class WebarchivSession:
else
:
raise
HTTPError
(
response
=
r
)
def
_add_api_key_and_token
(
self
,
params_dict
:
dict
):
"""
Add the saved api key and token to a given dictionary.
:param params_dict: A dictionary that
'
s probably used
as a
'
params
'
keyword parameter for calling requests.get().
:return: The same dictionary extended by
'
apikey
'
and
'
t
'
keys.
"""
params_dict
[
'
apikey
'
]
=
self
.
api_key
params_dict
[
'
t
'
]
=
self
.
token
return
params_dict
def
_display_http_error
(
self
,
e
:
HTTPError
):
print
(
self
.
_error_template
.
format
(
status_code
=
e
.
response
.
status_code
,
...
...
@@ -130,31 +122,43 @@ class WebarchivSession:
print
(
'
The requested API Version (via X-API-VERSION Header) is not available
'
,
file
=
sys
.
stderr
)
return
r
def
_get
(
self
,
op
,
auto_connect
=
True
,
**
kwargs
,
):
kwargs
[
'
params
'
]
=
self
.
_add_api_key_and_token
(
kwargs
.
pop
(
'
params
'
,
{}))
def
_get
(
self
,
op
,
auto_connect
=
True
,
**
kwargs
):
r
=
requests
.
get
(
self
.
base_url
.
format
(
op
),
**
kwargs
)
if
r
.
ok
:
return
r
elif
r
.
status_code
==
403
and
auto_connect
:
self
.
connect
()
return
self
.
_get
(
op
=
op
,
auto_connect
=
False
,
**
kwargs
)
kwargs
[
'
headers
'
]
=
{
'
Authorization
'
:
'
Bearer
'
+
self
.
token
}
return
self
.
_get
(
op
,
False
,
**
kwargs
)
else
:
kwargs
[
'
headers
'
]
=
{
'
Authorization
'
:
'
Bearer
'
+
self
.
token
}
return
self
.
_handle_response_errors
(
r
)
def
_post
(
self
,
op
,
auto_connect
=
True
,
json
:
dict
=
None
,
**
kwargs
):
if
not
json
:
json
=
{}
kwargs
[
'
json
'
]
=
self
.
_add_api_key_and_token
(
json
)
if
not
self
.
token
:
kwargs
[
'
headers
'
]
=
{
'
content-type
'
:
'
application/json
'
,
'
accept
'
:
'
application/ld+json
'
'
accept
'
:
'
application/ld+json
'
,
}
r
=
requests
.
post
(
self
.
base_url
.
format
(
op
),
**
kwargs
)
if
r
.
ok
:
else
:
kwargs
[
'
headers
'
]
=
{
'
content-type
'
:
'
application/json
'
,
'
accept
'
:
'
application/ld+json
'
,
'
Authorization
'
:
'
Bearer
'
+
self
.
token
}
r
=
requests
.
post
(
self
.
base_url
.
format
(
op
),
json
=
json
,
**
kwargs
)
if
r
.
status_code
==
201
:
return
r
elif
r
.
status_code
==
403
and
auto_connect
:
self
.
connect
()
return
self
.
_post
(
op
=
op
,
auto_connect
=
False
,
**
kwargs
)
return
self
.
_post
(
op
,
False
,
json
,
**
kwargs
)
else
:
return
self
.
_handle_response_errors
(
r
)
...
...
@@ -278,7 +282,6 @@ class WebarchivSession:
this is the polling request for the given type of request
"""
j
=
resp
.
json
()
context
=
j
[
'
context
'
]
requestid
=
j
[
'
requestid
'
]
type_
=
resp
.
json
()[
'
type
'
]
if
type_
==
1
:
...
...
@@ -339,7 +342,7 @@ class WebarchivSession:
print
(
'
Error:
'
.
format
(
query_string
))
def
get_snapshot_url
(
self
,
seed
,
capture
,
onlysvg
):
return
self
.
api_path
+
'
snapshot?capture=
'
+
capture
+
'
&t=
'
+
self
.
token
+
'
&apikey=
'
+
self
.
api_key
+
'
&onlysvg=
'
+
onlysvg
+
'
&seed=
'
+
seed
return
self
.
api_path
+
'
/
snapshot?capture=
'
+
capture
+
'
&t=
'
+
self
.
token
+
'
&onlysvg=
'
+
onlysvg
+
'
&seed=
'
+
seed
@staticmethod
def
result_contains_seeds
(
response
):
...
...
@@ -373,6 +376,7 @@ class WebarchivSession:
"
occurrence
"
:
occurrence
,
"
extractortype
"
:
EXTRACTOR_HTML
})
response
=
self
.
status_query
(
response
)
return
self
.
wait_for_response
(
response
)
except
HTTPError
as
e
:
...
...
@@ -443,4 +447,3 @@ class WebarchivSession:
return
response
except
HTTPError
as
e
:
self
.
_display_http_error
(
e
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment