From 4ba9f5f8c2409980d84e755ce9ef4f32f47d30ff Mon Sep 17 00:00:00 2001 From: Stefan Karner Date: Thu, 2 May 2019 10:09:18 +0200 Subject: [PATCH] Add allow_tracking option to webarchive session --- ... => 3.1 - IIIF Collection from SPARQL.ipynb | 0 ...Webarchive - Interacting with the API.ipynb | 2 +- webarchiv.py | 18 +++++++++++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) rename 3.3 - IIIF collection from SPARQL.ipynb => 3.1 - IIIF Collection from SPARQL.ipynb (100%) diff --git a/3.3 - IIIF collection from SPARQL.ipynb b/3.1 - IIIF Collection from SPARQL.ipynb similarity index 100% rename from 3.3 - IIIF collection from SPARQL.ipynb rename to 3.1 - IIIF Collection from SPARQL.ipynb diff --git a/4.1 - Webarchive - Interacting with the API.ipynb b/4.1 - Webarchive - Interacting with the API.ipynb index fc5d1ca..e68c12f 100644 --- a/4.1 - Webarchive - Interacting with the API.ipynb +++ b/4.1 - Webarchive - Interacting with the API.ipynb @@ -922,7 +922,7 @@ }, "outputs": [], "source": [ - "session = webarchiv.WebarchivSession(API_KEY)" + "session = webarchiv.WebarchivSession(API_KEY, allow_tracking=True)" ] }, { diff --git a/webarchiv.py b/webarchiv.py index adb0014..af8caf7 100644 --- a/webarchiv.py +++ b/webarchiv.py @@ -1,6 +1,8 @@ import sys import time import requests +import uuid +import hashlib from requests import HTTPError _datetime_format_string = '%Y%m%d%H%M%S' @@ -45,8 +47,9 @@ class WebarchivSession: """ return 'HTTP ERROR - status code {status_code}\n----\n{response_text}\n----\n\n' - def __init__(self, api_key): + def __init__(self, api_key, allow_tracking=False): self.api_key = api_key + self.allow_tracking = allow_tracking self.token = None def connect(self): @@ -59,12 +62,21 @@ class WebarchivSession: self._display_http_error(e) def _authenticate(self): + if self.allow_tracking: + from uuid import getnode as get_mac + mac = get_mac() + sha256 = hashlib.sha256() + sha256.update(str(mac).encode('utf-8')) + fingerprint = sha256.hexdigest() + else: + fingerprint = '' + r = requests.post(self.base_url.format('authentication'), data='''{{ "apikey": "{api_key}", - "fingerprint": "string", + "fingerprint": "{fingerprint}", "version": "{version}" - }}'''.format(api_key=self.api_key, version=self.version), + }}'''.format(api_key=self.api_key, version=self.version, fingerprint=fingerprint), headers={ 'content-type': 'application/json', 'accept': 'application/ld+json' -- GitLab