forked from tcr/skim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapi.js
208 lines (180 loc) · 5.72 KB
/
scrapi.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var async = require('async');
var rem = require('rem');
var cssax = require('cssax');
var toughCookie = require('tough-cookie'), Cookie = toughCookie.Cookie, CookieJar = toughCookie.CookieJar;
rem.USER_AGENT = 'Mozilla/5.0 (compatible; Scrapi/1.0)'
// Utilities
function stripHTML (html) {
return html.replace(/<.+?>/g, '');
}
function combineQueries (a, b) {
return (a.replace(/(?=,)|$/g, ' ' + b.replace(/^\(.*?\)/, ''))).trim();
}
// Scrapi
function onValue (stream, query, str, callback) {
stream.query(query).on('match', function (tag, attributes) {
if (match = str.match(/^\(attr( [^)]+?)?( [^)]+?)?\)/)) {
var value = attributes[match[1].substr(1)] || '';
callback((match[2] ? (value.match(new RegExp(match[2].substr(1))) || [])[0] : value) || '');
} else if (match = str.match(/^\(text( [^)]+?)?\)/)) {
this.readText(function (match, text) {
callback((match[1] ? (text.match(new RegExp(match[1].substr(1))) || [])[0] : text) || '');
}.bind(null, match))
} else if (match = str.match(/^\(html( [^)]+?)?\)/)) {
this.readHTML(function (match, text) {
text = text.replace(/^<[^>]+>|<[^>]+>$/g, '')
callback((match[1] ? (text.match(new RegExp(match[1].substr(1))) || [])[0] : text) || '');
}.bind(null, match))
}
});
}
function parseValueSpec (str) {
return {
$value: (str.match(/^[^)]+\)/) || [])[0],
$query: (str.match(/\)\s*(.*)$/) || [])[1]
};
}
// Setup listeners based on a JSON specification or subspec.
function onSpecification (stream, spec, prefix) {
prefix = prefix || '';
spec = (typeof spec == 'string') ? parseValueSpec(spec) : spec;
// Augment $query parameter.
var query = prefix + (spec.$query ? ' ' + spec.$query : '');
if ('$each' in spec) {
// Array to populate.
var ret = [], first = true;
var parser = onSpecification(stream, spec.$each, query);
stream.query(query).on('match', function (tag, attributes) {
if (first) {
first = false;
} else {
ret.push(parser.result());
}
parser.reset();
});
return {
result: function () {
var vals = ret.concat([parser.result()]);
return vals.filter(function (obj) {
return '$filter' in spec ? Object.prototype.hasOwnProperty.call(obj, spec.$filter) && obj[spec.$filter] : obj;
});
},
reset: function () {
ret = [];
}
};
} else if ('$value' in spec) {
// String to populate.
var ret = null;
onValue(stream, combineQueries(query, spec.$value), spec.$value, function (value) {
ret = value;
});
return {
result: function () {
return ret;
},
reset: function () {
ret = null;
}
};
}
// Object of named fields to populate.
var parsers = {};
Object.keys(spec).filter(function (key) {
return key.charAt(0) != '$';
}).forEach(function (key) {
parsers[key] = onSpecification(stream, spec[key], query);
});
return {
result: function () {
var values = ('$query' in spec) ? null : {};
Object.keys(parsers).forEach(function (key) {
var res = parsers[key].result();
if (res !== null) {
values = values || {};
values[key] = res;
}
})
return values;
},
reset: function () {
Object.keys(parsers).forEach(function (key) {
parsers[key].reset();
})
}
};
}
// Create a Scrapi object that can stream and parse pages.
function scrapi (manifest) {
var api = rem.create({
base: manifest.base,
uploadFormat: 'form'
}, {
key: 'SCRAPI'
});
var jar = new CookieJar();
api.pre('request', function (req, next) {
jar.getCookieString(rem.util.url.format(req.url), function (err, cookies) {
if (cookies) {
req.headers['cookie'] = cookies;
}
req.headers['accept'] = '*/*';
delete req.headers['host'];
req.redirect = false;
next();
})
});
api.pre('response', function (req, res, next) {
// Read cookies from headers.
if (res.headers['set-cookie'] instanceof Array) {
var cookies = res.headers['set-cookie'].map(Cookie.parse);
} else if (res.headers['set-cookie'] != null) {
var cookies = [Cookie.parse(res.headers['set-cookie'])];
} else {
var cookies = [];
}
// Retrieve authentication cookies from request using tough-cookie.
async.forEach(cookies, function (cookie, asyncnext) {
jar.setCookie(cookie, rem.util.url.format(req.url), asyncnext);
}, next);
});
api.parseStream = function (req, res, next) {
var stream = cssax.createStream();
// Find a specification URL that matches.
var spec = null;
Object.keys(manifest.spec).some(function (fullkey) {
return fullkey.split(/\s+/).some(function (key) {
var parts = key.replace(/^\//g, '').split('?');
var path = parts.shift(), query = parts.join('?');
if (req.url.pathname.replace(/^\//, '') == path) {
if (query) {
var query = rem.util.qs.parse(query);
for (var qkey in query) {
if (req.url.query[qkey] != query[qkey]) {
return;
}
}
}
spec = manifest.spec[fullkey];
return true;
}
});
});
spec = spec || manifest.spec['*'] || {};
// Pipre response into parser.
res.pipe(scrapi.parser(spec, next));
};
return api;
}
scrapi.parser = function (spec, next) {
// Build specification parser, return result after stream ends.
var stream = cssax.createStream();
var parser = onSpecification(stream, spec);
stream
.on('error', function () { }) // Toss errors
.on('end', function () {
next(parser.result());
})
return stream;
};
module.exports = scrapi;