forked from sibalzer/primelooter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
primelooter.py
170 lines (150 loc) · 4.84 KB
/
primelooter.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import argparse
import logging
import sys
import asyncio
import time
import traceback
from legacy import read_cookiefile, PrimeLooter, AuthException
from experiment import primelooter
from logging import LogRecord
def build_handler_filters(handler: str):
def handler_filter(record: LogRecord):
if hasattr(record, "block"):
if record.block == handler:
return False
return True
return handler_filter
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.addFilter(build_handler_filters("console"))
file_handler = logging.FileHandler("primelooter.log")
file_handler.addFilter(build_handler_filters("file"))
logging.basicConfig(
level=logging.INFO,
# format="%(asctime)s [%(levelname)s] %(msg)s",
format="{asctime} [{levelname}] {message}",
style="{",
datefmt="%Y-%m-%d %H:%M:%S",
handlers=[file_handler, stream_handler],
)
log = logging.getLogger()
def use_legacy_playwright(cookie_file, publishers, headless, use_chrome=False):
cookies = read_cookiefile(cookie_file)
with PrimeLooter(cookies, publishers, headless, use_chrome) as looter:
try:
looter.run(dump)
except AuthException as ex:
log.error(ex)
sys.exit(1)
except Exception as ex:
log.error(ex)
traceback.print_tb(ex.__traceback__)
def use_experimental_api(cookie_file):
asyncio.run(primelooter(cookie_file))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Notification bot for the lower saxony vaccination portal")
parser.add_argument(
"--legacy",
dest="legacy",
help="Tells Primelooter to use the legacy Playwright implementation instead of the API.",
required=False,
action="store_true",
default=False,
)
parser.add_argument(
"-p",
"--publishers",
dest="publishers",
help="Path to publishers.txt file",
required=False,
default="publishers.txt",
)
parser.add_argument(
"-c",
"--cookies",
dest="cookies",
help="Path to cookies.txt file",
required=False,
default="cookies.txt",
)
parser.add_argument(
"-l",
"--loop",
dest="loop",
help="Shall the script loop itself? (Cooldown 24h)",
required=False,
action="store_true",
default=False,
)
parser.add_argument(
"--dump",
dest="dump",
help="Dump html to output",
required=False,
action="store_true",
default=False,
)
parser.add_argument(
"-d",
"--debug",
dest="debug",
help="Print Log at debug level",
required=False,
action="store_true",
default=False,
)
parser.add_argument(
"-nh",
"--no-headless",
dest="headless",
help="Shall the script not use headless mode?",
required=False,
action="store_false",
default=True,
)
arg = vars(parser.parse_args())
with open(arg["publishers"]) as f:
publishers = f.readlines()
publishers = [x.strip() for x in publishers]
headless = arg["headless"]
dump = arg["dump"]
legacy = arg["legacy"]
cookie_file = arg["cookies"]
if arg["debug"]:
log.level = logging.DEBUG
while True:
try:
log.info("Starting Prime Looter\n")
if legacy:
log.warning(
"WARNING: The Legacy Playwright tool is no longer supported. "
"The code will be deleted soon as its not feasible for long term maintainence. "
"Please consider using the new experimental API Wrapper and opening PRs for any "
"features missing in the new code versus the old!"
)
use_legacy_playwright(cookie_file, publishers, headless)
else:
use_experimental_api(cookie_file)
log.info("Finished Looting!\n")
except AuthException as ex:
log.error(ex)
sys.exit(1)
except Exception as ex:
log.error(ex)
traceback.print_tb(ex.__traceback__)
time.sleep(60)
else:
if arg["loop"]:
log.info("Loop Enabled, sleeping for 24 hours.")
stream_handler.terminator = "\r"
sleep_time = 60 * 60 * 24
for time_slept in range(sleep_time):
m, s = divmod(sleep_time - time_slept, 60)
h, m = divmod(m, 60)
log.info(
f"{h:d}:{m:02d}:{s:02d} till next run...",
extra={"block": "file"},
)
time.sleep(1)
stream_handler.terminator = "\n"
if not arg["loop"]:
break