|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
-
-
-
-
-
-
-
-
-
-
- import os
- import requests
- import base64
- from wand.image import Image
-
-
- ops_url = os.environ.get('OPS_URL')
- ops_url_images = os.environ.get('OPS_URL_IMAGES')
- consumer_key = os.environ.get('CONSUMER_KEY')
- consumer_secret = os.environ.get('CONSUMER_SECRET')
-
- def get_access_token():
-
-
- endpoint_url = ops_url + '3.2/auth/accesstoken'
- auth = consumer_key + ":" + consumer_secret
- auth_bytes = auth.encode("ascii")
- base64_bytes = base64.b64encode(auth_bytes)
- base64_string = base64_bytes.decode("ascii")
-
-
- headers = {"Authorization": "Basic " + base64_string, "Content-Type": "application/x-www-form-urlencoded"}
- data = "grant_type=client_credentials"
-
-
- response = requests.post(endpoint_url, headers=headers, data=data)
-
- if response.status_code == 200:
-
- json = response.json()
- access_token = json['access_token']
-
- return access_token
-
- def get_publication_details(doc_ref):
-
- access_token = get_access_token()
-
-
- endpoint_url = ops_url + 'rest-services/published-data/publication/docdb/' + doc_ref + '/biblio'
-
-
- headers = {"Authorization": "Bearer " + access_token, "Accept": "application/json"}
-
-
- response = requests.get(endpoint_url, headers=headers)
-
- output = {}
-
- if response.status_code == 200:
-
- json = response.json()
-
-
- try:
- json['ops:world-patent-data']['exchange-documents']['exchange-document']['bibliographic-data']['invention-title']
- invention_titles = json['ops:world-patent-data']['exchange-documents']['exchange-document']['bibliographic-data']['invention-title']
- try:
- invention_titles[1]
- for invention_title in invention_titles:
- if invention_title['@lang'] is not None and invention_title['@lang'] != 'en':
- output['original_title'] = invention_title['$']
- except KeyError:
- if invention_titles['@lang'] is not None and invention_titles['@lang'] != 'en':
- output['original_title'] = invention_titles['$']
- except KeyError:
- pass
-
-
- try:
- json['ops:world-patent-data']['exchange-documents']['exchange-document']['abstract']
- abstracts = json['ops:world-patent-data']['exchange-documents']['exchange-document']['abstract']
- try:
- abstracts[1]
- for abstract in abstracts:
- if abstract['@lang'] is not None and abstract['@lang'] != 'en':
- output['original_abstract'] = abstract['p']['$']
- except KeyError:
- if abstracts['@lang'] is not None and abstracts['@lang'] != 'en':
- output['original_abstract'] = abstracts['p']['$']
- except KeyError:
- pass
-
- return output
-
- def get_images(doc_ref):
-
- access_token = get_access_token()
-
-
- endpoint_url = ops_url + 'rest-services/published-data/publication/docdb/' + doc_ref + '/images'
-
-
- headers = {"Authorization": "Bearer " + access_token, "Accept": "application/json"}
-
-
- response = requests.get(endpoint_url, headers=headers)
-
- if response.status_code == 200:
-
- output = {}
- drawings_url = {}
-
-
- json = response.json()
-
- try:
- json['ops:world-patent-data']['ops:document-inquiry']['ops:inquiry-result']['ops:document-instance']
- document_instances = json['ops:world-patent-data']['ops:document-inquiry']['ops:inquiry-result']['ops:document-instance']
- try:
- document_instances[0]
- for document_instance in document_instances:
- if document_instance['@desc'] == 'Drawing':
- drawings_url = ops_url_images + '3.2/rest-services/' + document_instance['@link'] + '?Range=1'
- if drawings_url is None:
- for document_instance in document_instances:
- if document_instance['@desc'] == 'FullDocument':
- drawings_url = ops_url_images + '3.2/rest-services/' + document_instance['@link'] + '?Range=1'
- except KeyError:
- pass
-
- try:
- drawings_url[0]
-
-
- headers = {"Authorization": "Bearer " + access_token, "Accept": "application/tiff"}
-
-
- response = requests.get(drawings_url, headers=headers)
-
- if response.status_code == 200:
- with Image(blob = response.content) as image:
- png_blob = image.make_blob('png')
- base64_bytes = base64.b64encode(png_blob)
- output['image'] = base64_bytes.decode("ascii")
-
- except KeyError:
- pass
-
- except KeyError:
- pass
-
- else:
- output = False
-
- return output
|