-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
59 lines (56 loc) · 1.54 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
const esana = require('./esana')
const fs = require('fs')
class Esana {
constructor(last_id = '',file_name='id_save_file') {
this.last_id = last_id
this.file_name = file_name + '.txt'
this.running_loop = false
}
async news(news_id) {
return await esana.esana_news(news_id)
}
async comments(news_id) {
return await esana.esana_news_comments(news_id)
}
async list() {
return await esana.esana_news_list()
}
async latest_news() {
return await esana.esana_latest_news()
}
async latest_id() {
return await esana.esana_latest_news_id()
}
save_id(news_id) {
this.last_id = news_id
fs.writeFileSync(this.file_name,news_id)
}
read() {
try {
return fs.readFileSync(this.file_name,'utf-8')
} catch {}
return ''
}
get_id() {
this.last_id = this.last_id || this.read()
return this.last_id
}
news_loop(callback,ms = 60*1000) {
this.clear_loop()
this.running_loop = setInterval(async () => {
var news = await this.latest_id()
var old = this.get_id()
if(news.results.news_id != old) {
this.save_id('' + news.results.news_id)
var full_news = await this.news(news.results.news_id)
await callback(full_news)
}
},ms)
}
clear_loop() {
try{
clearInterval(this.running_loop)
} catch {}
}
}
module.exports = Esana