-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
138 lines (118 loc) · 4.12 KB
/
index.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import 'dotenv/config'
import express from 'express'
import { Octokit } from "@octokit/core"
import wordwrap from "wordwrap"
import request from 'superagent';
const {
GIST_ID: gistId,
GH_TOKEN: githubToken,
GOOGLE_BOOKS_UID: googleBooksUID,
GOOGLE_BOOKS_KEY: googleBooksKey,
} = process.env;
async function main() {
const wrap = wordwrap(58);
try{
function delay() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(42);
}, 1500);
});
}
async function justRead() {
try {
var recent = await request.get(`https://www.googleapis.com/books/v1/users/${googleBooksUID}/bookshelves/4/volumes?&key=${googleBooksKey}`);
await delay();
return recent.body;
} catch(error) {
return null;
}
}
async function currentlyRead() {
try {
var current = await request.get(`https://www.googleapis.com/books/v1/users/${googleBooksUID}/bookshelves/3/volumes?&key=${googleBooksKey}`);
await delay();
return current.body;
} catch(error) {
return null;
}
}
(async function(){
let justFinished = await justRead();
let justFinishedTitle = ""
let justFinishedauthor = ""
if(justFinished?.totalItems < 1){
justFinishedTitle = "Nothing Read Yet"
justFinishedauthor = "NIL"
}else{
justFinishedTitle = justFinished?.items[0].volumeInfo.title;
justFinishedauthor = justFinished?.items[0].volumeInfo.authors;
}
const justReadTitle = justFinishedTitle;
const justReadAuthor = justFinishedauthor?.toString();
let currentReading = await currentlyRead();
let currentReadingTitle = ""
let currentReadingAuthor = ""
if(currentReading?.totalItems < 1){
justFinishedTitle = "Nothing For Now"
justFinishedauthor = "NIL"
}else{
currentReadingTitle = currentReading?.items[0].volumeInfo.title;
currentReadingAuthor = currentReading?.items[0].volumeInfo.authors;
}
const currentTitle = currentReadingTitle;
const currentAuthor = currentReadingAuthor?.toString();
const currentlyReading = justReadTitle && justReadAuthor
? `Recently Finished: ${justReadTitle.split(':'[0])} \n By ${justReadAuthor}\n`
: `I haven't read recently.\n`
console.log(justReadTitle, justReadAuthor)
const recentlyRead = currentTitle && currentAuthor
? `Currently Reading: ${currentTitle.split(':')[0]} \nBy ${currentAuthor}`
: `I am not reading anything at the moment.\n--------------------------------------`
console.log(currentTitle, currentAuthor)
await updateGist([wrap(recentlyRead), wrap(currentlyReading)]);
})();
}catch(error){
console.error(`Unable to fetch justFinished\n${error}`)
}
}
async function updateGist(readingStatus) {
let gist;
try {
const octokit = new Octokit({
auth: githubToken
})
gist = await octokit.request('GET /gists/{gist_id}', {
gist_id: gistId
}
)
} catch (error) {
console.error(`Unable to get gist 1\n${error}`);
}
// Get original filename to update that same file
const filename = Object.keys(gist.data.files)[0];
// Only update if the content has changed
if (gist.data.files[filename].content === readingStatus.join('\n')) {
console.log(`Reading status hasn't changed; skipping update.`);
return;
}
try {
const octokit = new Octokit({
auth: githubToken
})
octokit.request('PATCH /gists/{gist_id}',{
gist_id: gistId,
files: {
[filename]: {
filename,
content: readingStatus.join('\n'),
}
}
});
} catch (error) {
console.error(`Unable to update gist\n${error}`);
}
}
(async () => {
await main();
})();