-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
49 lines (35 loc) · 1.37 KB
/
tools.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
import datetime
import os
import requests
from Config import SENDER, RCPT, DEBUG, BOT_TOKEN, RCPT_USER_ID
def send_telegram_msg(msg, rcpt_user_id=RCPT_USER_ID, bot_token=BOT_TOKEN):
api_call = f'https://api.telegram.org/bot{bot_token}' + \
f'/sendMessage?chat_id={rcpt_user_id}' + \
f'&parse_mode=Markdown&text={msg}'
r = requests.get(api_call)
return r
def send_signal_msg(msg, sender=SENDER, rcpt=RCPT, debug=DEBUG):
syscall = f'signal-cli -u {sender} send -m \"{msg}\" {rcpt}'
print(syscall)
if not debug:
os.system(syscall)
def seconds_until(year, month, day, hour):
t = datetime.datetime(year, month, day, hour)
tnow = datetime.datetime.today()
return max(1, int((t - tnow).total_seconds()))
def calc_delay(delay, start_from=7, end_at=22):
t = datetime.datetime.today()
tnext = t + datetime.timedelta(days=1)
if t.hour >= end_at:
return seconds_until(tnext.year, tnext.month, tnext.day, start_from)
elif t.hour < start_from:
return seconds_until(t.year, t.month, t.day, start_from)
else:
return delay
if __name__ == '__main__':
#dt = datetime.datetime.today()
#print(dt)
#print(seconds_until(2021, 5, 18, 7))
#print(calc_delay(42, start_from=5, end_at=7))
#send_telegram_msg('Foobar was here!')
send_telegram_msg('Are you listening?')