| 1 |
ed. |
| 2 |
|
| 3 |
import urllib.request |
| 4 |
import re |
| 5 |
import json |
| 6 |
import sys |
| 7 |
from datetime import datetime |
| 8 |
from html.parser import HTMLParser |
| 9 |
|
| 10 |
base_url = "https://www.abclinuxu.cz" |
| 11 |
url = "https://www.abclinuxu.cz/History?type=comments&uid={uid}".format(uid=sys.argv[1]) |
| 12 |
response = urllib.request.urlopen(url) |
| 13 |
text = response.read().decode("utf-8") |
| 14 |
|
| 15 |
abc_title = re.search("<h1>(.+?)</h1>", text).group(1) |
| 16 |
abc_table = re.search("<table class=\"ds\">(.+?)<\/table>", text, re.S).group(1) |
| 17 |
|
| 18 |
articles_iter = re.finditer("<tr>.+?href=\"(.+?)\">(.+?)</a>.+?\"td-datum\">.+?(\d{1,2}\.\d{1,2}\.\d{4} \d{2}:\d{2}).+?</tr>", abc_table, re.S) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class HTMLFilter(HTMLParser): |
| 23 |
text = "" |
| 24 |
def handle_data(self, data): |
| 25 |
self.text += data |
| 26 |
|
| 27 |
json_feed = "{{\"title\": \"{title}\", \"items\": [{items}]}}" |
| 28 |
items = list() |
| 29 |
|
| 30 |
for article in articles_iter: |
| 31 |
article_url = json.dumps(base_url + article.group(1)) |
| 32 |
|
| 33 |
f = HTMLFilter() |
| 34 |
f.feed(article.group(2)) |
| 35 |
|
| 36 |
article_title = json.dumps(f.text) |
| 37 |
article_time = json.dumps(datetime.strptime(article.group(3), "%d.%m.%Y %H:%M").isoformat()) |
| 38 |
items.append("{{\"title\": {title}, \"content_html\": {html}, \"url\": {url}, \"date_published\": {date}}}".format(title=article_title, |
| 39 |
html=article_title, |
| 40 |
url=article_url, |
| 41 |
date=article_time)) |
| 42 |
|
| 43 |
json_feed = json_feed.format(title=abc_title, items=", ".join(items)) |
| 44 |
print(json_feed) |