Last active
January 25, 2019 04:40
-
-
Save ryuuji/f18ae84f7d73c069be47bf3afbcb7de7 to your computer and use it in GitHub Desktop.
Summonへのアクセス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from email.utils import formatdate | |
import hmac | |
import hashlib | |
import base64 | |
import json | |
secret_key="xxxxxxxxx" | |
userid="xxxxxx" | |
keyword = "ryuuji+yoshimoto" | |
dt = formatdate(timeval=None, localtime=False, usegmt=True) | |
text = "application/json" + "\n" + dt + "\n" + "api.summon.serialssolutions.com" + "\n" + "/2.0.0/search" + "\n" + "q=calil.jp&l=jp&ho=t" + "\n" | |
digest=hmac.new(secret_key.encode("utf-8"),text.encode('utf-8'),hashlib.sha1).digest() | |
signature=str(base64.b64encode(digest),'utf-8') | |
print("signed id>>---") | |
print(text) | |
print("---") | |
print("bytecode-digest",digest) | |
print("base64-signature",signature) | |
r = requests.get("http://api.summon.serialssolutions.com/2.0.0/search?q=calil.jp&l=jp&ho=t",headers={'Accept':'application/json','User-Agent': 'tsukuba-trial','x-summon-date':dt,'Authorization':'Summon '+userid+';'+signature}) | |
data = r.json() | |
for item in data['documents']: | |
print(item['ID'],item['Title'][0]) | |
#print(json.dumps(data,indent=2)) | |
print(r.elapsed.total_seconds()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import base64 | |
import hashlib | |
import hmac | |
from urllib.parse import urlencode, quote_plus, unquote_plus | |
from datetime import datetime | |
import requests | |
def search(access_id, api_key, params): | |
""" | |
Searches the Summon API with the provided parameters. | |
:param access_id: Your Summon API Access ID | |
:param api_key: Your Summon API Key | |
:param params: Search parameters, sent as dictionary or list of tuples | |
:returns: JSON response from Summon API containing search results, etc. | |
""" | |
host = 'api.summon.serialssolutions.com' | |
path = '/2.0.0/search' | |
query = urlencode(params, doseq=True, quote_via=quote_plus) | |
headers = build_headers(access_id, api_key, host, path, query) | |
print('Request headers: {}'.format(headers)) | |
url = 'http://{}{}?{}'.format(host, | |
path, | |
query) | |
response = requests.get(url, headers=headers) | |
response.raise_for_status() | |
return response.text | |
def build_headers(access_id, api_key, host, path, query): | |
""" | |
Generates the request headers for the Summon API query. | |
:param access_id: Your Summon API Access ID | |
:param api_key: Your Summon API Key | |
:param host: The Summon API host domain, defined in search function | |
:param path: The Summon API search path, defined in search function | |
:param query: URL-encoded query, created from params by search function | |
:returns: Dictionary containing request headers for Summon API query | |
""" | |
# application/xml also supported by Summon API but not used in this demo | |
accept = 'application/json' | |
date = datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT') | |
# sort and decode query | |
query_string = unquote_plus('&'.join(sorted(query.split('&')))) | |
id_string = "\n".join([accept, date, host, path, query_string]) + "\n" | |
auth_string = build_auth_string(access_id, api_key, id_string) | |
return {'Accept': accept, | |
'x-summon-date': date, | |
'Host': host, | |
'Authorization': auth_string} | |
def build_auth_string(access_id, api_key, id_string): | |
""" | |
Generates authentication string needed for Authorization header. | |
:param access_id: Your Summon API Access ID | |
:param api_key: Your Summon API Key | |
:param id_string: string from build_headers combining query, etc. | |
:returns: string "Summon your_access_id;base64_encoded_hash" | |
""" | |
key = bytes(api_key, 'UTF-8') | |
message = bytes(id_string, 'UTF-8') | |
hashed_code = hmac.new(key, | |
message, | |
hashlib.sha1).digest() | |
digest = base64.encodebytes(hashed_code).decode('UTF-8') | |
auth_string = 'Summon {};{}'.format(access_id, digest) | |
return auth_string.replace('\n', '') | |
secret_key="xxxxx" | |
userid="xxxxx" | |
r = search(userid,secret_key,{'s.q':u'文教大学越谷図書館資料受入システム','s.l':'jp','s.light':'t','s.ps':'50','s.role':'authenticated','s.ho':'t'}) #,'s.ho':'f' | |
import json | |
data = json.loads(r) | |
for item in data['documents']: | |
print(item['ID'],item['Title'][0],item.get('DOI',[""])[0],item.get('Score',[""])[0]) | |
##print(item) | |
with open('ok','wb') as f: | |
f.write(r.encode('utf-8')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment