-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.ts
58 lines (52 loc) · 1.42 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import got from 'got';
import xml from 'fast-xml-parser';
import dotenv from 'dotenv';
import { Octokit } from '@octokit/rest';
dotenv.config();
const octokit = new Octokit({ auth: process.env.GH_TOKEN });
const request = got.extend({
prefixUrl: 'https://www.goodreads.com',
searchParams: {
v: 2,
key: process.env.GOODREADS_API_KEY,
},
});
export async function getReviewList(
shelf = 'currently-reading'
): Promise<Goodreads.Review[]> {
const res = await request
.get('review/list', {
searchParams: {
id: process.env.GOODREADS_USER_ID,
shelf,
},
})
.text();
const reviewList: Goodreads.ReviewList = xml.parse(res);
const reviews = reviewList.GoodreadsResponse.reviews.review;
return reviews;
}
export async function getReviewShow(id: number): Promise<Goodreads.Review> {
const res = await request.get('review/show', { searchParams: { id } }).text();
const reviewShow: Goodreads.ReviewShow = xml.parse(res);
const review = reviewShow.GoodreadsResponse.review;
return review;
}
export async function updateGist(
title: string,
content: string
): Promise<string> {
const gist_id = process.env.GIST_ID || '';
const gist = await octokit.gists.get({ gist_id });
const filename = Object.keys(gist.data.files)[0];
await octokit.gists.update({
gist_id,
files: {
[filename]: {
filename: title,
content,
},
},
});
return gist.url;
}