-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.ts
206 lines (191 loc) · 7.86 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// google api stuff
import async from "async";
import https from "https";
import querystring from "querystring";
import { IncomingMessage } from "http";
import { AuthHelper } from "./auth";
import * as urlLib from "url";
export class YoutubeAPI3 {
checkFinished: Function
processEntry: Function
setDone: Function
setTotalComments: Function
public paged: boolean = false
public uploader: string = "" // used to not stop rescanning pinned comments since pins are not reported in youtube's api
pageflag: boolean = false
auth: AuthHelper
async snooze(ms: number) { new Promise(resolve => setTimeout(resolve, ms)); }
constructor(checkFinished: Function, processEntry: Function, setDone: Function, setTotalComments: Function) {
this.checkFinished = checkFinished
this.processEntry = processEntry
this.setDone = setDone
this.setTotalComments = setTotalComments
this.auth = new AuthHelper(this)
}
// nice fetch alternative for node
async getContent(url: string, params: any, post: boolean = false): Promise<string> {
// return new pending promise
return new Promise<string>((resolve, reject) => {
// select http or https module, depending on reqested url
let parsedUrl = new urlLib.URL((url + (post ? "" : "?" + querystring.stringify(params))))
const request = https.request({
path: parsedUrl.pathname + parsedUrl.search,
host: parsedUrl.hostname,
port: parsedUrl.port,
method: post ? "POST" : "GET",
headers: post ? {
'Content-Type': 'application/x-www-form-urlencoded'
} : undefined
}, (response: IncomingMessage) => {
// temporary data holder
const body: String[] = [];
// on every content chunk, push it to the data array
response.on("data", (chunk: String) => body.push(chunk));
// we are done, resolve promise with those joined chunks
response.on("end", () => {
// handle http errors
if (
response.statusCode &&
(response.statusCode < 200 || response.statusCode > 299)
) {
reject(
new Error(
"Failed to load page, status code: "
+ url + " "
+ response.statusMessage + " "
+ body.join("")
)
);
} else {
resolve(body.join(""))
}
});
});
if (post) request.write(querystring.stringify(params))
// handle connection errors of the request
request.on("error", err => reject(err));
request.end();
});
}
// fast alternative to gapi request that is real async
async apiFast(endpoint: string, parameters: { [key: string]: any }, n = 0): Promise<any> {
Object.assign(parameters, await this.auth.authenticate());
parameters.prettyPrint = false;
let url: string = `https://www.googleapis.com/youtube/v3/${endpoint}`;
let resp: string = ""
try {
resp = await this.getContent(url, parameters);
} catch (e) {
console.log(`Retrying (try ${n})`, e)
n++
if (n < 10) {
return await this.apiFast(endpoint, parameters, n)
} else {
console.log("Too many retries, giving up");
throw e
}
}
const returnv = JSON.parse(resp);
const final = {
result: returnv,
error: returnv.error
};
return final;
}
// paginate replies, dont run unless necessary since it eats quota
async getReplies(id: string, pageToken?: string) {
this.apiFast("comments", {
part: "snippet",
fields:
"items(id,snippet(textDisplay,authorChannelId,updatedAt,publishedAt)),nextPageToken",
parentId: id,
textFormat: "plainText",
maxResults: 100,
pageToken: pageToken
}).then(resp => {
let obj = resp.result;
if (obj.nextPageToken) {
this.getReplies(id, obj.nextPageToken);
}
if (obj.items) {
async.each(obj.items, (item: any) => {
item.isReply = true
this.noteItem(item);
});
}
this.checkFinished();
}, console.log);
}
// parse api comments into simpler entries and process them
async noteItem(item: any) {
if (item.replies != undefined) {
for (let reply of item.replies.comments) {
reply.isReply = true;
}
if (item.replies.comments.length < item.snippet.totalReplyCount) {
if (!this.pageflag || (item.snippet.topLevelComment && item.snippet.topLevelComment.snippet.authorChannelId.value != this.uploader)) {
// This is to prevent any pinned comments from disrupting the automatic page limiter since they are always scanned (the API has no pinned comment API so we use channel owner as a guess)
this.getReplies(item.id) //uses too much api but hey
}
}
}
const snippet = item.snippet.topLevelComment
? item.snippet.topLevelComment.snippet
: item.snippet;
if (snippet.authorChannelId != undefined && snippet.authorChannelId.value == this.uploader) { item.isReply = true } // anti-pin
const entry = {
likes: snippet.likeCount ? snippet.likeCount : 0,
content: snippet.textDisplay,
id: item.id,
isReply: item.isReply ? true : false,
userId: snippet.authorChannelId ? snippet.authorChannelId.value : 0,
userName: snippet.authorDisplayName
? snippet.authorDisplayName
: "unknown",
date: Date.parse(snippet.updatedAt),
postDate: Date.parse(snippet.publishedAt),
edited: (snippet.updatedAt == snippet.publishedAt ? false : true),
};
this.processEntry(entry);
}
// load comments of a certain modStatus
async loadComments(
id: string,
modStatus: string,
pageToken?: string,
paginate: boolean = true,
pageflag: boolean = false
) {
this.pageflag = pageflag
console.log("Loading comments")
this.apiFast("commentThreads", {
part: "snippet,replies",
fields:
"items(id,snippet(topLevelComment,totalReplyCount),replies),nextPageToken",
videoId: id,
moderationStatus: modStatus,
textFormat: "plainText",
order: "time",
maxResults: 100,
pageToken: pageToken
}).then(resp => {
let obj = resp.result;
if (obj.error) {
throw "Error requesting";
} else {
async.each(obj.items, (item: object, callback: Function) => {
this.noteItem(item);
callback()
}, () => {
if (obj.nextPageToken && paginate && (!pageflag || !this.paged)) {// recheck paged
this.loadComments(id, modStatus, obj.nextPageToken, paginate, pageflag);
} else {
this.setDone(modStatus, true);
console.log("Refresh done for " + modStatus + " comments");
}
})
}
this.checkFinished();
}, console.log);
}
}