1 contributor
from userio import *
import requests
import re
import newsParser
def articleImage(content):
articleImgBegin ="<meta property=\"og:image\" content=\""
articleImgEnd ="\" />"
indexImgBegin = content.index(articleImgBegin)
indexImgEnd = content.index(articleImgEnd,indexImgBegin)
image = content[indexImgBegin+len(articleImgBegin):indexImgEnd]
return image
def articleTitle(content):
articleImgBegin ="<meta property=\"og:title\" content=\""
articleImgEnd ="\" />"
indexImgBegin = content.index(articleImgBegin)
indexImgEnd = content.index(articleImgEnd,indexImgBegin)
title = content[indexImgBegin+len(articleImgBegin):indexImgEnd]
return title
def article(url):
say("Article: "+url)
r = requests.get(url, allow_redirects=True, 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'})
content = r.text
pageContent = ""
articleCstBegin = "<article"
# ~ articleCstEnd = "</article>"
articleCstEnd = "<!-- /Pagination -->"
indexBegin = content.index(articleCstBegin)
indexEnd = content.index(articleCstEnd)
articleStrImageUrl = articleImage(content)
articleStrTitle = articleTitle(content)
articleStrDescription = newsParser.articleDescription(content)
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 = "<h2>"+articleStrTitle+"</h2>\n"
article_only += "<img src=\""+articleStrImageUrl+"\">\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"<a class=\"share(.*?)\" data-social-name=\"(.*?)\" href=\"(.*?)\" target=\"_blank\">", '<a href="">', article_only)
article_only = re.sub(r"<li class=\"(.*?) share-bar__item\">", '<li>', article_only)
article_only = re.sub(r"<div class=\"share-bar share-bar--sticky yr-share\">",'<div class="share-bar share-bar--sticky yr-share" style="display:none;">', article_only)
article_only = re.sub(r"<div class=\"(.*?) share-bar(.*?)>",'<div style="display:none;">', article_only)
article_only = re.sub(r"<div class=\"yr-share\">",'<div style="display:none;">', article_only)
article_only = article_only.replace("><", ">\n<")
article_only = re.sub(r"href=\"\/", 'href=\"//www.straitstimes.com/', article_only)
pageContent += "<article>"+article_only+"</article>"
return pageContent