-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunfav-schedule.js
79 lines (69 loc) · 1.49 KB
/
unfav-schedule.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
'use strict';
const schedule = require('node-schedule');
const Fanfou = require('fanfou-sdk');
const async = require('async');
const {
CONSUMER_KEY: consumerKey,
CONSUMER_SECRET: consumerSecret,
FANFOU_USERNAME: username,
FANFOU_PASSWORD: password,
HTTPS: https,
REQUEST_LIMIT: requestLimit
} = require('./config');
const ff = new Fanfou({
consumerKey,
consumerSecret,
username,
password,
protocol: https ? 'https:' : 'http:',
fakeHttps: Boolean(https)
});
const unfav = ids => {
async.eachLimit(ids, requestLimit, (id, cb) => {
ff.post('/favorites/destroy/' + id)
.then(() => {
cb();
})
.catch(error => {
console.log(error.message);
cb();
});
}, error => {
if (error) {
console.log(error);
} else {
console.log('Done');
}
});
};
(async () => {
})();
const run = async () => {
await ff.xauth();
const user = await ff.get('/account/verify_credentials');
const {favourites_count: favCount} = user;
const pageCount = Math.ceil(favCount / 60);
const pages = Array.from({length: pageCount}, (v, i) => i + 1);
let fullIds = [];
async.eachLimit(pages, requestLimit, (page, cb) => {
ff.get('/favorites', {page, count: 60})
.then(list => {
const ids = list.map(status => status.id);
fullIds = fullIds.concat(ids);
cb();
})
.catch(error => {
console.log(error.message);
cb();
});
}, error => {
if (error) {
console.log(error);
} else {
unfav(fullIds);
}
});
};
schedule.scheduleJob('0 0 * * *', () => {
run();
});