-
Notifications
You must be signed in to change notification settings - Fork 0
/
social_handler.py
66 lines (51 loc) · 2.15 KB
/
social_handler.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
import paho.mqtt.client as mqtt
import pokemon_list
import mysql.connector
from mysql.connector import IntegrityError
from datetime import datetime, timedelta
import config
cnx = mysql.connector.connect(user=config.db['user'], password=config.db['password'],
host=config.db['host'],
database=config.db['database'])
cursor = cnx.cursor()
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("$SYS/#")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
if(msg.topic.startswith('pgomapcatch/all/catchable/')):
topic = msg.topic.split('pgomapcatch/all/catchable/')
pid = int(topic[1])
process_pokemon(pid, msg)
elif(msg.topic.startswith('pgomapgeo')):
topic = msg.topic.split('/')
pid = int(topic[2])
process_pokemon(pid, msg)
def process_pokemon(pid, msg):
pokemon_id = pid - 1
pokemon_name = pokemon_list.pokemon[pokemon_id]
data = msg.payload.split(',')
current_time = datetime.now() + timedelta(minutes=15)
time_string = current_time.strftime("%Y-%m-%d %H:%M:%S")
pokemon = (data[2], '', pokemon_id, data[0], data[1], time_string)
insert_pokemon(pokemon)
print("[" + time_string + "] " + pokemon_name + " found at " + data[0] + ", " + data[1])
def insert_pokemon(pokemon):
add_pokemon = ("INSERT INTO pokemon "
"(encounter_id, spawnpoint_id, pokemon_id, latitude, longitude, disappear_time) "
"VALUES (%s, %s, %s, %s, %s, %s)")
try:
cursor.execute(add_pokemon, pokemon)
except IntegrityError as e:
print "Duplicate"
cnx.commit()
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("test.mosca.io", 1883, 60)
client.subscribe("pgomapcatch/#", 0)
client.subscribe("pgomapgeo/#", 0)
client.loop_forever()