-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsalty_listener.py
116 lines (93 loc) · 4.29 KB
/
salty_listener.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
#! /usr/bin/env python2.7
# -*- coding: utf-8 -*-
import socket
import urlparse
import psycopg2
import psycopg2.extras
class WebRetrieve:
def __init__(self, dev, db_url, web_ip, web_port, web_secret):
self.development = dev
self.db_url = db_url
self.web_secret = ""
self.web_secret = web_secret
self.web_port = web_port
self.web_host = web_ip
self.web_s = socket.socket()
self.web_s.bind((self.web_host, self.web_port))
self.disect_url()
def disect_url(self):
urlparse.uses_netloc.append("postgres")
url_parts = urlparse.urlparse(self.db_url)
self.db_name = url_parts.path[1:]
self.db_user = url_parts.username
self.db_password = url_parts.password
self.db_host = url_parts.hostname
self.db_port = url_parts.port
def db_connect(self):
conn = psycopg2.connect(database=self.db_name, user=self.db_user, host=self.db_host, password=self.db_password, port=self.db_port)
return conn
def db_close(self, connection):
connection.close()
def setup_cursor(self, connection_inst, cf_type = psycopg2.extras.RealDictCursor):
cursor = connection_inst.cursor(cursor_factory=cf_type)
return cursor
def close_cursor(self, cursor):
cursor.close()
def execute_one(self, cursor, query, parameters = None):
cursor.execute(query, parameters)
return cursor.fetchone()
def execute_all(self, cursor, query, parameters = None):
cursor.execute(query, parameters)
return cursor.fetchall()
def initial_retrieve(self):
channels_dict = {}
conn = self.db_connect()
cur = self.setup_cursor(conn)
if not self.development:
users = self.execute_all(cur, """SELECT * FROM users AS u JOIN settings AS s on u.id=s.user_id WHERE s.active=true""")
commands = self.execute_all(cur, """SELECT * FROM commands AS c WHERE c.user_id in (SELECT s.user_id FROM Settings AS s WHERE s.active=true)""")
custom_commands = self.execute_all(cur, """SELECT * FROM custom_commands AS c WHERE c.user_id in (SELECT s.user_id FROM Settings AS s WHERE s.active=true)""")
else:
users = self.execute_all(cur, """SELECT * FROM users AS u JOIN settings AS s on u.id=1 AND u.id=s.user_id WHERE s.active=true""")
commands = self.execute_all(cur, """SELECT * FROM commands AS c WHERE c.user_id=1""")
custom_commands = self.execute_all(cur, """SELECT * FROM custom_commands AS c WHERE c.user_id=1""")
self.close_cursor(cur)
self.db_close(conn)
users_dict = {}
for i in users:
i["id"] = i["user_id"]
users_dict[i["id"]] = i
users_dict[i["id"]]["commands"] = []
users_dict[i["id"]]["custom_commands"] = []
for i in commands:
users_dict[i["user_id"]]["commands"].append(i)
for i in custom_commands:
users_dict[i["user_id"]]["custom_commands"].append(i)
for v in users_dict.values():
channels_dict[v["twitch_name"]] = v
return channels_dict
def update_retrieve(self, user_id):
user_dict = {}
conn = self.db_connect()
cur = self.setup_cursor(conn)
user = self.execute_one(cur, """SELECT u.*, s.* FROM users AS u JOIN settings AS s on s.user_id=%s WHERE u.id=%s""", parameters=(user_id, user_id))
commands = self.execute_all(cur, """SELECT c.* FROM commands AS c WHERE c.user_id=%s""", parameters=(user_id,))
custom_commands = self.execute_all(cur, """SELECT cc.* FROM custom_commands AS cc WHERE cc.user_id=%s""", parameters=(user_id,))
name = user["twitch_name"]
user_dict[name] = user
user_dict[name]["id"] = user_dict[name]["user_id"]
user_dict[name]["commands"] = commands
user_dict[name]["custom_commands"] = custom_commands
return user_dict
def main_listen(self):
self.web_s.listen(1)
connection, address = self.web_s.accept()
secret = connection.recv(128)
if secret != self.web_secret:
connection.close()
print address
raise ValueError
else:
to_update = connection.recv(128)
connection.close()
return to_update