-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.py
executable file
·62 lines (54 loc) · 1.92 KB
/
scheduler.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
#!/usr/bin/env python
from mastodon import Mastodon
import requests
from apscheduler.schedulers.blocking import BlockingScheduler
import logging
import os
import json
from zoneinfo import ZoneInfo
from BeachBot import BeachBot
logger = logging.getLogger("BeachBot")
logging.basicConfig(level=logging.INFO)
SERVER = os.environ.get("SERVER", "https://localhost")
TOKEN = os.environ.get("TOKEN", "insert coin")
TIMEZONE = os.environ.get("TIMEZONE", "Australia/Sydney")
RUNTIMES = os.environ.get("RUNTIMES", "23:18,23:21")
MAXLEN = os.environ.get("MAXLEN", 500)
URL = "https://api.beachwatch.nsw.gov.au/public/sites/geojson"
AREA_FILE = os.environ.get("AREA_FILE", "areas.json")
def runner():
logger.info("Running beachbot")
# https://github.com/halcy/Mastodon.py
# Create an instance of the Mastodon class
mastodon = Mastodon(
access_token=TOKEN,
api_base_url=SERVER
)
geojson = get_data(URL)
areas = get_areas(AREA_FILE)
beachbot = BeachBot(mastodon, areas, geojson, MAXLEN, TIMEZONE)
beachbot.do_all_the_things()
def get_beach_bot(mastodon: Mastodon) -> BeachBot:
geojson = get_data(URL)
areas = get_areas(AREA_FILE)
return BeachBot(mastodon, areas, geojson, MAXLEN, TIMEZONE)
def schedule(times: str, tz: str) -> None:
sched = BlockingScheduler()
tzinfo = ZoneInfo(tz)
for time in times.split(","):
time = time.split(":")
logger.info("Scheduling for {hour}:{minute} ({tz})".format(hour=time[0], minute=time[1], tz=tz))
sched.add_job(runner, 'cron', hour=time[0], minute=time[1], timezone=tzinfo)
sched.start()
def get_data(url: str) -> list:
response = requests.get(url)
data = response.json()
return data
def get_areas(file: str) -> list:
with open(file, "r") as reader:
return json.load(reader)
if __name__ == "__main__":
logger.info("Schedulting run.")
schedule(RUNTIMES, TIMEZONE)
runner()
logger.info("Be done.")