-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffd.js
169 lines (138 loc) · 4.55 KB
/
ffd.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
var httpAgent = require('http-agent'),
jsdom = require('jsdom'),
request = require('request'),
fs = require('fs');
var id = process.argv[2];
if (id.startsWith('https://')) {
id = id.split('/');
id = id[4]
console.log("Got storycode " + id)
}
var dir = "Stories";
var from = 1;
var to = 0;
if (process.argv.length > 3) {
from = process.argv[3];
}
if (process.argv.length > 4) {
to = process.argv[4];
}
request({ uri:'https://www.fanfiction.net/s/' + id }, function (error, response, body) {
if (error && response.statusCode !== 200) {
console.log('Error contacting fanfiction.net');
process.exit(-1);
}
fs.mkdir(dir, function(err) {
if (!err || err.code === 'EEXIST') {
jsdom.env({
html: body,
scripts: [ 'http://code.jquery.com/jquery-2.0.2.min.js' ],
done: function (er, window) {
var $ = window.jQuery;
var n = $('span select#chap_select option').length;
if (n === 0)
n = 1; // we still got one chapter
console.log('Got ' + n + ' chapters');
if (n > 0) {
// get title
var title = $('b.xcontrast_txt').text()
console.log('Got title: ' + title)
// create file
var out = fs.createWriteStream(dir + '/' + title.replace(/[\\/:*?"<>|]/g, " ") + '.rtf', { flags: 'w' });
writeHeader(out);
writeTitle(out, title);
var chs = [];
/* set to */
if (to !== 0 && to < n) {
n = to;
}
for (var i = from; i <= n; i++) {
chs.push('s/' + id + '/' + i);
}
var agent = httpAgent.create('www.fanfiction.net', chs);
var curChapter = from;
agent.addListener('next', function (err, agent) {
if (err)
console.log("ERROR: " + err);
else
jsdom.env({
html: agent.body,
scripts: [ 'http://code.jquery.com/jquery-2.0.2.min.js' ],
done: function (err, window) {
console.log('Writing chapter ' + curChapter);
var $ = window.jQuery;
var ok = out.write('\\ql\\b Chapter ' + curChapter + '\\b0\\\n');
if (!ok)
console.log('ERROR: writing chapter number');
html2Rtf(window.jQuery, $('#storytext'), 0)
out.write(unicodeEscape($('#storytext').html()), function() {
curChapter++;
agent.next();
});
}
});
});
agent.addListener('stop', function (agent) {
writeFooter(out, function() {
console.log('That\'s it, folks. All done!');
process.exit(0);
});
});
agent.start();
}
}
});
}});
});
function writeHeader(stream) {
stream.write('{\\rtf1\\ansi\\ansicpg1252\\deffont0\\deflang1033');
stream.write('{\\fonttbl {\\f0\\fnil\\fcharset1\\fprq0 Calibri;}}');
stream.write('{\\colortbl;\\red0\\green0\\blue0;\\red0\\green0\\blue255;');
stream.write('\\red0\\green255\\blue255;\\red0\\green255\\blue0;');
stream.write('\\red255\\green0\\blue255;\\red255\\green0\\blue0;\\red255\\green255\\blue0;');
stream.write('\\red255\\green255\\blue255;\\red0\\green0\\blue128;\\red0\\green128\\blue128;');
stream.write('\\red0\\green128\\blue0;\\red128\\green0\\blue128;\\red128\\green0\\blue0;');
stream.write('\\red128\\green128\\blue0;\\red128\\green128\\blue128;\\red192\\green192\\blue192;}\n');
stream.write('\\paperw11900\\paperh16840\\margl1440\\margr1440\\vieww10800\\viewh8400\\viewkind0\n');
stream.write('\\deftab720\n');
stream.write('\\pard\\tx566\\tx1133\\tx1700\\tx2267\\tx2834\\tx3401\\tx3968\\tx4535\\tx5102\\tx5669\\tx6236\\tx6803\\pardeftab720\\sb100\\sa100\\pardirnatural\n');
}
function writeTitle(stream, title) {
stream.write('\\ql\\b ' + title + '\\b0\\\n');
}
function writeFooter(stream, onDone) {
stream.write('}', onDone);
}
function html2Rtf($, el, level) {
el.children().each(function() {
if (level < 100)
html2Rtf($, $(this), level + 1);
else
console.log('ERROR: Too many levels (' + $(this) + ')');
});
var tag = el.prop('tagName').toLowerCase();
var txt = el.html();
if (tag === 'p')
el.replaceWith(txt + '\\\n');
else if (tag === 'hr')
el.replaceWith('#-#-#\\\n');
else if (tag === 'br')
el.replaceWith('\\line');
else if (tag === 'strong')
el.replaceWith('\\b ' + txt + '\\b0 ');
else if (tag === 'em')
el.replaceWith('\\i ' + txt + '\\i0 ');
else if (tag === 'span' && el.attr('style').indexOf("underline;") != -1)
el.replaceWith('\\ul ' + txt + '\\ul0 ');
else if (tag === 'div' && level === 0)
; // do nothing, body
else {
console.log('WARNING: Unknown tag ' + tag);
el.replaceWith(txt);
}
}
function unicodeEscape(str) {
return str.replace(/[\s\S]/g, function(character) {
return character.charCodeAt() < 0x80 ? character : '\\u' + character.charCodeAt().toString(10) + '?';
});
}