-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.js
76 lines (66 loc) · 1.92 KB
/
counter.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
const configs = require('./configs.json')
const { google } = require('googleapis')
const { OAuthClient, auth, refresh } = require('./auth')
const youtube = google.youtube('v3')
const interval = {
code: null,
refresh() {
this.clear()
this.set()
},
set() {
this.code = setInterval(sync, configs.PACE)
},
clear() {
clearInterval(this.code)
},
}
async function sync() {
console.log('syncing...')
try {
await new Promise(function (resolve, reject) {
youtube.videos
.list({
auth: OAuthClient,
part: ['statistics', 'snippet'],
id: configs.VIDEO_ID,
})
.then(function (res) {
const video = res.data?.items?.pop()
const newCount = video?.statistics?.viewCount
const title = video?.snippet.title
try {
const oldCount = title.match(/Bu video (.*) kez izlendi/)[1]
if (oldCount === newCount) {
console.log('skipping...')
return resolve()
}
} catch (error) {}
youtube.videos
.update({
auth: OAuthClient,
part: 'snippet',
requestBody: {
id: configs.VIDEO_ID,
snippet: {
categoryId: video.snippet.categoryId,
title: `Bu video ${newCount} kez izlendi`,
description: video.snippet.description,
},
},
})
.then(resolve)
.catch(reject)
})
.catch(reject)
})
} catch (error) {
await refresh()
interval.refresh()
}
}
async function init() {
await auth()
interval.set()
}
init()