-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
178 lines (159 loc) · 4.39 KB
/
cli.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#! /usr/bin/env node
'use strict';
var fs = require('fs');
var path = require('path');
var args = process.argv;
var cmd = args[2];
var cfgFile = '.itpub.json';
var cfgPath = path.join(process.env.HOME, cfgFile);
function showUsage() {
var usage = [
'Usage: itpub <cmd> [url|keywords]',
' -h Show usage',
' --help Show usage',
' -v Show version',
' -version Show version',
' config username "your name" password "your password" set config',
' showconfig Show config in ~/.itpub.json',
' ls <url> <only> List thread',
' list <url> <only> Show forum threads',
' listfrom <filepath> <only> Show forum threads from file',
' search <keywords> Search forum threads',
' threads Total threads',
' typeid <typeid> Show pages of threads filtered by typeid',
''
];
return console.log(usage.join('\n'));
}
function showVersion() {
return console.log(require('./package').version);
}
function loadConfig(cfgPath) {
try {
return require(cfgPath);
} catch(e) {
return {};
}
}
function setConfig() {
var kvs = args.slice(3);
var cfg = loadConfig(cfgPath);
for (var i = 0; i < kvs.length; i+=2) {
cfg[kvs[i]] = kvs[i+1];
}
fs.writeFileSync(cfgPath, JSON.stringify(cfg));
return cfg;
}
var ITPubClient, config, client;
try {
ITPubClient = require('itpub-crawler');
config = loadConfig(cfgPath);
} catch(e) {
ITPubClient = require('./lib/itpub-crawler');
config = require('./itpub.json');
}
try {
client = new ITPubClient(config);
} catch(e) {
if (cmd !== 'config') {
console.error('Invalid username or password.\nitpub config username "your name" password "your password"');
process.exit(-1);
}
}
function showResult(result, onlyAttachments) {
if (!result.links || result.links.length === 0) {
return;
}
console.log(' <li data-href="%s" data-text="%s">', onlyAttachments ? result.link.href : '', result.link.text);
console.log(' <ul>');
result.links.forEach(function(link) {
console.log(' <li><a href="%s">%s</a></li>', link.href, link.text);
});
console.log(' </ul>');
console.log(' </li>');
}
function showResults(results, onlyAttachments) {
if (!results || results.length === 0) {
return;
}
console.log('<ul>');
results.forEach(function(result) {
showResult(result, onlyAttachments);
});
console.log('</ul>');
}
var handlers = {
'-h': showUsage,
'--help': showUsage,
'-v': showVersion,
'--version': showVersion,
'config': setConfig,
'showconfig': function() {
var cfg = loadConfig(cfgPath);
console.log('Config: %s', JSON.stringify(cfg));
},
'ls': function() {
var url = args[3];
var onlyAttachments = args[4] || true;
console.log('Listing attachments ...');
client.listThread(url, function(err, results) {
if (err) {
throw err;
}
showResults(results, onlyAttachments);
});
},
'list': function() {
var url = args[3];
var onlyAttachments = args[4] || true;
console.log('Listing forum ...');
client.listForumThreads(url, function(err, results) {
if (err) {
throw err;
}
showResults(results, onlyAttachments);
});
},
'listfrom': function() {
var filepath = args[3];
var onlyAttachments = args[4] || true;
console.log('Listing from file ...');
client.listForumThreadsFromFiles(filepath, function(err, results) {
if (err) {
throw err;
}
showResults(results, onlyAttachments);
});
},
'search': function() {
var keywords = args[3];
console.log('Searching ...');
client.searchThreads(keywords, function(err, data) {
if (err) {
throw err;
}
console.log(data);
});
},
'threads': function() {
console.log('Fetching ...');
client.totalThreads(function(err, data) {
if (err) {
throw err;
}
console.log('Total threads: %s', data);
});
},
'typeid': function() {
console.log('Filtering ...');
console.log('You can also trying other types: ', client.itpubTypes());
var typeid = args[3] || 385;
client.pagesOfType(typeid, function(err, data) {
if (err) {
throw err;
}
console.log('Total pages of type %s: %s', typeid, data);
});
},
};
handlers[cmd || '-h'].call();