Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v1.0.3 #58

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ too long.
* channelnewsasia.com (CNAlifestyle)
* mothership.sg
* ricemedia.co
* scmp.com
* straitstimes.com
* tnp.sg
* todayonline.com
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}

BOT = {
"VERSION": "1.0.2",
"VERSION": "1.0.3",
"REPO_LINK": "https://github.com/fterh/sneakpeek",
"CONTRIBUTE_LINK": "https://github.com/fterh/sneakpeek"
}
Expand Down
63 changes: 63 additions & 0 deletions handlers/scmp_handler/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Handler for South China Morning Post."""

import requests
from bs4 import BeautifulSoup
from comment import Comment
from itertools import chain
from handlers.abstract_base_handler import AbstractBaseHandler, HandlerError

def get_text_from_children(children_element):
child_body = ""

for child in children_element:
if child['type'] == "text":
child_body += child['data']
elif child['type'] == "a" or child['type'] == "em":
child_body += get_text_from_children(child['children'])

return child_body

class ScmpHandler(AbstractBaseHandler):
"""Handler for Scmp."""

@classmethod
def handle(cls, url):
html = requests.get(url).text
soup = BeautifulSoup(html, "html.parser")

article_script = [script.text for script in soup.find_all("script") if script is not None and "window.__APOLLO_STATE__" in script.text.strip()]
json_article = json.loads(article_script[0][ article_script[0].find("=")+1: ])
json_content = json_article['contentService']

content_key = [k for k in json_content.keys() if k.startswith("$ROOT_QUERY.content")][0]
title = json_content[content_key]['headline']

subheadlines_element = json_content[content_key]['subHeadline']['json'][0]['children']

subheadlines = []
for elem in subheadlines_element:
if elem['type'] == "text":
subheadlines.append(elem['data'])
else:
for elem_child in elem['children']:
if elem_child['type'] == "text":
subheadlines.append(elem_child['data'])

subheadlines_md = "\n\n".join(["* " + subheadline.strip() for subheadline in subheadlines if subheadline.strip() != ""])

body_key = [k for k in json_content[content_key].keys() if k.startswith("body")][0]

body_list = []
for j in json_content[content_key][body_key]['json']:
if "children" not in j.keys():
continue

if j['type'] != "p":
continue

body_list.append(get_text_from_children(j['children']))

body_list = [b.strip() for b in body_list if b.strip() != ""]
body = subheadlines_md + "\n\n" + "\n\n".join(body_list)

return Comment(title, body.strip())