-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
57 lines (51 loc) · 2.09 KB
/
app.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
from apscheduler.schedulers.blocking import BlockingScheduler
from urllib import request
import os
import logging
import variables
logging.basicConfig(
level=logging.INFO,
filename="scheduler.log",
filemode="w",
format="[%(asctime)s] %(levelname)s - %(message)s")
def scheduled_job():
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'}
url = variables.URL
req = request.Request(url=url, headers=headers)
try:
request.urlopen(req).read()
except Exception as e:
logging.info(f"{e}")
else:
logging.info(f"Successful connection {url}.")
if __name__ == "__main__":
logging.info("====== RUN APSCHEDULER")
logging.info(f"APSCHEDULER_YEAR = {variables.APSCHEDULER_YEAR}")
logging.info(f"APSCHEDULER_MONTH = {variables.APSCHEDULER_MONTH}")
logging.info(f"APSCHEDULER_DAY = {variables.APSCHEDULER_DAY}")
logging.info(f"APSCHEDULER_WEEK = {variables.APSCHEDULER_WEEK}")
logging.info(f"APSCHEDULER_DAY_OF_WEEK = {variables.APSCHEDULER_DAY_OF_WEEK}")
logging.info(f"APSCHEDULER_HOUR = {variables.APSCHEDULER_HOUR}")
logging.info(f"APSCHEDULER_MINUTE = {variables.APSCHEDULER_MINUTE}")
logging.info(f"APSCHEDULER_SECOND = {variables.APSCHEDULER_SECOND}")
logging.info("====== DONE")
# run once
scheduled_job()
# init Scheduler
sched = BlockingScheduler(timezone="Asia/Taipei")
# add job
sched.add_job(scheduled_job, "cron",
year=variables.APSCHEDULER_YEAR,
month=variables.APSCHEDULER_MONTH,
day=variables.APSCHEDULER_DAY,
week=variables.APSCHEDULER_WEEK,
day_of_week=variables.APSCHEDULER_DAY_OF_WEEK,
hour=variables.APSCHEDULER_HOUR,
minute=variables.APSCHEDULER_MINUTE,
second=variables.APSCHEDULER_SECOND)
print("Press Ctrl+{0} to exit".format("Break" if os.name == "nt" else "C"))
try:
sched.start()
except (KeyboardInterrupt, SystemExit):
pass