formating

This commit is contained in:
Andrei Stoica 2024-01-27 00:18:42 -05:00
parent 793ef86202
commit 61ebfa776e
2 changed files with 132 additions and 51 deletions

View File

@ -2,77 +2,162 @@ import psycopg2 as pg
from os import environ as env from os import environ as env
from typing import TypeVar from typing import TypeVar
T = TypeVar('T') T = TypeVar("T")
U = TypeVar('U') U = TypeVar("U")
def connect(): def connect():
return pg.connect(dbname=env.get("POSTGRES_DB"), return pg.connect(
user=env.get("POSTGRES_USER"), dbname=env.get("POSTGRES_DB"),
password=env.get("POSTGRES_PASSWORD"), user=env.get("POSTGRES_USER"),
host=env.get("POSTGRES_HOST"), password=env.get("POSTGRES_PASSWORD"),
port=env.get("POSTGRES_PORT")) 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: def score_to_text(score: list[int]) -> str:
return "-".join(map(str,score)) return "-".join(map(str, score))
# maybe could get columns form db # maybe could get columns form db
def cols_from_player_stats(player_stats: dict) -> dict: def cols_from_player_stats(player_stats: dict) -> dict:
cols = ["id", "gameId", "gameFinishedAt", "steam64Id", "name", "preaim", cols = [
"reactionTime", "accuracy", "accuracyEnemySpotted", "id",
"accuracyHead", "shotsFiredEnemySpotted", "shotsFired", "gameId",
"shotsHitEnemySpotted", "shotsHitFriend", "shotsHitFriendHead", "gameFinishedAt",
"shotsHitFoe", "shotsHitFoeHead", "utilityOnDeathAvg", "steam64Id",
"heFoesDamageAvg", "heFriendsDamageAvg", "heThrown", "name",
"molotovThrown", "smokeThrown", "smokeThrownCT", "preaim",
"smokeThrownCTGood", "smokeThrownCTGoodRatio", "reactionTime",
"smokeThrownCTFoes", "counterStrafingShotsAll", "accuracy",
"counterStrafingShotsBad", "counterStrafingShotsGood", "accuracyEnemySpotted",
"counterStrafingShotsGoodRatio", "flashbangHitFoe", "accuracyHead",
"flashbangLeadingToKill", "flashbangHitFoeAvgDuration", "shotsFiredEnemySpotted",
"flashbangHitFriend", "flashbangThrown", "flashAssist", "score", "shotsFired",
"initialTeamNumber", "mvps", "ctRoundsWon", "ctRoundsLost", "shotsHitEnemySpotted",
"tRoundsWon", "tRoundsLost", "sprayAccuracy", "shotsHitFriend",
"molotovFoesDamageAvg", "molotovFriendsDamageAvg", "color", "shotsHitFriendHead",
"totalKills", "totalDeaths", "kdRatio", "multi2k", "multi3k", "shotsHitFoe",
"multi4k", "multi5k", "hltvRating", "hsp", "roundsSurvived", "shotsHitFoeHead",
"roundsSurvivedPercentage", "dpr", "totalAssists", "totalDamage", "utilityOnDeathAvg",
"tradeKillOpportunities", "tradeKillAttempts", "heFoesDamageAvg",
"tradeKillsSucceeded", "tradeKillAttemptsPercentage", "heFriendsDamageAvg",
"tradeKillsSuccessPercentage", "tradeKillOpportunitiesPerRound", "heThrown",
"tradedDeathOpportunities", "tradedDeathAttempts", "molotovThrown",
"tradedDeathAttemptsPercentage", "tradedDeathsSucceeded", "smokeThrown",
"tradedDeathsSuccessPercentage", "smokeThrownCT",
"tradedDeathsOpportunitiesPerRound", "leetifyRating", "smokeThrownCTGood",
"personalPerformanceRating", "ctLeetifyRating", "tLeetifyRating", "smokeThrownCTGoodRatio",
"leetifyUserId", "isCollector", "isProPlan", "isLeetifyStaff"] "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) return extract_cols(player_stats, cols)
def cols_from_profile_game(game: dict) -> dict: def cols_from_profile_game(game: dict) -> dict:
cols = ["ctLeetifyRating", "ctLeetifyRatingRounds", "dataSource", "elo", cols = [
"gameFinishedAt", "gameId", "isCs2", "mapName", "matchResult", "ctLeetifyRating",
"scores", "skillLevel", "tLeetifyRating", "tLeetifyRatingRounds", "ctLeetifyRatingRounds",
"deaths", "hasBannedPlayer", "kills", "partySize"] "dataSource",
"elo",
"gameFinishedAt",
"gameId",
"isCs2",
"mapName",
"matchResult",
"scores",
"skillLevel",
"tLeetifyRating",
"tLeetifyRatingRounds",
"deaths",
"hasBannedPlayer",
"kills",
"partySize",
]
return extract_cols(game, cols) return extract_cols(game, cols)
def meta_from_profile(profile: dict) -> dict: def meta_from_profile(profile: dict) -> dict:
meta = profile.get("meta"); meta = profile.get("meta")
if not meta: if not meta:
raise Exception("Could not get profile metadata", profile) raise Exception("Could not get profile metadata", profile)
cols = [ "steam64Id", "isCollector", "isLeetifyStaff", cols = [
"isProPlan", "leetifyUserId", "faceitNickname"] "steam64Id",
"isCollector",
"isLeetifyStaff",
"isProPlan",
"leetifyUserId",
"faceitNickname",
]
return extract_cols(meta, cols) 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 data[key] = value
return data return data
def convert_game_scores(game: dict) -> dict: def convert_game_scores(game: dict) -> dict:
score = game.get("scores") score = game.get("scores")
if not score: if not score:
@ -83,8 +168,6 @@ def convert_game_scores(game: dict) -> dict:
return game return game
def games_from_profile(profile: dict) -> list: def games_from_profile(profile: dict) -> list:
games = profile.get("games") games = profile.get("games")
if not 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(lambda game: insert_value(game, "leetifyUserId", id), games)
games = map(convert_game_scores, games) games = map(convert_game_scores, games)
return list(games) return list(games)

View File

@ -1,5 +1,6 @@
import requests import requests
class Leetify: class Leetify:
api_base_url = "https://api.leetify.com/api" api_base_url = "https://api.leetify.com/api"
profile_base_url = f"{api_base_url}/profile" profile_base_url = f"{api_base_url}/profile"
@ -16,5 +17,3 @@ class Leetify:
def get_match(self, id: str) -> dict: def get_match(self, id: str) -> dict:
url = f"{self.match_base_url}/{id}" url = f"{self.match_base_url}/{id}"
return self.__get_page(url) return self.__get_page(url)