-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjson_handler.py
More file actions
73 lines (61 loc) · 2.62 KB
/
json_handler.py
File metadata and controls
73 lines (61 loc) · 2.62 KB
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
import json
from datetime import datetime
from random import choice
from functools import partial
from hardcode_values import GROUP_ID
from strings import GRATULATIONS
from telegram.ext import JobQueue
from telegram.utils.helpers import mention_html
_dates = json.load(open("birthdays.json"))
def save_birthdays(new_birthday):
_dates.update(new_birthday)
with open('birthdays.json', 'w') as outfile:
json.dump(_dates, outfile, indent=4, sort_keys=True)
def return_birthdays():
birthdays = []
for birthday_id in _dates:
birthdays.append(_dates[birthday_id])
return birthdays
def _seconds(date, against):
delta = date[1] - against
delta_seconds = delta.total_seconds()
if delta_seconds == 0:
return -0.1
return -1 / delta_seconds
def next_birthday(job_queue: JobQueue):
from telegraph_handler import update_page
birthdays = []
for birthday_id in _dates:
birthdays.append([birthday_id, datetime.strptime(_dates[birthday_id]["birthday"][:-5], "%d.%m")])
today = datetime.now()
against = datetime(year=1900, month=today.month, day=today.day)
birthdays_sorted = sorted(birthdays, key=partial(_seconds, against=against))
closest_birthday_id = 0
for birthday in birthdays_sorted:
birthdate = birthday[1].date()
# this means birthday is today
if [birthdate.day, birthdate.month] == [today.day, today.month]:
if today.year - int(_dates[birthday[0]]["birthday"][-4:]) - _dates[birthday[0]]["age"]:
# this means we have to celebrate (since the difference isnt 0), so lets do this
increase_age(birthday[0])
msg = choice(GRATULATIONS).format(mention_html(int(birthday[0]), _dates[birthday[0]]["name"]),
_dates[birthday[0]]["age"])
job_queue._dispatcher.bot.send_message(GROUP_ID, msg, parse_mode="HTML")
update_page()
continue
closest_birthday_id = birthday[0]
break
if closest_birthday_id == 0:
# two situations can lead us here: the only birthday has birthday today or there are no birthdays in the file
# yet. Lets handle both
if _dates:
only_id = list(_dates.keys())[0]
return [only_id, _dates[only_id]]
return False
return [closest_birthday_id, _dates[closest_birthday_id]]
def increase_age(birthday_id):
new_age = _dates[birthday_id]["age"]
new_age += 1
_dates[birthday_id].update({"age": new_age})
with open('birthdays.json', 'w') as outfile:
json.dump(_dates, outfile, indent=4, sort_keys=True)