1 contributor
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)
if not "/amphtml" in url:
say("Trying AMP")
url = url.replace("buzzfeednews.com/article","buzzfeednews.com/amphtml")
url = url.replace("buzzfeed.com/","buzzfeed.com/amphtml/")
url.replace("?origin=web-hf","")
session = HTMLSession()
response = session.get(url,timeout=20,headers={'Accept-Encoding': 'deflate', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'})
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')
#print(articleStrAuthor)
article=r.html.find("main")[0]
#article=r.html.find("body")[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'<nav class="newsblock-trending-tags-bar__nav"(.+?)</nav>','',article_only,flags=re.M|re.S)
article_only = re.sub(r'<div class="newsblock-trending-tags-bar__title-wrapper"(.+?)</div>','',article_only,flags=re.M|re.S)
article_only = re.sub(r'<amp-ad type(.+?)</amp-ad>','',article_only,flags=re.M|re.S)
article_only = re.sub(r'<amp-social-share(.+?)</amp-social-share>','',article_only,flags=re.M|re.S)
article_only = re.sub(r'<p class="xs-pb1 xs-pt1 xs-text-center caps xs-text-6 text-gray-lightest ad__disclosure">(.+?)</p>','',article_only,flags=re.M|re.S)
#article_only = re.sub(r'','',article_only)
#article_only = re.sub(r"href=\"/",'href="https://bfmtv.com/',article_only)
#article_only = re.sub(r"src=\"/",'src="https://bfmtv.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"
lenAfter=len(article_only)
lenGain=float(10000-int(float(100*lenAfter/lenBefore)*100))/100
say("LengthAfter : "+str(lenAfter))
say("Gain : "+str(lenGain)+"%")
return pageContent
def articleOld(url):
say("Article: "+url)
if not "/amphtml" in url:
say("Trying AMP")
url = url.replace("buzzfeednews.com/article","buzzfeednews.com/amphtml")
url = url.replace("buzzfeed.com/","buzzfeed.com/amphtml/")
url.replace("?origin=web-hf","")
r = requests.get(url, allow_redirects=True)
content = r.text
pageContent = ""
articleCstBegin = "<article "
articleCstEnd = "<div class=\"subbuzz subbuzz-bfp\">"
articleCstEnd2 = "</article>"
articleCstEnd3 = "<div class=\"shares shares--inline"
articleStrTitle = newsParser.articleTitle(content)
articleStrDescription = newsParser.articleDescription(content)
articleStrImageUrl = newsParser.articleImage(content)
indexBegin = content.index(articleCstBegin)
try:
indexEnd = content.index(articleCstEnd)
except:
try:
indexEnd = content.index(articleCstEnd2)
except:
indexEnd = content.index(articleCstEnd3)
pageContent = ""
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\">"
article_only = ""
article_only += "<h1>"+articleStrTitle+"</h1>\n"
article_only += "<em>"+articleStrDescription+"</em>\n"
article_only += content[indexBegin:indexEnd]
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"<picture><source media=\"(.*?)\" srcSet=\"(.*?)\?(.*?)</picture>", "<img src=\"\g<2>\">", article_only)
article_only = re.sub(r"<amp-social-share (.*?)>", "<amp-social-share>", article_only)
article_only = re.sub(r"<span class=\"icon icon--primary flex\">", "<span>", article_only)
article_only = re.sub(r"<title>(.*?)</title>", "", article_only)
article_only = re.sub(r"<use xlink:href=\"(.*?)\">", "<use>", article_only)
article_only = re.sub(r"<svg class=\"svg-(.*?)\">", "<svg height=\"1px\">", article_only)
article_only = re.sub(r"Share on Facebook", "", article_only)
article_only = re.sub(r"Share on Pinterest", "", article_only)
article_only = article_only.replace("><", ">\n<")
if "buzzfeed.com" in url:
article_only = re.sub(r"href=\"\/", 'href=\"//www.buzzfeed.com/', article_only)
elif "buzzfeednews.com" in url:
article_only = re.sub(r"href=\"\/", 'href=\"//www.buzzfeednews.com/', article_only)
pageContent += "<article>"+article_only+"</article>"
return pageContent