-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_processing.py
126 lines (100 loc) · 4.2 KB
/
data_processing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import datetime
import json
from json.decoder import JSONDecodeError
import requests
weekly_base_url = "https://www.fyysikkokilta.fi/wp-content/uploads/"\
"viikkotiedote-data/{year}/{week}.json"
weekly_base_url_en = "https://www.fyysikkokilta.fi/wp-content/uploads/"\
"viikkotiedote-data/{year}/{week}-en.json"
weekly_news_url = "https://www.fyysikkokilta.fi/viikkotiedote/"
weekly_news_url_en = "https://www.fyysikkokilta.fi/en/viikkotiedote/"
def get_schedule_data(filename: str):
"""Data contains chat ids and message languages for scheduled messages
Data format:
messages: {
[{ 'chat_id': 9827432, 'language': 'en'},
{ 'chat_id': 0972345, 'language': 'fi'}]
}
"""
with open(filename) as schedule_json:
data = json.load(schedule_json)
return data['messages']
def get_weekly_data(year, week_number, base_url=weekly_base_url):
week_string = "week{:02}".format(week_number)
url = base_url.format(year=year, week=week_string)
try:
data = requests.get(url).json()
except JSONDecodeError:
data = {}
return data
def current_news(base_url=weekly_base_url):
today = datetime.date.today()
year, week_number = today.isocalendar()[0:2]
return get_weekly_data(year, week_number, base_url)
def current_news_en():
return current_news(weekly_base_url_en)
def next_week_news(base_url=weekly_base_url):
today = datetime.date.today()
next_week = today + datetime.timedelta(days=7)
year, week_number = next_week.isocalendar()[0:2]
return get_weekly_data(year, week_number, base_url)
def next_week_news_en():
return next_week_news(weekly_base_url_en)
def news_message_fi(news_function):
def filter_by_date(date):
event_date = datetime.date(date[2], date[1], date[0])
today = datetime.date.today()
return event_date - today < datetime.timedelta(days=8)
try:
data = news_function()["_default"].values()
except KeyError:
return "Tiedote on tyhjä"
headers = {"guild_soon": [], "guild": [], "other": []}
for event in data:
if (event["category"] == "Killan tapahtumat") & (filter_by_date(event["date"])):
headers["guild_soon"].append(
"<a>⏰ {}</a>".format(event["header"])
)
elif event["category"] == "Killan tapahtumat":
headers["guild"].append(
"<a>• {}</a>".format(event["header"])
)
else:
headers["other"].append(
"<a>• {}</a>".format(event["header"])
)
guild_events_title = "<b>Killan tapahtumat</b>"
other_events_title = "\n<b>Muut</b>"
news_link = "\n<a href=\"{}\">Lue viikkotiedote</a>".format(weekly_news_url)
message = "\n".join([guild_events_title] + headers["guild_soon"] + headers["guild"] +
[other_events_title] + headers["other"] + [news_link])
return message
def news_message_en(news_function):
def filter_by_date(date):
event_date = datetime.date(date[2], date[1], date[0])
today = datetime.date.today()
return event_date - today < datetime.timedelta(days=8)
try:
data = news_function()["_default"].values()
except KeyError:
return "No weekly news"
headers = {"guild_soon": [], "guild": [], "other": []}
for event in data:
if (event["category"] == "Guild's events") & (filter_by_date(event["date"])):
headers["guild_soon"].append(
"<a>⏰ {}</a>".format(event["header"])
)
elif event["category"] == "Guild's events":
headers["guild"].append(
"<a>• {}</a>".format(event["header"])
)
else:
headers["other"].append(
"<a>• {}</a>".format(event["header"])
)
guild_events_title = "<b>Guild's events</b>"
other_events_title = "\n<b>Other events</b>"
news_link = "\n<a href=\"{}\">Read the Guild News</a>".format(weekly_news_url_en)
message = "\n".join([guild_events_title] + headers["guild_soon"] + headers["guild"] +
[other_events_title] + headers["other"] + [news_link])
return message