This repository has been archived on 2021-06-30. You can view files and clone it, but cannot push or open issues or pull requests.
trendogate_scraper/index.ts

141 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-03-05 15:30:20 +00:00
"use strict";
2020-03-05 18:44:18 +00:00
2020-03-05 19:11:51 +00:00
import { addDays, format } from 'date-fns';
2020-03-05 18:44:18 +00:00
import cheerio from 'cheerio';
import axios from 'axios';
import fs from 'fs';
const BASE_URL = "https://trendogate.com/";
const CSV_PATH = "./data.csv";
2020-03-05 15:30:20 +00:00
const USA_ID = 23424977;
2020-03-06 19:09:56 +00:00
const NUMBER_OF_DAYS = 200;
const WAIT = 500; // in ms
2020-03-05 15:30:20 +00:00
2020-03-05 18:44:18 +00:00
// e.g https://trendogate.com/placebydate/23424977/2015-04-01
// Topic | Position | Date
2020-03-05 15:30:20 +00:00
2020-03-06 19:09:56 +00:00
const now: Date = new Date();
2020-03-05 15:30:20 +00:00
let tmp : Date = now;
let days: Array<Date> = [];
2020-03-05 18:44:18 +00:00
for (let i = 0; i < NUMBER_OF_DAYS; i++) {
tmp = addDays(tmp, -1);
2020-03-05 15:30:20 +00:00
days.push(tmp);
}
2020-03-05 18:44:18 +00:00
async function scrapeData() {
const csv = new CSVManager(CSV_PATH);
csv.createWriteStream();
for (let i = 0; i < days.length; i++) {
const url = getNewDateUrl(days[i]);
axios.get(url).then((res: any) => {
2020-03-05 15:30:20 +00:00
2020-03-05 18:44:18 +00:00
if (res.status === 200) {
let trends: Array<Trend> = handleHttpResponse(res.data, days[i]);
process.stdout.write(`URL: ${url}\n`);
writeToCsv(trends, csv);
2020-03-05 15:30:20 +00:00
2020-03-05 18:44:18 +00:00
}
else process.stderr.write(`Server returned with a Status of: ${res.status}\n`);
});
await sleep(WAIT);
}
}
function handleHttpResponse(html: string, day: Date): Array<Trend> {
let trends: Array<Trend> = [];
const $ = cheerio.load(html);
2020-03-05 15:30:20 +00:00
2020-03-05 18:44:18 +00:00
$('div.panel > ul.list-group').children().each((i, child) => {
let term = child.firstChild.firstChild.data;
trends.push(new Trend(i + 1, term, day));
2020-03-06 19:09:56 +00:00
2020-03-05 18:44:18 +00:00
});
return trends;
}
// SO: https://stackoverflow.com/questions/14249506/how-can-i-wait-in-node-js-javascript-l-need-to-pause-for-a-period-of-time
function sleep(ms: number) {
return new Promise(res => {
setTimeout(res, ms);
});
}
function getNewDateUrl(date: Date): string {
2020-03-05 19:11:51 +00:00
return `${BASE_URL}/placebydate/${USA_ID}/${format(date, "yyyy-MM-dd")}`;
2020-03-05 18:44:18 +00:00
}
function writeToCsv(trends: Array<Trend>, csv: CSVManager) {
for (let i = 0; i < trends.length; i++) {
2020-03-06 19:09:56 +00:00
if (i == 20) break; // Should Only Write 20 per day hopefully.
let trend = trends[i];
// Test if The String is Latin+ Some Other Codepoint ranges.
if (trend.getName().match(/^([A-Za-z\u00C0-\u00D6\u00D8-\u00f6\u00f8-\u00ff\s\#]*)$/i)) {
csv.write(trends[i].toCsv());
}
2020-03-05 18:44:18 +00:00
}
}
class CSVManager {
private stream: fs.WriteStream = null;
private path: string;
constructor(path: string) {
this.path = path;
}
public write(row: string) {
if (this.stream !== null) {
this.stream.write(row, err => { if (err) throw err; });
} else process.stderr.write(`Unable to write to "${this.path}". Stream does not exist.`);
}
public createWriteStream() {
2020-03-05 19:11:51 +00:00
if (!fs.existsSync(this.path)) fs.closeSync(fs.openSync(this.path, "w"));
2020-03-05 18:44:18 +00:00
this.stream = fs.createWriteStream(this.path, { flags: "a" });
}
public closeWriteStream() {
this.stream.end();
this.stream = null;
}
public getPath() {
return this.path;
}
}
class Trend {
private name: string;
private date: Date;
private ranking: number;
constructor(ranking: number, name: string, date: Date) {
this.name = name;
this.date = date;
this.ranking = ranking;
}
public getName() {
return this.name;
}
public getDate() {
return this.date;
}
public getRanking() {
return this.ranking;
}
public toCsv() {
2020-03-05 19:11:51 +00:00
return `${this.name}, ${this.ranking}, ${format(this.date, "yyyy-MM-dd")}\n`;
2020-03-05 18:44:18 +00:00
}
}
scrapeData();