-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.js
152 lines (118 loc) · 3 KB
/
delete.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
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
"use strict";
const request = require("request").defaults({jar: true});
const dateformat = require("dateformat");
const cheerio = require("cheerio");
const async = require("async");
const sleep = require("sleep-async")();
const prompt = require("prompt");
function calcDays(dFrom, dUntil) {
const day = 86400000;
var days = [], i;
for (i = new Date(dFrom.getTime()); i <= dUntil; i = new Date(i.getTime() + day)) {
days.push(i);
}
return days;
}
module.exports = function(config, cb, /*optional*/ log) {
log = log || console.log.bind(console);
function signin(cb) {
log("Loading start page");
request({
method: "POST",
uri: `${config.site}/sign_in`
},
(err, data) => {
if(err) {
cb(err);
return;
}
const $ = cheerio.load(data.body);
const token = $("input[name='xsrf_token']").val();
if(!token) {
cb(new Error("sign_in received no xsrf_token"));
return;
}
log("Signing in");
request({
method: "POST",
uri: `${config.site}/sign_in`,
formData: {
xsrf_token: token,
email: config.email,
password: config.password
}
},
cb);
});
}
function wipeDay(day, cb) {
const dateStr = dateformat(day, "yyyy/mm/dd");
const uri = `${config.site}/history/member/${config.memberIdOther}/${dateStr}`;
log(`Fetching conversation for ${dateStr}`);
request({
method: "GET",
uri: uri
},
(err, data) => {
if(err) {
cb(err);
return;
}
const $ = cheerio.load(data.body);
const dateStr = dateformat(day, "yyyy/mm/dd");
const $messages = $(`form[action='${uri}']`);
const mcount = $messages.length;
log(` ${mcount} messages found`);
if($messages.length === 0) {
const delay = Math.floor(Math.random() * 2000 + 1000);
console.log(` Waiting ${delay} ms`);
sleep.sleep(delay, cb);
return;
}
const $m = $messages.first();
const token = $m.find("input[name='xsrf_token']").val();
const mid = $m.find("input[name='message_id']").val();
const mix = $m.find("input[name='message_index']").val();
if(!token) {
cb(new Error("delete form has no xsrf_token"));
return;
}
if(!mid) {
cb(new Error("delete form has no message_id"));
return;
}
if(!mix) {
cb(new Error("delete form has no message_index"));
return;
}
log(` Deleting message: ${dateStr} ${mid}`);
const p = {
method: "POST",
uri: uri,
formData: {
action: "delete",
message_id: mid,
message_index: mix,
xsrf_token: token
}
};
request(
p,
(err, data) => {
if(err) {
cb(err);
}
else {
// recursive deleting
wipeDay(day, cb);
}
});
});
}
const steps = [ signin ];
const dateFrom = config.dateFrom instanceof Date ? config.dateFrom : new Date(config.dateFrom);
const dateUntil = config.dateUntil instanceof Date ? config.dateUntil : new Date(config.dateUntil);
const days = calcDays(dateFrom, dateUntil);
days.forEach(day => steps.push(wipeDay.bind(null, day)));
async.series(steps, cb);
};