forked from ccev/stopwatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.py
More file actions
206 lines (178 loc) · 6.14 KB
/
Copy pathinit.py
File metadata and controls
206 lines (178 loc) · 6.14 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
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
# -*- coding: utf-8 -*-
import time
import requests
import argparse
from pymysql import connect
try:
from ConfigParser import ConfigParser
except ImportError:
from configparser import ConfigParser
DEFAULT_CONFIG = "default.ini"
QUERY_CHECK = """SELECT {db_id} FROM {db_dbname}.{db_table}"""
def create_config(config_path):
config = dict()
config_raw = ConfigParser()
config_raw.read(DEFAULT_CONFIG)
config_raw.read(config_path)
### Config
config['send_stops'] = config_raw.getboolean(
'Config',
'STOPS')
config['send_gyms'] = config_raw.getboolean(
'Config',
'GYMS')
config['send_portals'] = config_raw.getboolean(
'Config',
'PORTALS')
config['db_scan_schema'] = config_raw.get(
'DB',
'SCANNER_DB')
config['db_portal_schema'] = config_raw.get(
'DB',
'PORTAL_DB')
config['db_host'] = config_raw.get(
'DB',
'HOST')
config['db_port'] = config_raw.getint(
'DB',
'PORT')
config['db_user'] = config_raw.get(
'DB',
'USER')
config['db_pass'] = config_raw.get(
'DB',
'PASSWORD')
config['db_portal_dbname'] = config_raw.get(
'DB',
'PORTAL_DB_NAME')
config['db_dbname'] = config_raw.get(
'DB',
'NAME')
config['db_stop_table'] = config_raw.get(
'DB',
'POKESTOP_TABLE')
config['db_stop_id'] = config_raw.get(
'DB',
'POKESTOP_ID')
config['db_gym_table'] = config_raw.get(
'DB',
'GYM_TABLE')
config['db_gym_id'] = config_raw.get(
'DB',
'GYM_ID')
config['db_portal_table'] = config_raw.get(
'DB',
'PORTAL_TABLE')
config['db_portal_id'] = config_raw.get(
'DB',
'PORTAL_ID')
return config
def connect_db(config):
print("Initializing")
mydb = connect(
host=config['db_host'],
user=config['db_user'],
passwd=config['db_pass'],
database=config['db_dbname'],
port=config['db_port'],
autocommit=True)
cursor = mydb.cursor()
return cursor
def db_config(config):
if config['db_scan_schema'] == "mad":
config['db_stop_table'] = "pokestop"
config['db_stop_id'] = "pokestop_id"
config['db_gym_table'] = "gym"
config['db_gym_id'] = "gym_id"
if config['db_scan_schema'] == "rdm":
config['db_stop_table'] = "pokestop"
config['db_stop_id'] = "id"
config['db_gym_table'] = "gym"
config['db_gym_id'] = "id"
if config['db_portal_schema'] == "pmsf":
config['db_portal_table'] = "ingress_portals"
config['db_portal_id'] = "external_id"
def get_portals():
return open("txt/portals.txt", "r").read().splitlines()
def get_stops_full():
return open("txt/stop_full.txt", "r").read().splitlines()
def get_stops_unfull():
return open("txt/stop_unfull.txt", "r").read().splitlines()
def get_gyms_unfull():
return open("txt/gym_unfull.txt", "r").read().splitlines()
def get_gyms_full():
return open("txt/gym_full.txt", "r").read().splitlines()
def write_portals(cursor, config):
if config['send_portals']:
print("Checking for portals to write")
check_portals_query = QUERY_CHECK.format(
db_id=config['db_portal_id'],
db_dbname=config['db_portal_dbname'],
db_table=config['db_portal_table']
)
cursor.execute(check_portals_query)
portals = cursor.fetchall()
for db_id in portals:
for var in db_id:
if not var in get_portals():
print("Writing portal id", var)
with open("txt/portals.txt", "a") as f:
f.write(var + "\n")
def write_stops(cursor, config):
if config['send_stops']:
print("Checking for stops to write")
check_stops_query = QUERY_CHECK.format(
db_id=config['db_stop_id'],
db_dbname=config['db_dbname'],
db_table=config['db_stop_table']
)
cursor.execute(check_stops_query)
stops = cursor.fetchall()
for db_id in stops:
for var in db_id:
if not var in get_stops_full():
print("Writing full stop id", var)
with open("txt/stop_full.txt", "a") as f:
f.write(var + "\n")
for db_id in stops:
for var in db_id:
if not var in get_stops_unfull():
print("Writing unfull stop id", var)
with open("txt/stop_unfull.txt", "a") as f:
f.write(var + "\n")
def write_gyms(cursor, config):
if config['send_gyms']:
print("Checking for gyms to write")
check_gyms_query = QUERY_CHECK.format(
db_id=config['db_gym_id'],
db_dbname=config['db_dbname'],
db_table=config['db_gym_table']
)
cursor.execute(check_gyms_query)
gyms = cursor.fetchall()
for db_id in gyms:
for var in db_id:
if not var in get_gyms_full():
print("Writing full gym id", var)
with open("txt/gym_full.txt", "a") as f:
f.write(var + "\n")
for db_id in gyms:
for var in db_id:
if not var in get_gyms_unfull():
print("Writing unfull gym id", var)
with open("txt/gym_unfull.txt", "a") as f:
f.write(var + "\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-c", "--config", default="default.ini", help="Config file to use")
args = parser.parse_args()
config_path = args.config
config = create_config(config_path)
cursor = connect_db(config)
db_config(config)
write_portals(cursor, config)
write_stops(cursor, config)
write_gyms(cursor, config)
cursor.close()
print("Done.")