added db module
This commit is contained in:
parent
30497075be
commit
e26a3ac63a
|
|
@ -0,0 +1,113 @@
|
|||
import psycopg2 as pg
|
||||
from os import environ as env
|
||||
|
||||
from extract import vals_in_order
|
||||
|
||||
|
||||
def connect():
|
||||
return pg.connect(
|
||||
dbname=env.get("POSTGRES_DB"),
|
||||
user=env.get("POSTGRES_USER"),
|
||||
password=env.get("POSTGRES_PASSWORD"),
|
||||
host=env.get("POSTGRES_HOST"),
|
||||
port=env.get("POSTGRES_PORT"),
|
||||
)
|
||||
|
||||
|
||||
def get_all_profile_names(conn=None):
|
||||
if not conn:
|
||||
conn = connect()
|
||||
|
||||
with conn.cursor() as curs:
|
||||
curs.execute("""SELECT name, leetify_user_id FROM data.profile_meta;""")
|
||||
data = curs.fetchall()
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def get_profile_game_count(user_id, conn=None):
|
||||
if not conn:
|
||||
conn = connect()
|
||||
|
||||
with conn.cursor() as curs:
|
||||
curs.execute(
|
||||
"""SELECT count(*) FROM data.profile_game
|
||||
WHERE leetify_user_id = %s;""",
|
||||
(user_id,),
|
||||
)
|
||||
data = curs.fetchone()
|
||||
|
||||
if not data:
|
||||
data = [0]
|
||||
return data[0]
|
||||
|
||||
|
||||
def get_profile_game_ids(user_id, conn=None):
|
||||
if not conn:
|
||||
conn = connect()
|
||||
|
||||
with conn.cursor() as curs:
|
||||
curs.execute(
|
||||
"""SELECT game_id FROM data.profile_game
|
||||
WHERE leetify_user_id = %s;""",
|
||||
(user_id,),
|
||||
)
|
||||
data = curs.fetchall()
|
||||
|
||||
return [row[0] for row in data]
|
||||
|
||||
|
||||
def insert_games(games, conn=None):
|
||||
if not conn:
|
||||
conn = connect()
|
||||
|
||||
cols_api = [
|
||||
"leetifyUserId",
|
||||
"ctLeetifyRating",
|
||||
"ctLeetifyRatingRounds",
|
||||
"dataSource",
|
||||
"elo",
|
||||
"gameFinishedAt",
|
||||
"gameId",
|
||||
"isCs2",
|
||||
"mapName",
|
||||
"matchResult",
|
||||
"scores",
|
||||
"skillLevel",
|
||||
"tLeetifyRating",
|
||||
"tLeetifyRatingRounds",
|
||||
"deaths",
|
||||
"hasBannedPlayer",
|
||||
"kills",
|
||||
"partySize",
|
||||
]
|
||||
cols_db = [
|
||||
"leetify_user_id",
|
||||
"ct_Leetify_rating",
|
||||
"ct_Leetify_rating_rounds",
|
||||
"data_source",
|
||||
"elo",
|
||||
"game_finished_at",
|
||||
"game_id",
|
||||
"is_cs2",
|
||||
"map_name",
|
||||
"match_result",
|
||||
"scores",
|
||||
"skill_level",
|
||||
"t_leetify_rating",
|
||||
"t_leetify_rating_rounds",
|
||||
"deaths",
|
||||
"has_banned_player",
|
||||
"kills",
|
||||
"party_size",
|
||||
]
|
||||
|
||||
vals = map(lambda game: vals_in_order(game, cols_api), games)
|
||||
|
||||
with conn.cursor() as curs:
|
||||
sql = f"""
|
||||
INSERT into data.profile_game ({', '.join(cols_db)})
|
||||
VALUES ({', '.join(['%s'] * len(cols_db))});
|
||||
"""
|
||||
curs.executemany(sql, vals)
|
||||
conn.commit()
|
||||
Loading…
Reference in New Issue