File renamed from scaper.ts to scraper.ts
This commit is contained in:
parent
6a896efaef
commit
008e7c1cf4
|
@ -1,37 +0,0 @@
|
|||
import axios from "axios";
|
||||
import * as cheerio from "cheerio";
|
||||
|
||||
import * as fs from "fs";
|
||||
|
||||
// TODO: Move promise handling function to util file
|
||||
async function handlePromise(promise: Promise<any>) {
|
||||
try {
|
||||
const data = await promise;
|
||||
|
||||
return [true, data];
|
||||
} catch (err) {
|
||||
return [false, err];
|
||||
}
|
||||
}
|
||||
|
||||
export default function scrapeData(url: string) {
|
||||
return new Promise(async function (resolve, reject) {
|
||||
const [success, data] = await handlePromise(axios.get(url));
|
||||
|
||||
if (success) {
|
||||
const html = data.data;
|
||||
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
// For now, only scraping the first/top meaning;
|
||||
|
||||
const meaning = $(".meaning.mb-4").first().text().trim();
|
||||
|
||||
resolve(meaning);
|
||||
} else {
|
||||
console.error(data);
|
||||
|
||||
reject(data);
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue