import { Log } from "fme-logger"; var L = new Log("models/drudge"); L.setLevel("info"); import * as request from "request-promise-native"; import * as url from "url"; import * as cheerio from "cheerio"; export class DrudgeArticle { html: string; time: Date; location: string; index: number; source: string; href: string; latest: Date; } export class DrudgeFetch { ready:boolean; history: DrudgeArticle[]; latest : DrudgeArticle[]; constructor () { L.debug("DrudgeFetch.constructor Starting; ===>") this.ready = false; this.history = []; this.latest = []; this.ready = true; } findNew() { var newArticles:any[] = []; return new Promise ( async (resolve,reject)=> { var articles:DrudgeArticle[] = await this.getLatest(); for (var i in articles ) { var article = articles[i]; if ( this.isDuplicate(article) ) { // L.info("DrudgeFetch: Duplicate found",article.href) continue; } else { newArticles.push(article); this.history.push(article); } } resolve(newArticles) }); } isDuplicate (article:any) { for (var i=0; i((resolve,reject)=> { var d:any[] = []; try { this.get(). then (data => { d.push(...this.parseHeadlines(data)); d.push(...this.parseMainHeadline(data)); d.push(...this.parseFirstColumn(data)); d.push(...this.parseSecondColumn(data)); d.push(...this.parseThirdColumn(data)); resolve(d) }) } catch (err) { L.error("drudgeFetch.getLatest Error",err); reject(err) } }); } get () { return new Promise((resolve, reject) => { request("http://drudgereport.com",(err,res,body) =>{ if (err) { reject(err); } else { resolve(body); } }) }); } parseHeadlines (body:string) { var links:any[] = []; var $ = cheerio.load(body); $("#app_topstories a").each(function(this:any, index:any){ var source = url.parse($(this).attr("href")).hostname links.push( { href: $(this).attr("href"), full_link: $(this).attr("href"), html: $(this).html(), time: new (Date), location: "leftHeadlines", index: index, source: source } ) }); return links; } parseMainHeadline (body:string) { var links:any[] = []; var $ = cheerio.load(body); $("#app_mainheadline a").each(function(this:any, index:any){ var source = url.parse($(this).attr("href")).hostname links.push( { href: $(this).attr("href"), full_link: $(this).attr("href"), html: $(this).html(), time: new (Date), location: "mainHeadlines", index: index, source: source } ) }); return links; } parseFirstColumn (body:string) { var links:any[] = []; var $ = cheerio.load(body); var col1 = $("#app_col1 table td")[0]; $(col1).find("a").each (function (this:any, idx:any){ var source = url.parse($(this).attr("href")).hostname links.push( { href: $(this).attr("href"), full_link: $(this).attr("href"), html: $(this).html(), time: new (Date), location: "firstColumn", index: idx, source: source } ) }) // // => filter to remove links list // for (var i=0; i filter to remove links liet // for (var i=0; i filter to remove links list // for (var i=0; i