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
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"<noscript(.+)</noscript>",'',article_only,flags=re.M|re.S)
#article_only = re.sub(r"",'',article_only)
article_only = re.sub(r"href=\"/",'href="https://slate.com/,',article_only)
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"
say("Length: "+str(len(article_only)))
return pageContent