-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.js
216 lines (182 loc) · 5.59 KB
/
scrape.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
209
210
211
212
213
214
215
216
// ----------------------------------------------------------------
// Includes and parameters
// ----------------------------------------------------------------
const MAX_ARTICLE_LENGTH = 5000;
const MAX_SECTION_LENGTH = 1000;
const DB_CLEAR = false;
const request = require('request');
const mysql = require('mysql');
const stopWords = require('./stopwords');
let startYear = 1852;
let endYear = 1870;
let apiKey = '8f4b1028d941458d9041934219d8b96f';
// ----------------------------------------------------------------
// Make database connection
// ----------------------------------------------------------------
let db = mysql.createConnection({
host : 'localhost',
port : 8889,
user : 'newspaper',
password : 'new-york',
database : 'nyt'
});
db.connect();
if (DB_CLEAR) db.query("truncate table articles");
// ----------------------------------------------------------------
// Main
// ----------------------------------------------------------------
let startTime = Date.now();
articleCount = 0;
categories = ["Headline", "Keywords", "Snippet", "Lead Paragraph", "Abstract"];
categoryCount = [ 0, 0, 0, 0, 0 ];
(async () => {
for (let year = startYear; year < endYear + 1; year++) {
for (let month = 1; month < 13; month++) {
startTime = Date.now();
console.log();
console.log(`Making request for articles from ${month.toString().padStart(2, '0')}/${year}`);
body = await getFromUrl(getUrl(year, month));
storeResponse(body.response.docs);
}
}
console.log();
console.log("== ========================= =")
console.log("");
console.log(` TOTAL ARTICLE COUNT: ${articleCount}`);
console.log();
for (i in categories) {
console.log(` ${categories[i]}: ${categoryCount[i]}`);
}
console.log();
console.log("== ========================= =")
// close connection
db.end();
})();
// ----------------------------------------------------------------
// Functions
// ----------------------------------------------------------------
function getUrl(year, month) {
return `https://api.nytimes.com/svc/archive/v1/${year.toString()}/${month.toString()}.json?api-key=${apiKey}`;
}
async function getFromUrl(url) {
let success = false;
let count = 0;
let body;
while (!success) {
try {
body = await makeSingleRequest(url);
body = JSON.parse(body);
success = true;
} catch(err) {
console.log();
console.log(err);
console.log();
console.log(`Failed ${++count} trie(s) at retrieval of ${url}`);
console.log('Trying again...');
console.log();
}
}
return body;
}
async function makeSingleRequest(url) {
return new Promise( (resolve, reject) => {
request.get(url, (err, res, body) => {
if (err) {
reject(err);
}
resolve(body);
})
})
}
function storeResponse(articles) {
try {
if (!articles || articles.length <= 0) return;
gotResponseTime = Date.now();
console.log("Retrieved: " + articles.length.toString() + " headlines in " + (gotResponseTime - startTime).toString() + " ms");
let loopCount = 0;
for (i in articles) {
// get info
pubDate = articles[i].pub_date;
section = getSection(articles[i]);
articleString = processArticle(articles[i]);
// exit if invalid
if ( !articleString || !pubDate ) continue;
// chuck it onto the db
if ( (typeof articleString) != "string" ) {
console.log(pubDate);
console.log(articleString);
console.log(section);
continue;
}
let query = `INSERT INTO articles (pub_date, article_string, section) VALUES ('${pubDate}', '${articleString}', '${section}')`;
db.query(query, (err, res, fields) => {
if (err) {
console.log(`Error with query: ${query} in:`);
console.log(articles[i]);
throw err;
}
});
articleCount += 1;
loopCount += 1;
}
console.log(`Entered ${loopCount.toString()} articles into the database`);
console.log();
console.log('--- -- -- -- -- -- -- ---');
} catch (err){
console.log(err);
//console.log("Couldn't load articles, this was the response received:");
//console.log(body);
}
}
function processArticle(article) {
/**
* some processing:
* - utilise the longest out of: [ keywords, abstract, snippet, lead_paragraph, main_headline ]
* - make all lowercase
* - remove stop words
* - replace all punctuation with spaces
* - shorten all repeated spaces
* - remove leading and trailing whitespace
*/
// init array
let strings = [
article.headline.main,
article.keywords.map((element) => {
return element.value;
}).join(" "),
article.snippet,
article.lead_paragraph,
article.abstract
]
// replace null with empty strings, and find the longest string too
let maxIndex = 0;
let maxLength = 0;
for (i in strings) {
strings[i] = strings[i] ? strings[i] : "";
if (strings[i].length > maxLength) {
maxIndex = i;
maxLength = strings[i].length;
}
}
articleString = strings[maxIndex];
// further text processing
articleString = stopWords.removeFrom(articleString.toLowerCase())
.replace(/[.,/#!$?%\^&\*;:{}=\-_`~()'"]/g, '')
.replace(/\\/g, ' ')
.replace(/\s\s+/g, ' ')
.trim();
// return
if (articleString.length == 0) return null;
if (articleString.split(" ").length < 2) return null;
categoryCount[maxIndex]++;
return articleString.substring(0, 5000);
}
function getSection(article) {
section = article.section_name ? article.section_name : "";
section = stopWords.removeFrom(section.toLowerCase())
.replace(/[.,\/#!$?%\^&\*;:{}=\-_`~()'"]/g, '')
.replace(/\\/g, ' ')
.replace(/\s\s+/g, ' ')
.trim();
return section.length > 0 ? section.substring(0, 1000) : null;
}