Skip to content

Instantly share code, notes, and snippets.

@mh61503891
Last active May 24, 2023 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mh61503891/86791e77bdc4fc3fc954431916ec56c1 to your computer and use it in GitHub Desktop.
Save mh61503891/86791e77bdc4fc3fc954431916ec56c1 to your computer and use it in GitHub Desktop.
bib.gs
// Googleシートからは `=TOROW(BIB(A1))` のように使う。
function BIB(isbn) {
return _getBibEntriesAsListByIsbn(isbn);
}
function _getBibEntriesAsListByIsbn(isbn) {
const entries = _getBibEntriesAsMapByIsbn(isbn);
return [
entries.isbn,
entries.title,
entries.author,
entries.pubdate,
entries.publisher,
entries.cover,
];
}
function _getBibEntriesAsMapByIsbn(isbn) {
const res = _fetchBibEntriesByIsbn(isbn);
return JSON.parse(res)[0]["summary"];
}
function _fetchBibEntriesByIsbn(isbn) {
const cache = CacheService.getScriptCache();
const cached = cache.get(isbn);
if (cached != null) {
return cached;
}
const url = _IsbnToBibApiURL(isbn);
const res = UrlFetchApp.fetch(url).getContentText();
cache.put(isbn, res);
return res;
}
function _IsbnToBibApiURL(isbn) {
const ENDPOINT_URL = "https://api.openbd.jp/v1/get";
const params = {
"isbn": isbn,
};
const query = Object.keys(params).map(key => {
return `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`;
}).join("&");
return ENDPOINT_URL + "?" + query;
}
function _debug() {
Logger.log(BIB("9784320123021"));
}
@mh61503891
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment