-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (54 loc) · 1.51 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
const wabetainfo = require("./wabetainfo");
const fs = require('fs');
class WAB {
constructor(last_url = '', file_name = 'id_save_file') {
this.last_url = last_url
this.file_name = file_name + '.txt'
this.running_loop = false
}
async articlelist(filters) {
return await wabetainfo.articlelist(filters);
}
async latest() {
return await wabetainfo.latest();
}
async latest_url() {
const article = await wabetainfo.latest();
return article.url;
}
async searchurl(url) {
return await wabetainfo.searchurl(url)
}
save_url(article_url) {
this.last_url = article_url
fs.writeFileSync(this.file_name, article_url)
}
read() {
try {
return fs.readFileSync(this.file_name, 'utf-8')
} catch { }
return ''
}
get_url() {
this.last_url = this.last_url || this.read()
return this.last_url
}
article_loop(callback, ms = 60 * 1000) {
this.clear_loop()
this.running_loop = setInterval(async () => {
var article = await this.latest_url()
var old = this.get_url()
if (article.url != old) {
this.save_url('' + article.url)
var full_article = await this.searchurl(article.url)
await callback(full_article)
}
}, ms)
}
clear_loop() {
try {
clearInterval(this.running_loop)
} catch { }
}
}
module.exports = WAB