Funny and useful scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

73 lines
2.2 KiB

#!/usr/local/bin/python3
from datetime import datetime
from num2words import num2words
import json
import os
import random
PERIODS = ["night", "morning", "day", "evening"]
TIMES_OF_DAY = ["ночи", "утра", "дня", "вечера"]
def load_phrases(filename):
with open(filename, 'r', encoding='utf-8') as f:
return json.load(f)
def get_time_of_day(hour, minute):
i = hour // 6
return PERIODS[i], TIMES_OF_DAY[i]
def pluralize(n, forms):
if n % 10 == 1 and n % 100 != 11:
return forms[0]
elif (n % 10 in [2, 3, 4]) and (n % 100 not in [12, 13, 14]):
return forms[1]
else:
return forms[2]
def generate_message():
now = datetime.now()
hour = now.hour
minute = now.minute
display_hour = hour
if hour == 0:
display_hour = 12
elif hour > 12:
display_hour = hour - 12
h_word = num2words(display_hour, lang = "ru", gender = "m")
m_word = num2words(minute, lang = "ru", gender = "f")
h_sfx = pluralize(display_hour, ['час', 'часа', 'часов'])
m_sfx = pluralize(minute, ['минута', 'минуты', 'минут'])
p_key, time_of_day = get_time_of_day(hour, minute)
if hour == 0 and minute == 0:
msg = random.choice(MIDNIGHT_PHRASES)
elif hour == 12 and minute == 0:
msg = random.choice(NOON_PHRASES)
elif minute == 0:
phrase_i = (hour % 12) % 6
if (hour > 0 and hour < 6) or (hour > 12 and hour < 18):
phrase_i = phrase_i - 1
tmpl = random.choice(HOURS_TEMPLATES[p_key][phrase_i])
msg = tmpl.format(h_word=h_word, h_sfx=h_sfx, time_of_day=time_of_day)
else:
tmpl = random.choice(MINUTE_PHRASES)
msg = tmpl.format(h_word=h_word, m_word=m_word, h_sfx=h_sfx, m_sfx = m_sfx, time_of_day=time_of_day)
return msg
user_home = os.path.expanduser("~")
phrases_dir = f"{user_home}/.local/share/time_signals"
phrases = load_phrases(f"{phrases_dir}/galaxy_news_radio.json")
MIDNIGHT_PHRASES = phrases['midnight_phrases']
NOON_PHRASES = phrases['noon_phrases']
HOURS_TEMPLATES = phrases['hour_templates_by_period']
MINUTE_PHRASES = phrases['minute_phrases']
print(generate_message())