| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import urllib.request |
| 7 |
import requests |
| 8 |
import re |
| 9 |
import json |
| 10 |
import sys |
| 11 |
import ssl |
| 12 |
import http.client |
| 13 |
import http.cookies |
| 14 |
import dateparser |
| 15 |
import bs4 |
| 16 |
import datetime |
| 17 |
import pytz |
| 18 |
|
| 19 |
|
| 20 |
category = sys.argv[1] |
| 21 |
number_of_pages = int(sys.argv[2]) |
| 22 |
|
| 23 |
url_base = "https://hudebnibazar.cz" |
| 24 |
url = "{}/{}/?is=1&f=&n=vse&r=&i=50&o=datum&ign=on".format(url_base, category) |
| 25 |
json_feed = '{{"title": "HudebniBazar - {cat}", "version": "https://jsonfeed.org/version/1.1", "items": [{items}]}}' |
| 26 |
items = list() |
| 27 |
|
| 28 |
|
| 29 |
ct = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) |
| 30 |
ct.set_ciphers("DEFAULT:@SECLEVEL=0") |
| 31 |
|
| 32 |
|
| 33 |
def processListingDate(string_date: str): |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
dy = dateparser.parse(string_date, languages=["cs"]).replace(microsecond=0) |
| 54 |
local = pytz.timezone("Europe/Prague") |
| 55 |
return local.localize(dy).astimezone(pytz.utc) |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
def processListingImgs(listing: bs4.Tag): |
| 60 |
pics_elems = listing.find("div", class_="InzeratObr") |
| 61 |
pics = list() |
| 62 |
|
| 63 |
if pics_elems is None: |
| 64 |
return pics |
| 65 |
|
| 66 |
for pic in pics_elems.find_all("a"): |
| 67 |
pics.append(url_base + pic.get("href")) |
| 68 |
|
| 69 |
return pics |
| 70 |
|
| 71 |
|
| 72 |
def generateListingJson(listing: bs4.Tag): |
| 73 |
try: |
| 74 |
article_price = listing.find(class_="InzeratCena").contents[0].get_text(strip=True) |
| 75 |
except: |
| 76 |
article_price = "dohoda" |
| 77 |
|
| 78 |
article_title = listing.find(class_="InzeratNadpis").b.get_text(strip=True) |
| 79 |
article_date = listing.find(class_="InzeratZarazeno").get_text(strip=True) |
| 80 |
|
| 81 |
article_parsed_date = processListingDate(article_date) |
| 82 |
article_imgs = processListingImgs(listing) |
| 83 |
|
| 84 |
if article_imgs.count == 0: |
| 85 |
article_attachments = "" |
| 86 |
else: |
| 87 |
article_attachments = ", ".join( |
| 88 |
['{{"url": {}, "mime_type": "image/jpeg"}}'.format(json.dumps(i)) for i in article_imgs] |
| 89 |
) |
| 90 |
|
| 91 |
article_url = json.dumps(url_base + listing.find("a", recursive=False)["href"]) |
| 92 |
article_fulltitle = json.dumps("[{}] {}".format(article_price, article_title)) |
| 93 |
article_html = json.dumps(listing.find(class_="InzeratText").get_text(strip=True)) |
| 94 |
article_author = json.dumps( |
| 95 |
listing.find(lambda tag: tag.name == "div" and not tag.attrs).get_text( |
| 96 |
strip=True |
| 97 |
) |
| 98 |
) |
| 99 |
article_fulldate = json.dumps(article_parsed_date.isoformat()) |
| 100 |
|
| 101 |
return '{{"title": {title}, "id": {url}, "attachments": [{att}], "authors": [{{"name": {author}}}], "date_published": {publ}, "content_html": {html}, "url": {url}}}'.format( |
| 102 |
title=article_fulltitle, |
| 103 |
html=article_html, |
| 104 |
url=article_url, |
| 105 |
author=article_author, |
| 106 |
publ=article_fulldate, |
| 107 |
att=article_attachments, |
| 108 |
) |
| 109 |
|
| 110 |
|
| 111 |
php_ssid = None |
| 112 |
|
| 113 |
for page_number in range(1, number_of_pages + 1): |
| 114 |
page_url = url + "&p={}".format(page_number) |
| 115 |
page_request = urllib.request.Request(page_url) |
| 116 |
page_request.add_header("User-Agent", "curl/8.4.0") |
| 117 |
|
| 118 |
if php_ssid is not None: |
| 119 |
page_request.add_header("Cookie", "PHPSESSID={};".format(php_ssid)) |
| 120 |
|
| 121 |
page_response = urllib.request.urlopen(page_request, context=ct) |
| 122 |
page_html = page_response.read().decode("utf-8") |
| 123 |
|
| 124 |
if php_ssid is None: |
| 125 |
cook = http.cookies.SimpleCookie() |
| 126 |
cook.load(page_response.getheader("Set-Cookie")) |
| 127 |
php_ssid = cook.get("PHPSESSID").value |
| 128 |
|
| 129 |
soup = bs4.BeautifulSoup(page_html, "html.parser") |
| 130 |
listings = soup.find_all("div", class_="InzeratBody") |
| 131 |
|
| 132 |
for listing in listings: |
| 133 |
items.append(generateListingJson(listing)) |
| 134 |
|
| 135 |
json_feed = json_feed.format(cat=category, items=", ".join(items)) |
| 136 |
print(json_feed) |