128 lines
3.8 KiB
Python
128 lines
3.8 KiB
Python
import os
|
|
|
|
abspath = os.path.abspath(__file__)
|
|
dname = os.path.dirname(abspath)
|
|
os.chdir(dname)
|
|
|
|
FEED_HEADER = """<?xml version="1.0" encoding="UTF-8"?>
|
|
<rss version="2.0">
|
|
<channel>
|
|
"""
|
|
|
|
from time import strftime, gmtime
|
|
FEED_TITLE = """
|
|
<title>{}</title>
|
|
<link>https://firmereferendum.giustizia.it/referendum/open</link>
|
|
<description>{}</description>
|
|
<language>it-it</language>
|
|
<pubDate>{}</pubDate>
|
|
<generator>makefeed.py</generator>
|
|
<ttl>720</ttl>
|
|
<docs>https://www.rssboard.org/rss-specification</docs>
|
|
<image>
|
|
<url>https://serben.kantai.online/rsslogo.png</url>
|
|
<title>Fac-simile logo della repubblica italiana</title>
|
|
<link>https://firmereferendum.giustizia.it/referendum/open</link>
|
|
</image>
|
|
""".format(
|
|
"NON UFFICIALE - Ministero della Giustizia - Referendum e iniziative popolari",
|
|
"Aggiornamenti sui referendum ed iniziative popolari in fase di raccolta firme",
|
|
strftime("%a, %-d %h %Y %H:%m:%S %z", gmtime())
|
|
)
|
|
|
|
FEED_FOOTER = """
|
|
</channel>
|
|
</rss>
|
|
"""
|
|
|
|
# strftime("%a, %-d %h %Y %H:%m:%S %z", gmtime())
|
|
|
|
URL_API = "https://firmereferendum.giustizia.it/referendum/api-portal/iniziativa/public"
|
|
URL_INFO = "https://firmereferendum.giustizia.it/referendum/open/dettaglio-open/"
|
|
AGENT = "Mozilla/5.0 (Windows; Windows NT 10.2;; en-US) Gecko/20100101 Firefox/52.7"
|
|
|
|
mesi = [
|
|
"gennaio", "febbraio", "marzo", "aprile",
|
|
"magggio", "giugno", "luglio", "agosto",
|
|
"settembre", "ottobre", "novembre", "dicembre"
|
|
]
|
|
|
|
def date2it(date):
|
|
date = date.split('-')
|
|
return f"{date[2]} {mesi[int(date[1])-1]} {date[0]}"
|
|
|
|
def htmlescape(string):
|
|
return string.replace('<','<').replace('>','>').replace('&', '&')
|
|
|
|
from datetime import datetime
|
|
from zoneinfo import ZoneInfo
|
|
def dataIns_to_iso(date, date_time):
|
|
time = date_time.split('T')[1]
|
|
tz = datetime.fromisoformat(date)
|
|
tz = tz.replace(tzinfo=ZoneInfo("Europe/Rome"))
|
|
tz = tz.utcoffset().total_seconds() / 3600
|
|
|
|
return_date = datetime.fromisoformat(date).strftime("%a, %-d %h %Y")
|
|
return_date += f" {time}"
|
|
return_date += f" {int(tz):+03}00"
|
|
return return_date
|
|
|
|
def make_item(itemdata):
|
|
# Do not include current votes in card, only quorum, as it is supposed to be immutable
|
|
item_str = """
|
|
<item>
|
|
<title>{}</title>
|
|
<guid>{}</guid>
|
|
<link>{}</link>
|
|
<pubDate>{}</pubDate>
|
|
<description>{}</description>
|
|
</item>
|
|
"""
|
|
title = itemdata['titolo']
|
|
link = URL_INFO + str(itemdata['id'])
|
|
|
|
insert_time = itemdata['dataIns']
|
|
validity = f"Puoi firmare per questa iniziativa dal {date2it(itemdata['dataInizioRaccolta'])} al {date2it(itemdata['dataFineRaccolta'])}"
|
|
initiative_type = itemdata['idDecTipoIniziativa']['nome']
|
|
text_description = itemdata['descrizione']
|
|
pub_time = dataIns_to_iso(itemdata['dataGazzetta'], itemdata['dataIns'])
|
|
|
|
website = ''
|
|
if 'sito' in itemdata:
|
|
itemdata['sito']
|
|
website = f'<br>Sito dell\'iniziativa: <a href="{website}">{website}</a>'
|
|
|
|
description = f"""
|
|
<b>{initiative_type}</b><br>
|
|
{text_description}<br>
|
|
<br>
|
|
<i>{validity}</i><br>
|
|
"""
|
|
|
|
return item_str.format(title,link,link,pub_time,htmlescape(description))
|
|
|
|
import urllib.request, json
|
|
|
|
if __name__ == "__main__":
|
|
print("Fetching data ...")
|
|
data = json.loads(
|
|
urllib.request.urlopen(
|
|
urllib.request.Request(
|
|
URL_API,
|
|
data=None,
|
|
headers={ 'User-Agent': AGENT }
|
|
)
|
|
).read().decode('utf-8')
|
|
)
|
|
print("Fetch complete")
|
|
|
|
if not data['resultDescription'] == "Successo":
|
|
print("Error in fetching information")
|
|
exit(1)
|
|
|
|
with open('rss.xml', 'w') as file:
|
|
file.write(FEED_HEADER + FEED_TITLE)
|
|
for item in data['content']:
|
|
file.write(make_item(item))
|
|
file.write(FEED_FOOTER)
|