forked from pokealarm/pokealarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_pokealarm.py
213 lines (180 loc) · 9.48 KB
/
start_pokealarm.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Monkey Patch to allow Gevent's Concurrency
from gevent import monkey
monkey.patch_all()
# Setup Logging
import logging
logging.basicConfig(format='%(asctime)s [%(processName)15.15s][%(name)10.10s][%(levelname)8.8s] %(message)s',
level=logging.INFO)
# Standard Library Imports
import configargparse
from gevent import wsgi, spawn
import pytz
import Queue
import json
import os
import sys
# 3rd Party Imports
from flask import Flask, request, abort
# Local Imports
from PokeAlarm import config
from PokeAlarm.Manager import Manager
from PokeAlarm.WebhookStructs import RocketMap
from PokeAlarm.Utils import get_path, parse_unicode
# Reinforce UTF-8 as default
reload(sys)
sys.setdefaultencoding('UTF8')
# Set up logging
log = logging.getLogger('Server')
# Global Variables
app = Flask(__name__)
data_queue = Queue.Queue()
managers = {}
@app.route('/', methods=['GET'])
def index():
return "PokeAlarm Running!"
@app.route('/', methods=['POST'])
def accept_webhook():
try:
log.debug("POST request received from {}.".format(request.remote_addr))
data = json.loads(request.data)
if type(data) == dict: # older webhook style
data_queue.put(data)
else: # For RM's frame
for frame in data:
data_queue.put(frame)
except Exception as e:
log.error("Encountered error while receiving webhook ({}: {})".format(type(e).__name__, e))
abort(400)
return "OK" # request ok
# Thread used to distribute the data into various processes (for PokemonGo-Map format)
def manage_webhook_data(queue):
while True:
if queue.qsize() > 300:
log.warning("Queue length is at {}... this may be causing a delay in notifications.".format(queue.qsize()))
data = queue.get(block=True)
obj = RocketMap.make_object(data)
if obj is not None:
for name, mgr in managers.iteritems():
mgr.update(obj)
log.debug("Distributed to {}.".format(name))
log.debug("Finished distributing object with id {}".format(obj['id']))
queue.task_done()
# Configure and run PokeAlarm
def start_server():
log.setLevel(logging.INFO)
logging.getLogger('PokeAlarm').setLevel(logging.INFO)
logging.getLogger('requests').setLevel(logging.WARNING)
logging.getLogger('pyswgi').setLevel(logging.WARNING)
logging.getLogger('connectionpool').setLevel(logging.WARNING)
logging.getLogger('gipc').setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
parse_settings(os.path.abspath(os.path.dirname(__file__)))
# Start Webhook Manager in a Thread
spawn(manage_webhook_data, data_queue)
# Start up Server
log.info("PokeAlarm is listening for webhooks on: http://{}:{}".format(config['HOST'], config['PORT']))
server = wsgi.WSGIServer((config['HOST'], config['PORT']), app, log=logging.getLogger('pyswgi'))
server.serve_forever()
################################################## CONFIG UTILITIES ###################################################
def parse_settings(root_path):
config['ROOT_PATH'] = root_path
# Set the default config files up
config_files = [get_path('config/config.ini')] if '-cf' not in sys.argv and '--config' not in sys.argv else []
parser = configargparse.ArgParser(default_config_files=config_files)
parser.add_argument('-cf', '--config', is_config_file=True, help='Configuration file')
parser.add_argument('-d', '--debug', help='Debug Mode', action='store_true', default=False)
parser.add_argument('-H', '--host', help='Set web server listening host', default='127.0.0.1')
parser.add_argument('-P', '--port', type=int, help='Set web server listening port', default=4000)
parser.add_argument('-m', '--manager_count', type=int, default=1,
help='Number of Manager processes to start.')
parser.add_argument('-M', '--manager_name', type=parse_unicode, action='append', default=[],
help='Names of Manager processes to start.')
parser.add_argument('-k', '--key', type=parse_unicode, action='append', default=[None],
help='Specify a Google API Key to use.')
parser.add_argument('-f', '--filters', type=parse_unicode, action='append', default=['filters.json'],
help='Filters configuration file. default: filters.json')
parser.add_argument('-a', '--alarms', type=parse_unicode, action='append', default=['alarms.json'],
help='Alarms configuration file. default: alarms.json')
parser.add_argument('-gf', '--geofences', type=parse_unicode, action='append', default=[None],
help='Alarms configuration file. default: None')
parser.add_argument('-l', '--location', type=parse_unicode, action='append', default=[None],
help='Location, can be an address or coordinates')
parser.add_argument('-L', '--locale', type=parse_unicode, action='append', default=['en'],
choices=['de', 'en', 'es', 'fr', 'it', 'ko', 'zh_hk'],
help='Locale for Pokemon and Move names: default en, check locale folder for more options')
parser.add_argument('-u', '--units', type=parse_unicode, default=['imperial'], action='append',
choices=['metric', 'imperial'],
help='Specify either metric or imperial units to use for distance measurements. ')
parser.add_argument('-tl', '--timelimit', type=int, default=[0], action='append',
help='Minimum number of seconds remaining on a pokemon to send a notify')
parser.add_argument('-ma', '--max_attempts', type=int, default=[3], action='append',
help='Maximum number of attempts an alarm makes to send a notification.')
parser.add_argument('-tz', '--timezone', type=str, action='append', default=[None],
help='Timezone used for notifications. Ex: "America/Los_Angeles"')
args = parser.parse_args()
if args.debug:
log.setLevel(logging.DEBUG)
logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger('PokeAlarm').setLevel(logging.DEBUG)
logging.getLogger('Manager').setLevel(logging.DEBUG)
log.debug("Debug mode enabled!")
config['HOST'] = args.host
config['PORT'] = args.port
config['DEBUG'] = args.debug
# Check to make sure that the same number of arguements are included
for list_ in [args.key, args.filters, args.alarms, args.geofences, args.location,
args.locale, args.units, args.timelimit, args.max_attempts, args.timezone]:
if len(list_) > 1: # Remove defaults from the list
list_.pop(0)
size = len(list_)
if size != 1 and size != args.manager_count:
log.critical("Number of arguments must be either 1 for all managers or ".format(args.manager_count) +
"equal to Manager Count. Please provided the correct number of arguments.")
log.critical(list_)
sys.exit(1)
# Attempt to parse the timezones
for i in range(len(args.timezone)):
if str(args.timezone[i]).lower() == "none":
args.timezone[i] = None
continue
try:
log.info(args.timezone[i])
args.timezone[i] = pytz.timezone(args.timezone[i])
except pytz.exceptions.UnknownTimeZoneError:
log.error("Invalid timezone. For a list of valid timezones, " +
"see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones")
sys.exit(1)
# Build the managers
for m_ct in range(args.manager_count):
# This needs to be changed a few times... because
config['UNITS'] = args.units[m_ct] if len(args.units) > 1 else args.units[0]
m = Manager(
name=args.manager_name[m_ct] if m_ct < len(args.manager_name) else "Manager_{}".format(m_ct),
google_key=args.key[m_ct] if len(args.key) > 1 else args.key[0],
locale=args.locale[m_ct] if len(args.locale) > 1 else args.locale[0],
units=args.units[m_ct] if len(args.units) > 1 else args.units[0],
timezone=args.timezone[m_ct] if len(args.timezone) > 1 else args.timezone[0],
time_limit=args.timelimit[m_ct] if len(args.timelimit) > 1 else args.timelimit[0],
max_attempts=args.max_attempts[m_ct] if len(args.max_attempts) > 1 else args.max_attempts[0],
quiet=False, # TODO: I'll totally document this some day. Promise.
location=args.location[m_ct] if len(args.location) > 1 else args.location[0],
filter_file=args.filters[m_ct] if len(args.filters) > 1 else args.filters[0],
geofence_file=args.geofences[m_ct] if len(args.geofences) > 1 else args.geofences[0],
alarm_file=args.alarms[m_ct] if len(args.alarms) > 1 else args.alarms[0],
debug=config['DEBUG']
)
if m.get_name() not in managers:
# Add the manager to the map
managers[m.get_name()] = m
else:
log.critical("Names of Manager processes must be unique (regardless of capitalization)! Process will exit.")
sys.exit(1)
log.info("Starting up the Managers")
for m_name in managers:
managers[m_name].start()
########################################################################################################################
if __name__ == '__main__':
log.info("PokeAlarm is getting ready...")
start_server()