-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrails
executable file
·184 lines (136 loc) · 4.74 KB
/
trails
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
import os
import sys
import time
import logging
import threading
import argparse
from columnar import columnar
from click import style
from twilio.rest import Client
from lib.multi import multiproc
from lib.transformations import filter_trails, sort_trails
from lib.config import get_config
import lib.constants
# Plugins
from plugins.vail import vail
from plugins.abasin import abasin
from plugins.mtn_powder import mtn_powder
from plugins.copper_eldora import copper_eldora
from plugins.loveland import loveland
def plugin(resort):
'''
wrapper func to run a plugin
'''
t = eval(resort["plugin"])(resort)
for a in t["areas"]:
for r in a["trails"]:
trail = [resort["name"], a["name"], r["name"], lib.constants.RATINGS_MAP[r["rating"]]["sym"], r["status"]]
data.append(trail)
def pretty_print(data):
'''
pretty print cli output
'''
patterns = [
('OPEN', lambda text: style(text, fg='green')),
('●', lambda text: style(text, fg='green')),
('■', lambda text: style(text, fg='blue')),
('⬬', lambda text: style(text, fg='red')),
]
table = columnar(data, headers=lib.constants.CLI_HEADER,patterns=patterns, no_borders=True)
print(table)
def parse_cli_args():
'''
parse cli args
'''
parser = argparse.ArgumentParser(description="Get Ski Trail Status", epilog="By default, show all trails from config.yaml")
parser.add_argument('-o', '--open', default=False, action='store_true', help="Open trails only")
parser.add_argument('-s', '--sort', default=False, help="sort by (--sort difficulty)")
parser.add_argument('-f', '--filter', default=[], action="append", help="filter by (--filter difficulty=blue)")
parser.add_argument('--server', default=False, action="store_true", help="start in server mode")
parser.add_argument('--debug', default=False, action="store_true", help="Server Mode: don't send texts")
parser.add_argument('--test', default=False, action="store_true", help="Server Mode: send one text with diff")
return parser.parse_args()
def process_trails(config):
global data
data = []
threads = []
for resort in config:
threads.append(threading.Thread(target=plugin, args=(resort,)))
multiproc(threads)
return data
def send_notifications(args, config, diff):
for n in config["notifications"]:
if diff:
logging.info("New trails found (%s)" % len(diff))
client = Client(account_sid, auth_token)
body = "🌲 NEW TRAILS OPEN! 🌲\n--------------------------\n"
for i in diff:
body += "%s - %s %s\n" % (i[0], i[2], i[3])
body = body.strip()
if not args.debug:
message = client.messages.create(
to=n["sms"],
from_=n["from"],
body=str(body))
else:
logging.info("Would have sent text:\n\n%s\n" % body)
def test_data(l):
'''
Add test data to test twilio
'''
test = ['Breckenridge', 'Peak 6', 'Irie', '♦♦', 'OPEN']
test2 = ['Keystone', 'Outback', 'Christmas Tree', '♦', 'OPEN']
test3 = ['Breckenridge', 'Peak 7 Alpine', 'Magic Carpet', '♦♦', 'OPEN']
l.append(test)
l.append(test2)
l.append(test3)
return l
def server(args, config):
global account_sid
global auth_token
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
old = []
while True:
# process trails
new = process_trails(config["resorts"])
# if this is the first loop, copy old data to new data
if not old:
old = new.copy()
continue
# append test data if testing twilio functionality
if args.test:
new = test_data(new)
# find the diff
diff = [x for x in new if x not in old]
# filter out trails that have closed
diff = filter_trails(diff, ["status=open"])
# send notifications
send_notifications(args, config, diff)
# copy new list to old list
old = new.copy()
if args.test:
sys.exit(0)
time.sleep(60)
def cli(args, config):
data = process_trails(config["resorts"])
if args.open:
data = [i for i in data if i[4] == "OPEN"]
if args.filter:
data = filter_trails(data, args.filter)
data = sort_trails(data, args.sort)
if data:
pretty_print(data)
def main():
'''
Main function
'''
logging.basicConfig(level=logging.INFO)
args = parse_cli_args()
config = get_config()
if args.server:
server(args, config)
cli(args, config)
if __name__ == "__main__":
main()