forked from manriquezmanny/algo-allies-scraper-prototype
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrapeData.js
More file actions
206 lines (178 loc) · 6.71 KB
/
scrapeData.js
File metadata and controls
206 lines (178 loc) · 6.71 KB
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
//Authors: Manuel, Mobin
//// IMPORTS ////
// Getting Scraper functions.
require("dotenv").config();
const { modestoBeeScraper } = require("./scrapers/modestoScraper");
const { turlockJournalScraper } = require("./scrapers/turlockScraper");
const { oakdaleLeaderScraper } = require("./scrapers/oakdaleScraper");
const { riverbankNewsScraper } = require("./scrapers/riverbankScraper");
const { tracyPressScraper } = require("./scrapers/tracyScraper");
const { riponScraper } = require("./scrapers/riponScraper");
const { getDataBaseURLS } = require("./getDataBaseURLS");
const dbURL = process.env.API_URL;
const createArticleURL = process.env.Create_URL;
//// FUNCTIONS ////
// @ desc Scrapes city data or all cities if all is passed as arg.
// @ returns an array of objects where each object represents an article with the data we need as properties.
async function scrapeData(city = "all") {
console.log("\n");
let articles = [];
console.time();
console.log("Getting Database URLS");
const dbURLS = await getDataBaseURLS();
console.log("Got database URLS.\n");
switch (city) {
case "turlock":
articles = await turlockJournalScraper(dbURLS);
console.log(
`Scraped ${articles.length} articles from The Turlock Journal`
);
await fetch(createArticleURL, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(articles),
}).catch((e) => console.log(`Error sending new articles to db: ${e}`));
console.log(`Sent ${articles.length} articles to Database. \n`);
break;
case "modesto":
articles = await modestoBeeScraper(dbURLS);
console.log(`Scraped ${articles.length} articles from The Modesto Bee`);
await fetch(createArticleURL, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(articles),
}).catch((e) => console.log(`Error sending new articles to db: ${e}`));
console.log(`Sent ${articles.length} articles to Database. \n`);
break;
case "oakdale":
articles = await oakdaleLeaderScraper(dbURLS);
console.log(
`Scraped ${articles.length} articles from The Oakdale Leader`
);
await fetch(createArticleURL, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(articles),
}).catch((e) => console.log(`Error sending new articles to db: ${e}`));
console.log(`Sent ${articles.length} articles to Database. \n`);
break;
case "riverbank":
articles = await riverbankNewsScraper(dbURLS);
console.log(`Scraped ${articles.length} articles from Riverbank News`);
await fetch(createArticleURL, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(articles),
}).catch((e) => console.log(`Error sending new articles to db: ${e}`));
console.log(`Sent ${articles.length} articles to Database. \n`);
break;
case "tracy":
articles = await tracyPressScraper(dbURLS);
console.log(`Scraped ${articles.length} articles from Tracy Press`);
await fetch(createArticleURL, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(articles),
}).catch((e) => console.log(`Error sending new articles to db: ${e}`));
console.log(`Sent ${articles.length} articles to Database. \n`);
break;
case "ripon":
articles = await riponScraper(dbURLS);
console.log(`Scraped ${articles.length} articles from Ripon Press`);
await fetch(createArticleURL, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(articles),
}).catch((e) => console.log(`Error sending new articles to db: ${e}`));
console.log(`Sent ${articles.length} articles to Database. \n`);
break;
case "all":
try {
modestoArr = await modestoBeeScraper(dbURLS);
articles = [...articles, ...modestoArr];
console.log(
`Scraped ${modestoArr.length} articles from The Modesto Bee\n`
);
} catch (e) {
console.log(`Failed to scrape Modesto. Error: ${e.message}\n`);
}
try {
tracyArr = await tracyPressScraper(dbURLS);
articles = [...articles, ...tracyArr];
console.log(
`Scraped ${tracyArr.length} articles from The Tracy Press\n`
);
} catch (e) {
console.log(`Failed to scrape Tracy. Error ${e.message}\n`);
}
try {
turlockArr = await turlockJournalScraper(dbURLS);
articles = [...articles, ...turlockArr];
console.log(
`Scraped ${turlockArr.length} articles from The Turlock Journal\n`
);
} catch (e) {
console.log(`Failed to scrape Turlock. Error ${e.message}\n`);
}
try {
oakdaleArr = await oakdaleLeaderScraper(dbURLS);
articles = [...articles, ...oakdaleArr];
console.log(`Scraped ${oakdaleArr.length} from The Oakdale Leader\n`);
} catch (e) {
console.log(`Failed to scrape Oakdale. Error ${e.message}\n`);
}
try {
riverbankArr = await riverbankNewsScraper(dbURLS);
articles = [...articles, ...riverbankArr];
console.log(
`Scraped ${riverbankArr.length} articles from The Riverbank News\n`
);
} catch (e) {
console.log(`Failed to scrape Riverbank. Error ${e.message}\n`);
}
try {
riponArr = await riponScraper(dbURLS);
articles = [...articles, ...riponArr];
console.log(
`Scraped ${riponArr.length} articles from The Ripon Press\n`
);
} catch (e) {
console.log(`Failed to scrape Ripon. Error ${e.message}\n`);
}
console.log(`Scraped a Total of ${articles.length} Articles. \n`);
await fetch(createArticleURL, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(articles),
}).catch((e) => console.log(`Error sending new articles to db: ${e}`));
console.log(`Sent ${articles.length} articles to Database. \n`);
if (articles.length === 0) {
console.log("No new articles to send.");
}
break;
}
console.timeEnd();
}
// Updates Scraped Data object and will write to JSON file.
scrapeData();