diff --git a/src/extract.py b/src/extract.py index cf749e2..777910b 100644 --- a/src/extract.py +++ b/src/extract.py @@ -2,77 +2,162 @@ import psycopg2 as pg from os import environ as env from typing import TypeVar -T = TypeVar('T') -U = TypeVar('U') +T = TypeVar("T") +U = TypeVar("U") 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")) + 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 extract_cols(data: dict, cols: list[str]) -> dict: return {key: data.get(key) for key in cols} -def extract_cols(data: dict, cols: list[str]) -> dict: - return {key: data.get(key) for key in cols} def score_to_text(score: list[int]) -> str: - return "-".join(map(str,score)) + return "-".join(map(str, score)) + # maybe could get columns form db def cols_from_player_stats(player_stats: dict) -> dict: - cols = ["id", "gameId", "gameFinishedAt", "steam64Id", "name", "preaim", - "reactionTime", "accuracy", "accuracyEnemySpotted", - "accuracyHead", "shotsFiredEnemySpotted", "shotsFired", - "shotsHitEnemySpotted", "shotsHitFriend", "shotsHitFriendHead", - "shotsHitFoe", "shotsHitFoeHead", "utilityOnDeathAvg", - "heFoesDamageAvg", "heFriendsDamageAvg", "heThrown", - "molotovThrown", "smokeThrown", "smokeThrownCT", - "smokeThrownCTGood", "smokeThrownCTGoodRatio", - "smokeThrownCTFoes", "counterStrafingShotsAll", - "counterStrafingShotsBad", "counterStrafingShotsGood", - "counterStrafingShotsGoodRatio", "flashbangHitFoe", - "flashbangLeadingToKill", "flashbangHitFoeAvgDuration", - "flashbangHitFriend", "flashbangThrown", "flashAssist", "score", - "initialTeamNumber", "mvps", "ctRoundsWon", "ctRoundsLost", - "tRoundsWon", "tRoundsLost", "sprayAccuracy", - "molotovFoesDamageAvg", "molotovFriendsDamageAvg", "color", - "totalKills", "totalDeaths", "kdRatio", "multi2k", "multi3k", - "multi4k", "multi5k", "hltvRating", "hsp", "roundsSurvived", - "roundsSurvivedPercentage", "dpr", "totalAssists", "totalDamage", - "tradeKillOpportunities", "tradeKillAttempts", - "tradeKillsSucceeded", "tradeKillAttemptsPercentage", - "tradeKillsSuccessPercentage", "tradeKillOpportunitiesPerRound", - "tradedDeathOpportunities", "tradedDeathAttempts", - "tradedDeathAttemptsPercentage", "tradedDeathsSucceeded", - "tradedDeathsSuccessPercentage", - "tradedDeathsOpportunitiesPerRound", "leetifyRating", - "personalPerformanceRating", "ctLeetifyRating", "tLeetifyRating", - "leetifyUserId", "isCollector", "isProPlan", "isLeetifyStaff"] + cols = [ + "id", + "gameId", + "gameFinishedAt", + "steam64Id", + "name", + "preaim", + "reactionTime", + "accuracy", + "accuracyEnemySpotted", + "accuracyHead", + "shotsFiredEnemySpotted", + "shotsFired", + "shotsHitEnemySpotted", + "shotsHitFriend", + "shotsHitFriendHead", + "shotsHitFoe", + "shotsHitFoeHead", + "utilityOnDeathAvg", + "heFoesDamageAvg", + "heFriendsDamageAvg", + "heThrown", + "molotovThrown", + "smokeThrown", + "smokeThrownCT", + "smokeThrownCTGood", + "smokeThrownCTGoodRatio", + "smokeThrownCTFoes", + "counterStrafingShotsAll", + "counterStrafingShotsBad", + "counterStrafingShotsGood", + "counterStrafingShotsGoodRatio", + "flashbangHitFoe", + "flashbangLeadingToKill", + "flashbangHitFoeAvgDuration", + "flashbangHitFriend", + "flashbangThrown", + "flashAssist", + "score", + "initialTeamNumber", + "mvps", + "ctRoundsWon", + "ctRoundsLost", + "tRoundsWon", + "tRoundsLost", + "sprayAccuracy", + "molotovFoesDamageAvg", + "molotovFriendsDamageAvg", + "color", + "totalKills", + "totalDeaths", + "kdRatio", + "multi2k", + "multi3k", + "multi4k", + "multi5k", + "hltvRating", + "hsp", + "roundsSurvived", + "roundsSurvivedPercentage", + "dpr", + "totalAssists", + "totalDamage", + "tradeKillOpportunities", + "tradeKillAttempts", + "tradeKillsSucceeded", + "tradeKillAttemptsPercentage", + "tradeKillsSuccessPercentage", + "tradeKillOpportunitiesPerRound", + "tradedDeathOpportunities", + "tradedDeathAttempts", + "tradedDeathAttemptsPercentage", + "tradedDeathsSucceeded", + "tradedDeathsSuccessPercentage", + "tradedDeathsOpportunitiesPerRound", + "leetifyRating", + "personalPerformanceRating", + "ctLeetifyRating", + "tLeetifyRating", + "leetifyUserId", + "isCollector", + "isProPlan", + "isLeetifyStaff", + ] return extract_cols(player_stats, cols) + def cols_from_profile_game(game: dict) -> dict: - cols = ["ctLeetifyRating", "ctLeetifyRatingRounds", "dataSource", "elo", - "gameFinishedAt", "gameId", "isCs2", "mapName", "matchResult", - "scores", "skillLevel", "tLeetifyRating", "tLeetifyRatingRounds", - "deaths", "hasBannedPlayer", "kills", "partySize"] + cols = [ + "ctLeetifyRating", + "ctLeetifyRatingRounds", + "dataSource", + "elo", + "gameFinishedAt", + "gameId", + "isCs2", + "mapName", + "matchResult", + "scores", + "skillLevel", + "tLeetifyRating", + "tLeetifyRatingRounds", + "deaths", + "hasBannedPlayer", + "kills", + "partySize", + ] return extract_cols(game, cols) + def meta_from_profile(profile: dict) -> dict: - meta = profile.get("meta"); + meta = profile.get("meta") if not meta: raise Exception("Could not get profile metadata", profile) - cols = [ "steam64Id", "isCollector", "isLeetifyStaff", - "isProPlan", "leetifyUserId", "faceitNickname"] + cols = [ + "steam64Id", + "isCollector", + "isLeetifyStaff", + "isProPlan", + "leetifyUserId", + "faceitNickname", + ] return extract_cols(meta, cols) -def insert_value(data: dict[T,U], key: T, value: U) -> dict[T,U]: + +def insert_value(data: dict[T, U], key: T, value: U) -> dict[T, U]: data[key] = value return data + def convert_game_scores(game: dict) -> dict: score = game.get("scores") if not score: @@ -83,8 +168,6 @@ def convert_game_scores(game: dict) -> dict: return game - - def games_from_profile(profile: dict) -> list: games = profile.get("games") if not games: @@ -102,4 +185,3 @@ def games_from_profile(profile: dict) -> list: games = map(lambda game: insert_value(game, "leetifyUserId", id), games) games = map(convert_game_scores, games) return list(games) - diff --git a/src/leetify/core.py b/src/leetify/core.py index a797feb..29420de 100644 --- a/src/leetify/core.py +++ b/src/leetify/core.py @@ -1,5 +1,6 @@ import requests + class Leetify: api_base_url = "https://api.leetify.com/api" profile_base_url = f"{api_base_url}/profile" @@ -16,5 +17,3 @@ class Leetify: def get_match(self, id: str) -> dict: url = f"{self.match_base_url}/{id}" return self.__get_page(url) - -