Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
35 lines (34 sloc)
1015 Bytes
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 { NowRequest, NowResponse } from "@now/node"; | |
import { getGoodreadBooks } from "../helpers/goodreads"; | |
import { safeDump } from "js-yaml"; | |
import { writeGitHubFile } from "../helpers/github"; | |
export default async (req: NowRequest, res: NowResponse) => { | |
try { | |
const result = [ | |
...(await getGoodreadBooks("currently-reading")), | |
...(await getGoodreadBooks("read")) | |
] | |
.sort( | |
(a, b) => | |
new Date(a.readAt || a.dateAdded).getTime() - | |
new Date(b.readAt || b.dateAdded).getTime() | |
) | |
.reverse(); | |
const booksMap = new Map(result.map(o => [o.title, o])); | |
const uniqueBooks = [...booksMap.values()]; | |
await writeGitHubFile( | |
"AnandChowdhary/life-data", | |
"books.yml", | |
`📘 ${[...uniqueBooks] | |
.slice(0, 5) | |
.map(i => i.title) | |
.join(", ")}`, | |
safeDump(uniqueBooks) | |
); | |
res.json({ done: true }); | |
} catch (error) { | |
res.status(500); | |
console.log(error); | |
res.json({ error }); | |
} | |
}; |