e506045 2 years ago
1 contributor
67 lines | 3.787kb
from userio import *
import requests
import re
import newsParser
from requests_html import HTML
from requests_html import HTMLSession


def article(url):
  say("Article: "+url)
  session = HTMLSession()
  response = session.get(url,timeout=20)
  pageContent=""
  article_only=""
  with response as r:
    articleStrTitle = r.html.xpath('//meta[@property="og:title"]/@content')[0]
    articleStrDescription = r.html.xpath('//meta[@property="og:description"]/@content')[0]
    articleStrImageUrl = r.html.xpath('//meta[@property="og:image"]/@content')[0]
    articleStrAuthor = r.html.xpath('//div[@class="author_wrapper"]/@content')
    article=r.html.find("main")[0]
    article_only+=article.html
    lenBefore=len(article_only)
    say("LengthBefore: "+str(lenBefore))
    pageContent += "<meta property=\"og:type\" content=\"article\" />\n"
    pageContent += "<meta property=\"og:title\" content=\""+articleStrTitle+"\" />\n"
    pageContent += "<meta property=\"og:description\" content=\""+articleStrDescription+"\" />\n"
    pageContent += "<meta property=\"og:url\" content=\""+url+"\" />\n"
    pageContent += "<meta property=\"og:image\" content=\""+articleStrImageUrl+"\" />\n"
    pageContent += "<meta property=\"og:image:type\" content=\"image/jpeg\" />\n"
    #pageContent += "<meta name=\"author\" content=\""+articleStrAuthor+"\" />\n"

  article_only = re.sub(r"<amp-img", '<img', article_only)
  article_only = re.sub(r"</amp-img>", '', article_only)
  article_only = re.sub(r"<h2", '<h3', article_only)
  article_only = re.sub(r"</h2>", '</h3>', article_only)
  article_only = re.sub(r"<h1", '<h2', article_only)
  article_only = re.sub(r"</h1>", '</h2>', article_only)
  article_only = re.sub(r'<script(.+?)/>','',article_only)
  article_only = re.sub(r'<script(.+?)</script>','',article_only,flags=re.M|re.S)
  article_only = re.sub(r'<section class="block block-mc-content-share-bookmark block-content-share-bookmark clearfix">(.+?)</section>','',article_only,flags=re.M|re.S)

  article_only = re.sub(r'<div id="ad-(.+?)</div>','',article_only,flags=re.M|re.S)
  article_only = re.sub(r'<section class="block block-ad-entity(.+?)</section>','',article_only,flags=re.M|re.S)
  article_only = re.sub(r'<div class="sponsors__item">(.+?)</div>','',article_only,flags=re.M|re.S)
  article_only = re.sub(r'<div class="sponsors">(.+?)</div>','',article_only,flags=re.M|re.S)
  article_only = re.sub(r'<a class="link link--trending"(.+?)</a>','',article_only,flags=re.M|re.S)
  article_only = re.sub(r'<div class="author-card__figure">(.+?)</div>','',article_only,flags=re.M|re.S)
  article_only = re.sub(r'<span class="social-link__text">(.+?)</span>','',article_only,flags=re.M|re.S)
  article_only = re.sub(r'<svg role="article"(.+?)</svg>','',article_only,flags=re.M|re.S)
  article_only = re.sub(r'<div class="OUTBRAIN"(.+?)</div>','',article_only,flags=re.M|re.S)
  article_only = re.sub(r'<div class="read-next__loader-icon"/>Content is loading...</div>','',article_only)
  article_only = re.sub(r'<a class="article__read-full-story-button"(.+?)</a>','',article_only,flags=re.M|re.S)
  #article_only = re.sub(r'','',article_only)
  article_only = re.sub(r"href=\"/",'href="https://www.channelnewsasia.com/',article_only)
  article_only = re.sub(r"src=\"/",'src="https://www.channelnewsasia.com/',article_only)
  article_only = re.sub(r"^$",'',article_only)
  article_only = re.sub(r'^\s*$', '',article_only,flags=re.M|re.S)
  article_only = re.sub(r"><",'>\n<',article_only)

  pageContent += "<article>\n"+article_only+"\n</article>\n"
  #pageContent += article_only
  lenAfter=len(article_only)
  lenGain=float(10000-int(float(100*lenAfter/lenBefore)*100))/100
  say("LengthAfter : "+str(lenAfter))
  say("Gain        : "+str(lenGain)+"%")
  return pageContent