forked from martinloland/returbil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.py
More file actions
executable file
·71 lines (54 loc) · 2.4 KB
/
scan.py
File metadata and controls
executable file
·71 lines (54 loc) · 2.4 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
import argparse
import asyncio
import os
from logger import Logger
from returbil import Returbil
def _set_up_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(description='This script scans http://returbil.no for desired trips.')
parser.add_argument(
'-usr', metavar='USER_KEY', type=str, help='Pushover user key')
parser.add_argument(
'-app', metavar='APP_TOKEN', type=str, help='Pushover app token')
parser.add_argument(
'--from-city', metavar='FROM', type=str, help='The city to travel from')
parser.add_argument(
'--to-city', metavar='TO', type=str, help='The city to travel to')
parser.add_argument(
'--interval', metavar='INTERVAL', type=int, default=60,
help='Interval time for scan in seconds (default 60)')
parser.add_argument(
'--fromfile', action="store_true", help='Load trips from `wanted.txt` instead')
parser.add_argument(
'--fuzzy', action="store_true",
help="Allow non-exact matches for city names, by only looking at the first word (e.g. "
"`Trondheim LUFTHAVN VÆRNES` would match if you have entered `Trondheim`)"
)
args = parser.parse_args()
if args.usr is None:
parser.error(message="You need to provide your pushover user key.")
if args.app is None:
parser.error(message="You need to provide your pushover app token.")
if args.fromfile:
if args.from_city or args.to_city:
print("`--fromfile` has precedence over `-from-city` and `-to-city`. Ignoring these two commands...")
if not os.path.exists('wanted.txt'):
parser.error(message="You need a file called `wanted.txt` in order to use the --fromfile command")
elif args.from_city is None:
parser.error(message="You need to provide a from value.")
elif args.to_city is None:
parser.error(message="You need to provide a to value.")
return args
async def main():
try:
args = _set_up_arguments()
returbil = Returbil(args=args)
Logger.add_entry(None)
Logger.add_entry("** Program startup **")
while True:
Logger.add_entry("Started looking for available trips...")
await returbil.parse_web_page()
await asyncio.sleep(delay=args.interval)
except Exception as e:
Logger.add_entry(f"ERROR: {e}")
if __name__ == "__main__":
asyncio.run(main())