-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscrape-data.js
167 lines (143 loc) · 3.21 KB
/
scrape-data.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
var fs = require('fs');
var through2 = require('through2');
var slugger = require('slugger');
var paths = require('./paths');
var transformRecords = require('./transform');
var x = require('x-ray')();
// [month, year] or [year]
var chunk1 = [
[9, 1996],
[10, 1996],
[11, 1996],
[12, 1996],
[1997],
[1998],
[1999],
[2000]
];
var chunk2 = [
[2001],
[2002],
[2003],
[2004],
[2005]
];
var chunk3 = [
[2006],
[2007],
[2008],
[2009],
[2010]
];
var chunk4 = [
[2011],
[2012],
[2013],
[2014],
[2015],
[2016]
];
var chunk5 = [
[2017],
[1, 2018]
];
var dates = [
[1, 2018]
];
ingestData(expandDates(dates));
function ingestData(dates) {
return dates.map(function([month, year]) {
var outputFile = paths.sourceDataFile(month, year);
var recordsArrayStream = xrayStream(month, year)
.pipe(fromJson())
.pipe(cleanUp())
.pipe(toJson())
.pipe(fs.createWriteStream(outputFile))
.on('error', function(e) {
console.error(outputFile, e)
})
.on('close', function() {
console.log(`wrote ${outputFile}`);
});
});
}
function xrayStream(month, year) {
if (!canGetDataFor(month, year)) {
throw new Error(`Cannot get data for ${month}/${year}`);
}
var url = getUrl(month, year);
console.log(`hitting ${url}`);
return x(url, '#Top300Comics tr', [{
rank: 'td:nth-of-type(1)',
title: 'td:nth-of-type(3) a',
issue: 'td:nth-of-type(4)',
price: 'td:nth-of-type(5)',
publisher: 'td:nth-of-type(7)',
count: 'td:nth-of-type(8) strong'
}]).stream();
}
function toJson() {
return through2.obj(function toJsonTransform(chunk, encoding, callback) {
try {
return callback(null, JSON.stringify(chunk, null, 2));
} catch (e) {
return callback(e);
}
});
}
function fromJson() {
return through2.obj(function fromJsonTransform(chunk, encoding, callback) {
try {
return callback(null, JSON.parse(chunk.toString()));
} catch (e) {
return callback(e);
}
});
}
function cleanUp() {
return through2.obj(function cleanUpTransform(records, encoding, callback) {
try {
var cleaned = transformRecords(records);
return callback(null, cleaned);
} catch (e) {
console.error(e)
return callback(e);
}
});
}
function getUrl(month, year) {
var paddedMonth = zeroPadMonth(month);
return `http://www.comichron.com/monthlycomicssales/${year}/${year}-${paddedMonth}.html`;
}
function canGetDataFor(month, year) {
return year > 1996 || (year == 1996 && month > 8);
}
function zeroPadMonth(month) {
var string = month.toString();
return string.length == 1 ? '0' + string : string;
}
function expandDates(dates) {
var result = dates.map(function(date) {
if (date.length == 1) {
let year = date[0];
return [
[1, year],
[2, year],
[3, year],
[4, year],
[5, year],
[6, year],
[7, year],
[8, year],
[9, year],
[10, year],
[11, year],
[12, year]
];
} else {
// need to wrap in extra array so the unwrap step works at the end
return [date];
}
});
return Array.prototype.concat.apply([], result);
}