Last active
May 24, 2023 10:43
-
-
Save mh61503891/86791e77bdc4fc3fc954431916ec56c1 to your computer and use it in GitHub Desktop.
bib.gs
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
// 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")); | |
} |
Author
mh61503891
commented
May 24, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment