diff --git a/index.ts b/index.ts index b9d12a5..ceb3ce9 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,6 @@ "use strict"; -import { addDays } from 'date-fns'; +import { addDays, format } from 'date-fns'; import cheerio from 'cheerio'; import axios from 'axios'; import fs from 'fs'; @@ -9,7 +9,7 @@ const BASE_URL = "https://trendogate.com/"; const CSV_PATH = "./data.csv"; const USA_ID = 23424977; const NUMBER_OF_DAYS = 100; -const WAIT = 5000; // in ms +const WAIT = 2000; // in ms // e.g https://trendogate.com/placebydate/23424977/2015-04-01 // Topic | Position | Date @@ -64,19 +64,7 @@ function sleep(ms: number) { } function getNewDateUrl(date: Date): string { - const year: number = date.getFullYear(); - const month: number = date.getMonth(); - const day: number = date.getDay(); - let monthStr: string; - let dayStr: string; - - if (month < 10) monthStr = `0${month}`; - else monthStr = `${month}`; - - if (day < 10) dayStr = `0${day}`; - else dayStr = `${day}`; - - return `${BASE_URL}/placebydate/${USA_ID}/${year}-${monthStr}-${dayStr}`; + return `${BASE_URL}/placebydate/${USA_ID}/${format(date, "yyyy-MM-dd")}`; } function writeToCsv(trends: Array, csv: CSVManager) { @@ -100,7 +88,7 @@ class CSVManager { } public createWriteStream() { - if (fs.existsSync(this.path)) fs.closeSync(fs.openSync(this.path, "w")); + if (!fs.existsSync(this.path)) fs.closeSync(fs.openSync(this.path, "w")); this.stream = fs.createWriteStream(this.path, { flags: "a" }); } @@ -114,7 +102,6 @@ class CSVManager { } } - class Trend { private name: string; private date: Date; @@ -139,9 +126,8 @@ class Trend { } public toCsv() { - return `${this.name}, ${this.ranking}, ${this.date}\n`; + return `${this.name}, ${this.ranking}, ${format(this.date, "yyyy-MM-dd")}\n`; } } - scrapeData(); \ No newline at end of file