Last active
June 2, 2019 13:56
-
-
Save AWtnb/c7501f9d8b5f5e0803c3434397a1fa59 to your computer and use it in GitHub Desktop.
get book information by ISBN
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
/* | |
slash command | |
ISBN から書誌情報を取得する | |
自社の本であれば5桁のみでマッチ | |
*/ | |
// ISBN 12桁の末尾にからチェックディジットを追加する関数 | |
function appendCheckDigit (isbn12) { | |
var array = String(isbn12).split("") | |
var total = 0; | |
// 奇数桁 | |
for (var i = 0; i <= 10; i += 2) { | |
total += Number(array[i]) | |
} | |
// 偶数桁 | |
for (var j = 1; j <= 11; j += 2) { | |
total += Number(array[j]) * 3 | |
} | |
var checkD = ((10 - (total % 10))) % 10 | |
return String(isbn12) + String(checkD); | |
} | |
// API 経由で取得した刊行年月日をフォーマットする関数 | |
function formatDateFromAPI (pubdate) { | |
var str = String(pubdate); | |
str = str.replace(/[^\d]/g, ""); | |
var y = str.substring(0, 4); | |
var m = "■" | |
var d = "■" | |
if (str.length >= 6) { | |
m = str.substring(4, 6); | |
} | |
if (str.length == 8) { | |
d = str.substring(6, 9); | |
} | |
return (y + "年" + m + "月" + d + "日") | |
} | |
// ISBN から書誌情報を取得する関数 | |
function getInfoByISBN (ISBN13) { | |
var fivecode = String(ISBN13).substring(7, 12); | |
// openBD API | |
var url = "https://api.openbd.jp/v1/get?isbn=" + ISBN13; | |
var res = UrlFetchApp.fetch(url); | |
if (res == "[null]") { | |
return (ISBN13 + "\t" + fivecode + "\t■\t■\t■\t■") | |
} | |
else { | |
var json = JSON.parse(res); | |
var summary = json[0].summary; | |
var title = summary.title; | |
var authors = summary.author; | |
var publisher = summary.publisher; | |
var pubdate = summary.pubdate; | |
pubdate = formatDateFromAPI(pubdate); | |
return (ISBN13 + "\t" + fivecode + "\t" + title + "\t" + authors + "\t" + publisher + "\t" + pubdate) | |
} | |
} | |
// メイン処理 | |
function mainProc (s) { | |
var lines = String(s).split(/[\r\n]+/g); | |
var ret = []; | |
for (var i = 0; i < lines.length; i++){ | |
var item = String(lines[i]); | |
if (item && item.replace(/\s/g, "")) { | |
if (item.length == 5) { | |
var isbn12 = "9784641" + item; | |
var isbn13 = appendCheckDigit(isbn12); | |
ret.push(getInfoByISBN(isbn13)); | |
} | |
else if (item.length == 12) { | |
var isbn13 = appendCheckDigit(item); | |
ret.push(getInfoByISBN(isbn13)); | |
} | |
else if (item.length == 13) { | |
ret.push(getInfoByISBN(item)); | |
} | |
else { | |
ret.push("■" + "\t" + item + "\t■\t■\t■\t■") | |
} | |
} | |
} | |
return ret; | |
} | |
// 応答処理 | |
function doPost (e) { | |
var txt = e.parameter.text; | |
var infos = mainProc(txt); | |
var message = infos.join("\n"); | |
var response = { | |
"text": message | |
}; | |
return ContentService.createTextOutput(JSON.stringify(response)).setMimeType(ContentService.MimeType.JSON); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment