-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcron.py
More file actions
71 lines (61 loc) · 1.89 KB
/
cron.py
File metadata and controls
71 lines (61 loc) · 1.89 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
from __future__ import annotations
import dataclasses
import datetime
import math
import time
import traceback
import typing
import requests
import advent_of_code
import config
import instagram
import log
import prun
import steam_news
import twitch
from bot import Bot
def main() -> None:
jobs: typing.Sequence[CronJob] = []
if config.bot.advent_of_code is not None and datetime.date.today().month in (12, 1):
jobs.append(CronJob(12 * 60 * 60, advent_of_code.check_leaderboards))
if config.bot.instagram is not None:
# https://developers.facebook.com/docs/graph-api/overview/rate-limiting#platform-rate-limits
# 240 * users / hour is 4× what we'll need
jobs.append(CronJob(60, instagram.new_media))
if config.bot.prun_upkeep is not None:
jobs.append(CronJob(60 * 60, prun.planetary_upkeep))
if config.bot.steam_news is not None:
jobs.append(CronJob(60, steam_news.news))
if config.bot.twitch is not None:
# https://dev.twitch.tv/docs/api/guide#rate-limits
# 30 points per minute, streams endpoint costs 1 point
jobs.append(CronJob(15, twitch.live_streams))
now = time.time()
for job in jobs:
job.last_run = now
period = math.gcd(*(job.interval for job in jobs))
bot = Bot({})
while True:
sleep_duration = now - time.time() + period
if sleep_duration > 0:
time.sleep(sleep_duration)
now = time.time()
for job in jobs:
if now - job.last_run < job.interval:
continue
job.last_run = now
try:
job.task(bot)
except requests.exceptions.HTTPError as e:
log.write(f'{job.task.__name__}: {e}\n{e.response.text[:1000]}')
except requests.exceptions.RequestException as e:
log.write(f'{job.task.__name__}: {e}')
except Exception:
log.write(f'{job.task.__name__}:\n{traceback.format_exc()}')
@dataclasses.dataclass(eq=False, slots=True)
class CronJob:
interval: int
task: typing.Callable[[Bot], None]
last_run: float = -1
if __name__ == '__main__':
main()