| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import json |
| 5 |
import re |
| 6 |
import sys |
| 7 |
import urllib.request |
| 8 |
from html.parser import HTMLParser |
| 9 |
from bs4 import BeautifulSoup |
| 10 |
from datetime import datetime |
| 11 |
|
| 12 |
input_data = sys.stdin.read() |
| 13 |
soup = BeautifulSoup(input_data, 'html.parser') |
| 14 |
|
| 15 |
json_feed = "{{\"title\": {title}, \"items\": [{items}]}}" |
| 16 |
items = list() |
| 17 |
|
| 18 |
regex_batch = re.compile('^styles_postContent__.+$') |
| 19 |
for post_content in soup.find_all("div", {"class" : regex_batch}): |
| 20 |
regex_single = re.compile('^styles_content__.+$') |
| 21 |
for content in post_content.find_all("div", {"class" : regex_single}): |
| 22 |
article_url = json.dumps("https://www.producthunt.com" + content.p.a["href"]) |
| 23 |
article_title = json.dumps(content.h3.text) |
| 24 |
article_contents = json.dumps(content.p.text) |
| 25 |
article_time = json.dumps(datetime.utcnow().isoformat()) |
| 26 |
items.append("{{\"title\": {title}, \"content_html\": {html}, \"url\": {url}, \"date_published\": {date}}}".format( |
| 27 |
title=article_title, |
| 28 |
html=article_contents, |
| 29 |
url=article_url, |
| 30 |
date=article_time)) |
| 31 |
|
| 32 |
json_feed = json_feed.format(title = json.dumps(soup.title.text), items = ", ".join(items)) |
| 33 |
print(json_feed) |