Full database integration. No more json!
Showing
4 changed files
with
150 additions
and
3 deletions
No preview for this file type
1 | import json | 1 | import json |
2 | import sqlite3 | 2 | import sqlite3 |
3 | import datetime | ||
3 | 4 | ||
4 | conn = sqlite3.connect('db.sqlite3') | 5 | conn = sqlite3.connect('db.sqlite3') |
5 | 6 | ||
... | @@ -65,6 +66,152 @@ def import_messages(conn): | ... | @@ -65,6 +66,152 @@ def import_messages(conn): |
65 | pass | 66 | pass |
66 | conn.commit() | 67 | conn.commit() |
67 | 68 | ||
68 | import_jokes(conn) | ||
69 | import_fortunes(conn) | ||
70 | import_messages(conn) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
69 | def get_game_names(game_id_list): | ||
70 | games_file = 'games.json' | ||
71 | json_data=open(games_file).read() | ||
72 | data = json.loads(json_data) | ||
73 | result = [] | ||
74 | for game_id in game_id_list: | ||
75 | if isinstance(game_id, str) and not game_id.isdigit(): | ||
76 | result.append(game_id) | ||
77 | continue | ||
78 | name_set = False | ||
79 | for game in data: | ||
80 | if game['id'] == game_id: | ||
81 | result.append(game['name']) | ||
82 | name_set = True | ||
83 | return result | ||
84 | |||
85 | def dict_factory(cursor, row): | ||
86 | if row == None: | ||
87 | return None | ||
88 | d = {} | ||
89 | for idx, col in enumerate(cursor.description): | ||
90 | d[col[0]] = row[idx] | ||
91 | return d | ||
92 | |||
93 | def db_get_member(discord_id=None, username=None): | ||
94 | # Do a lookup by ID, if it's found but the name doesn't match then add a row to aliases with the previous name and change the member name | ||
95 | c = conn.cursor() | ||
96 | result = None | ||
97 | if discord_id: | ||
98 | result = c.execute("SELECT member_id, member_name, discord_id, discord_mention, is_afk, afk_at, status, prev_status, status_change_at, current_game FROM members WHERE discord_id = ?;", (discord_id,)).fetchone() | ||
99 | if username: | ||
100 | result = c.execute("SELECT member_id, member_name, discord_id, discord_mention, is_afk, afk_at, status, prev_status, status_change_at, current_game FROM members WHERE member_name = ?;", (username,)).fetchone() | ||
101 | return dict_factory(c, result) | ||
102 | def log(message): | ||
103 | print("{} - {}".format(datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S'), message)) | ||
104 | |||
105 | def db_add_game(member_id, game_name): | ||
106 | # Do a lookup by ID, if it's found but the name doesn't match then add a row to aliases with the previous name and change the member name | ||
107 | c = conn.cursor() | ||
108 | games = c.execute("SELECT game_id FROM games WHERE game_name = ?;", (game_name,)).fetchone() | ||
109 | db_game_id = 0 | ||
110 | if not games: | ||
111 | log("Adding Game: {}".format(game_name,)) | ||
112 | c.execute("INSERT INTO games(game_name) VALUES(?);", (game_name,)) | ||
113 | conn.commit() | ||
114 | db_game_id = c.execute("select last_insert_rowid();").fetchone()[0] | ||
115 | else: | ||
116 | db_game_id = games[0] | ||
117 | #log("DB Game ID: {}".format(db_game_id,)) | ||
118 | member_games = c.execute("SELECT launch_count FROM xmember_games WHERE game_id = ? AND member_id = ?;", (db_game_id, member_id)).fetchone() | ||
119 | if not member_games: | ||
120 | #log("Inserting Member Games: {}, {}".format(db_game_id, member_id)) | ||
121 | c.execute("INSERT INTO xmember_games(game_id, member_id, launch_count) VALUES(?, ?, 1);", (db_game_id, member_id)) | ||
122 | conn.commit() | ||
123 | else: | ||
124 | #log("Updating Member Games: {}, {}".format(db_game_id, member_id)) | ||
125 | c.execute("UPDATE xmember_games SET launch_count = launch_count + 1 WHERE game_id = ? AND member_id = ?;", (db_game_id, member_id)) | ||
126 | conn.commit() | ||
127 | |||
128 | def db_create_member(member): | ||
129 | # Do a lookup by ID, if it's found but the name doesn't match then add a row to aliases with the previous name and change the member name | ||
130 | c = conn.cursor() | ||
131 | c.execute("""INSERT INTO members (member_name, discord_id, discord_mention, | ||
132 | is_afk, afk_at, status, prev_status, | ||
133 | status_change_at, current_game) | ||
134 | VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?);""", (member.name.lower(), | ||
135 | member.id, member.mention, | ||
136 | 0, datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S'), | ||
137 | 'online', 'offline', | ||
138 | datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S'), | ||
139 | member.game_id)) | ||
140 | conn.commit() | ||
141 | if member.game_id != None: | ||
142 | db_add_game(db_get_member(member.id)['member_id'], member.game_id) | ||
143 | class Member: | ||
144 | pass | ||
145 | def import_games(conn): | ||
146 | games_file = 'games.json' | ||
147 | members_file = 'members.json' | ||
148 | |||
149 | json_data=open(members_file).read() | ||
150 | member_data = json.loads(json_data) | ||
151 | |||
152 | json_data=open(games_file).read() | ||
153 | game_data = json.loads(json_data) | ||
154 | |||
155 | c = conn.cursor() | ||
156 | user_games = {} | ||
157 | for username in member_data: | ||
158 | if 'games_played' in member_data[username]: | ||
159 | for game_id in member_data[username]['games_played']: | ||
160 | if (game_id != 'None' and game_id != 0): | ||
161 | if username not in user_games: | ||
162 | user_games[username] = [] | ||
163 | if game_id: | ||
164 | if isinstance( game_id, int) or game_id.isdigit(): | ||
165 | game_names = get_game_names([game_id]) | ||
166 | #print(game_names) | ||
167 | if len(game_names) > 0: | ||
168 | user_games[username].append(game_names[0]) | ||
169 | else: | ||
170 | user_games[username].append(game_id) | ||
171 | #print(user_games) | ||
172 | for username, game_list in user_games.iteritems(): | ||
173 | member = db_get_member(username=username) | ||
174 | if member: | ||
175 | for game in game_list: | ||
176 | db_add_game(member['member_id'], game) | ||
177 | print("Added Game: {} - {}".format(member['member_id'], game)) | ||
178 | else: | ||
179 | if 'id' not in member_data[username]: | ||
180 | continue | ||
181 | member_to_create = Member() | ||
182 | member_to_create.name = username | ||
183 | member_to_create.id = member_data[username]['id'] | ||
184 | member_to_create.mention = member_data[username]['mention'] | ||
185 | member_to_create.game_id = None | ||
186 | db_create_member(member_to_create) | ||
187 | print("missing {}".format(byteify(username))) | ||
188 | print(member_to_create) | ||
189 | |||
190 | |||
191 | |||
192 | # 'id': user_id, | ||
193 | # 'mention': mention, | ||
194 | # 'is_afk': is_afk, | ||
195 | # 'afk_at': afk_at, | ||
196 | # 'status': status, | ||
197 | # 'prev_status': prev_status, | ||
198 | # 'status_change_at': status_change_at, | ||
199 | # 'game_id': game_id, | ||
200 | # 'games_played': games_played, | ||
201 | # 'aliases': aliases | ||
202 | |||
203 | |||
204 | # for username in data: | ||
205 | # print("Username: %s" % username) | ||
206 | # for author in data[username]: | ||
207 | # try: | ||
208 | # c.execute("INSERT INTO messages (message, delivery_time, channel, message_from, message_to, user_id) VALUES (?, ?, ?, ?, ?, ?)", (data[username][author]['message'], data[username][author]['delivery_time'],data[username][author]['channel'], author, username, data[username][author]['user_id'])) | ||
209 | # except Exception as e: | ||
210 | # print(e) | ||
211 | # pass | ||
212 | # conn.commit() | ||
213 | |||
214 | #import_jokes(conn) | ||
215 | #import_fortunes(conn) | ||
216 | #import_messages(conn) | ||
217 | #import_games(conn) | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
This diff is collapsed.
Click to expand it.
This diff is collapsed.
Click to expand it.
-
Please register or sign in to post a comment