1 contributor
from userio import *
import requests
import re
from requests_html import HTML
from requests_html import HTMLSession
def articleImage(content):
articleImgBegin ="<meta property=\"og:image\" content=\""
articleImgEnd ="\""
indexImgBegin = content.index(articleImgBegin)
indexImgEnd = content.index(articleImgEnd,indexImgBegin+len(articleImgBegin))
try:
image = content[indexImgBegin+len(articleImgBegin):indexImgEnd]
except:
image = "favicon.png"
return image
def articleDescription(content):
articleImgBegin ="<meta property=\"og:description\" content=\""
articleImgEnd ="\""
indexImgBegin = content.index(articleImgBegin)
indexImgEnd = content.index(articleImgEnd,indexImgBegin+len(articleImgBegin))
try:
title = content[indexImgBegin+len(articleImgBegin):indexImgEnd]
except:
title = "Description Extraction Failed"
return title
def articleTitle(content):
#articleImgBegin ="<meta property=\"og:title\" content=\""
articleImgBegin ="\"og:title\" content=\""
articleImgEnd ="\""
indexImgBegin = content.index(articleImgBegin)
indexImgEnd = content.index(articleImgEnd,indexImgBegin+len(articleImgBegin))
try:
title = content[indexImgBegin+len(articleImgBegin):indexImgEnd]
except:
title = "Title Extraction Failed"
return title
def article(url):
say("Article: "+url)
session = HTMLSession()
response = session.get(url,timeout=20)
pageContent=""
article_only=""
with response as r:
#articleStrTitle=r.html.find('title')[0].text
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('//meta[@name="author"]/@content')[0]
article=r.html.find("article")[0]
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+=article.html
lenBefore=len(article_only)
say("LengthBefore: "+str(lenBefore))
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"<div class=\"slate-ad__label\">Advertisement</div>",'',article_only)
#article_only = re.sub(r"<!-- data-uri=(.+?)-->",'',article_only)
#article_only = re.sub(r"<script data-uri=\"(.+?)</script>",'',article_only,flags=re.M|re.S)
#article_only = re.sub(r"<p class=\"slate-paragraph (.+?)\" data-word-count=\"(.+?)\" data-uri=\"(.+?)\">",'<p>',article_only)
#article_only = re.sub(r"<div class=\"slate-ad__creative\"(.+?)</div>",'',article_only,flags=re.M|re.S)
#article_only = re.sub(r"<div class=\"slate-ad (.+?)</div>",'',article_only,flags=re.M|re.S)
#article_only = re.sub(r"<div class=\"article__share-sidebar-xl share-sidebar\">(.+?)</div>",'',article_only,flags=re.M|re.S)
#article_only = re.sub(r"<div class=\"article__share-sidebar share-sidebar\">(.+?)</div>",'',article_only,flags=re.M|re.S)
#article_only = re.sub(r"<div class=\"article__left-rail\">(.+?)</div>",'',article_only,flags=re.M|re.S)
#article_only = re.sub(r"<div class=\"article__podcast-subscribe\"/>(.+?)</div>",'',article_only,flags=re.M|re.S)
#article_only = re.sub(r"<div class=\"social-share\" aria-label=\"social media links\"(.+?)>(.+?)</div>",'',article_only,flags=re.M|re.S)
#article_only = re.sub(r"<img (.+?)src=\"(.+?)\"(.+?)>",r'<img src="\2">',article_only)
#article_only = re.sub(r"data-uri=\"slate.com",'data-uri="https://slate.com',article_only)
#article_only = re.sub(r"<aside(.+)</aside>",'',article_only,flags=re.M|re.S)
#article_only = re.sub(r"<aside(.+)/>",'',article_only,flags=re.M|re.S)
article_only = re.sub(r"<script(.+)</script>",'',article_only,flags=re.M|re.S)
article_only = re.sub(r"<nav class=\"footer__socials\">(.+)</nav>",'',article_only,flags=re.M|re.S)
article_only = re.sub(r"<form method=\"post\" target=\"_blank\">(.+?)</form>",'',article_only,flags=re.M|re.S)
article_only = re.sub(r"<div class=\"article-header(.+?)\">",'<div>',article_only)
article_only = re.sub(r"<button class=\"sharing-btn(.+?)</button>",'',article_only)
article_only = re.sub(r"<div class=\"article-content(.+?)\">",'<div>',article_only)
article_only = re.sub(r" class=\"article-header__title\"",'',article_only)
article_only = re.sub(r" class=\"article-header__chapo\"",'',article_only)
article_only = re.sub(r"<div class=\"row\">",'<div>',article_only)
article_only = re.sub(r"<div (.+?)/>",'',article_only)
#article_only = re.sub(r"",'',article_only)
article_only = re.sub(r"href=\"/",'href="https://slate.fr/',article_only)
article_only = re.sub(r"src=\"/",'src="https://slate.fr/',article_only)
article_only = re.sub(r"<img class=\"image image--full lazyload\" data-full-src=\"",'<img src="',article_only)
article_only = re.sub(r"<div class=\"sharing-tools(.+?)>(.+?)</div>",'',article_only,flags=re.M|re.S)
article_only = re.sub(r"<div class=\"ad(.+?)>(.+?)</div>",'',article_only,flags=re.M|re.S)
article_only = re.sub(r"^$",'',article_only)
article_only = re.sub(r'^\s*$', '',article_only,flags=re.M|re.S)
#pageContent += "<article>\n"+article_only+"\n</article>\n"
pageContent += article_only+"\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