From e72b41cb7168ab0b2377e4568c942c0b508de89f Mon Sep 17 00:00:00 2001 From: paoda Date: Tue, 17 Mar 2020 05:08:21 -0300 Subject: [PATCH] Fix error in compiling list of dates to scrape --- index.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/index.ts b/index.ts index ee87a91..fbfd54c 100644 --- a/index.ts +++ b/index.ts @@ -19,16 +19,16 @@ const WAIT = 500; // in ms // e.g https://trendogate.com/placebydate/23424977/2015-04-01 // Topic | Position | Date -const goal: Date = new Date(2015, 2, 1); // 2 is March because Month is 0-indexed (why?) -let tmp : Date = new Date(); +const goal: Date = new Date(); +let tmp: Date = new Date(2015, 2, 1); // 2 is March because Month is 0-indexed (why?) let days: Array = []; -while (tmp >= goal) { +while (goal >= tmp) { days.push(tmp); - tmp = addDays(tmp, -7); + tmp = addDays(tmp, 7); } -async function scrapeData() { +async function scrapeData(): Promise { const csv = new CSVManager(CSV_PATH); csv.createWriteStream(); @@ -63,7 +63,7 @@ function handleHttpResponse(html: string, day: Date): Array { } // 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) { +function sleep(ms: number): Promise { return new Promise(res => { setTimeout(res, ms); }); @@ -73,7 +73,7 @@ function getNewDateUrl(date: Date): string { return `${BASE_URL}/placebydate/${USA_ID}/${format(date, "yyyy-MM-dd")}`; } -function writeToCsv(trends: Array, csv: CSVManager) { +function writeToCsv(trends: Array, csv: CSVManager): void { for (let i = 0; i < trends.length; i++) { if (i == 20) break; // Should Only Write 20 per day hopefully. @@ -94,23 +94,23 @@ class CSVManager { this.path = path; } - public write(row: string) { + public write(row: string): void { 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() { + public createWriteStream(): void { if (!fs.existsSync(this.path)) fs.closeSync(fs.openSync(this.path, "w")); this.stream = fs.createWriteStream(this.path, { flags: "a" }); } - public closeWriteStream() { + public closeWriteStream(): void { this.stream.end(); this.stream = null; } - public getPath() { + public getPath(): string { return this.path; } } @@ -121,24 +121,24 @@ class Trend { private ranking: number; constructor(ranking: number, name: string, date: Date) { - this.name = name; + this.name = name.trim(); this.date = date; this.ranking = ranking; } - public getName() { + public getName(): string { return this.name; } - public getDate() { + public getDate(): Date { return this.date; } - public getRanking() { + public getRanking(): number { return this.ranking; } - public toCsv() { + public toCsv(): string { return `${this.name}, ${this.ranking}, ${format(this.date, "yyyy-MM-dd")}\n`; } }