-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
95 lines (83 loc) · 2.19 KB
/
app.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
var fs = require('fs'),
request = require('request'),
jsdom = require('jsdom'),
optimist = require('optimist'),
colors = require('colors'),
config = fs.existsSync('./config.json') ? require('./config') : {};
var argv = optimist
.usage('Usage $0 [-u username] [-p password] [file]')
.alias({
u: 'username',
p: 'password'
})
.describe({
u: 'Account email',
p: 'Account password'
})
.argv;
var username = argv.username || config.username,
password = argv.password || config.password,
output = argv._[0] || 'instapaper-' + (new Date()).toISOString() + '.csv';
if (argv.help) {
optimist.showHelp();
process.exit();
}
var Instapaper = {
URL: 'http://www.instapaper.com/',
export: function() {
this.logIn();
},
logIn: function() {
var options = {
url: this.URL + 'user/login',
form: {
username: username,
password: password
}
};
console.log('Logging in as ' + username + '...');
request.post(options, this._onLogIn.bind(this));
},
exportCSV: function() {
var self = this;
request(this.URL + 'u', function(error, response, body) {
var jquery = fs.readFileSync(__dirname + '/scripts/jquery.js').toString();
// Parse document
console.log('Parsing /u to extract form key...');
jsdom.env({
html: body,
src: [jquery],
done: function(errors, window) {
var $ = window.$;
if (!errors) {
// Serialize form
body = $('form[action="/export/csv"]').serialize();
if (body) {
console.log('CSV Export Form unique key is ' + body);
// Request csv backup and save it on disk
console.log(('Requesting and saving CSV backup as ' + output + '...').green);
request({
url: self.URL + 'export/csv',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: body
}).pipe(fs.createWriteStream(output));
} else {
console.log('User is not authenticated or perhaps export key was not found...'.red)
}
} else {
console.dir(errors);
}
}
});
});
},
_onLogIn: function(error, response, body) {
if (!error && response.statusCode == 200) {
this.exportCSV();
}
}
};
Instapaper.export();