-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
MagicalLiebe
committed
Jul 6, 2021
1 parent
952e19c
commit 22335c6
Showing
6 changed files
with
126 additions
and
150 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import moji from "moji"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
const compare = (a: bookData, b: bookData) => { | ||
if (a.title < b.title) { | ||
return -1; | ||
} else { | ||
return 1; | ||
} | ||
}; | ||
|
||
type rakutenItem = { | ||
Item: bookItem; | ||
}; | ||
|
||
type bookItem = { | ||
title: string; | ||
author: string; | ||
publisherName: string; | ||
largeImageUrl: string; | ||
isbn: string; | ||
}; | ||
|
||
export type bookData = { | ||
title: string; | ||
author: string; | ||
publisherName: string; | ||
imageUrl: string; | ||
isbn: string; | ||
}; | ||
|
||
const convertToUtf8 = (text: string) => { | ||
return unescape(encodeURIComponent(text)); | ||
}; | ||
|
||
const extractData = (item: rakutenItem) => { | ||
let titleString = item.Item.title; | ||
titleString = moji(titleString).convert("ZE", "HE").toString(); | ||
titleString = moji(titleString).convert("ZS", "HS").toString(); | ||
let authorString = item.Item.author; | ||
authorString = moji(authorString).convert("ZE", "HE").toString(); | ||
authorString = moji(authorString).convert("ZS", "HS").reject("HS").toString(); | ||
const data: bookData = { | ||
title: titleString, | ||
author: authorString, | ||
publisherName: item.Item.publisherName, | ||
imageUrl: item.Item.largeImageUrl, | ||
isbn: item.Item.isbn, | ||
}; | ||
return data; | ||
}; | ||
|
||
const Rakuten = async (req: NextApiRequest, res: NextApiResponse) => { | ||
let url = | ||
"https://app.rakuten.co.jp/services/api/BooksBook/Search/20170404?applicationId=" + | ||
process.env.RAKUTEN_APP_ID + | ||
"&booksGenreId=001001"; | ||
|
||
let { title } = req.query; | ||
if (title) { | ||
title = title.toString(); | ||
url += "&title=" + convertToUtf8(title); | ||
} | ||
|
||
let { author } = req.query; | ||
if (author) { | ||
author = author.toString(); | ||
url += "&author=" + convertToUtf8(author); | ||
} | ||
|
||
if (title || author) { | ||
let bookList: bookData[] = []; | ||
let count = -1; | ||
const response = await fetch(url); | ||
const data = await response.json(); | ||
if (data) { | ||
data.Items.map((item: rakutenItem) => { | ||
bookList = [...bookList, extractData(item)]; | ||
}); | ||
count = data.count; | ||
|
||
for (let i = 1; i < data.pageCount; i++) { | ||
await new Promise((resolve) => setTimeout(resolve, 300)); | ||
const response = await fetch(url + "&page=" + (i + 1)); | ||
const data = await response.json(); | ||
if (data) { | ||
data.Items.map((item: rakutenItem) => { | ||
bookList = [...bookList, extractData(item)]; | ||
}); | ||
} | ||
} | ||
} | ||
|
||
if (count != bookList.length) { | ||
res.status(500).json({ massege: "Error: Rakuten Book API." }); | ||
} else { | ||
bookList.sort(compare); | ||
res.status(200).json({ data: bookList, size: count }); | ||
} | ||
} else { | ||
res | ||
.status(500) | ||
.json({ massege: "Error: Please set title or author to query." }); | ||
} | ||
}; | ||
|
||
export default Rakuten; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,6 +223,11 @@ | |
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" | ||
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4= | ||
|
||
"@types/moji@^0.5.0": | ||
version "0.5.0" | ||
resolved "https://registry.yarnpkg.com/@types/moji/-/moji-0.5.0.tgz#4b68f789702baeb9aa5482ea68191ca4080c8b2b" | ||
integrity sha512-oZhMgrG0iv5u1yICABzV4NrlpGOE7hNapc3atIMiQ1ebWOTkay/x1eMYbQPXjCxxk14Td3cqy7S+kvmGdE7NGA== | ||
|
||
"@types/node@*": | ||
version "15.12.5" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-15.12.5.tgz#9a78318a45d75c9523d2396131bd3cca54b2d185" | ||
|
@@ -2232,6 +2237,13 @@ modern-normalize@^1.1.0: | |
resolved "https://registry.yarnpkg.com/modern-normalize/-/modern-normalize-1.1.0.tgz#da8e80140d9221426bd4f725c6e11283d34f90b7" | ||
integrity sha512-2lMlY1Yc1+CUy0gw4H95uNN7vjbpoED7NNRSBHE25nWfLBdmMzFCsPshlzbxHz+gYMcBEUN8V4pU16prcdPSgA== | ||
|
||
moji@^0.5.1: | ||
version "0.5.1" | ||
resolved "https://registry.yarnpkg.com/moji/-/moji-0.5.1.tgz#088eecd1c22c8f31a240adcf9c95e54f33eb54fb" | ||
integrity sha1-CI7s0cIsjzGiQK3PnJXlTzPrVPs= | ||
dependencies: | ||
object-assign "^3.0.0" | ||
|
||
[email protected]: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" | ||
|
@@ -2403,6 +2415,11 @@ normalize-range@^0.1.2: | |
resolved "https://registry.yarnpkg.com/normalize-range/-/normalize-range-0.1.2.tgz#2d10c06bdfd312ea9777695a4d28439456b75942" | ||
integrity sha1-LRDAa9/TEuqXd2laTShDlFa3WUI= | ||
|
||
object-assign@^3.0.0: | ||
version "3.0.0" | ||
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-3.0.0.tgz#9bedd5ca0897949bca47e7ff408062d549f587f2" | ||
integrity sha1-m+3VygiXlJvKR+f/QIBi1Un1h/I= | ||
|
||
object-assign@^4.1.1: | ||
version "4.1.1" | ||
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" | ||
|