From af131b89c3e0c5495b2d6009f2bcce664a8a5977 Mon Sep 17 00:00:00 2001 From: Hofei90 <29521028+Hofei90@users.noreply.github.com> Date: Tue, 5 May 2020 11:45:46 +0200 Subject: [PATCH] erste version --- .gitmodules | 3 ++ db_model.py | 32 +++++++++++---- get_cod_stats.py | 101 ++++++++++++++++++++++++++++++++++++----------- requirements.txt | 3 +- sshtunnel | 1 + 5 files changed, 108 insertions(+), 32 deletions(-) create mode 100644 .gitmodules create mode 160000 sshtunnel diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..177a0d4 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "sshtunnel"] + path = sshtunnel + url = https://github.com/Hofei90/sshtunnel.git diff --git a/db_model.py b/db_model.py index a1cbd4e..fc14d0a 100644 --- a/db_model.py +++ b/db_model.py @@ -1,26 +1,44 @@ import peewee -database = peewee.Proxy() +DATABASE = peewee.Proxy() class BaseModel(peewee.Model): class Meta: - database = database + database = DATABASE class CoDUser(BaseModel): user = peewee.TextField() -class CoDStats(BaseModel): +class CoDStandardStats(BaseModel): ts = peewee.DateTimeField() - typ = peewee.TextField() - value = peewee.FloatField() + user = peewee.TextField() + kills = peewee.IntegerField() + deaths = peewee.IntegerField() + assists = peewee.IntegerField() + suicides = peewee.IntegerField() + total_games_played = peewee.IntegerField() + wins = peewee.IntegerField() + losses = peewee.IntegerField() + ties = peewee.IntegerField() + time_played_total = peewee.IntegerField() + average_life = peewee.FloatField() + career_score = peewee.IntegerField() + headshots = peewee.IntegerField() + total_shots = peewee.IntegerField() + hits = peewee.IntegerField() + misses = peewee.IntegerField() class Meta: - primary_key = peewee.CompositeKey('ts', 'typ') + primary_key = peewee.CompositeKey("ts", "user") def create_tables(): - database.create_tables([TestPeewee]) + DATABASE.create_tables([CoDUser, CoDStandardStats]) + + +def create_hypertable(): + DATABASE.execute_sql("SELECT create_hypertable('CoDStandardStats', 'ts');") diff --git a/get_cod_stats.py b/get_cod_stats.py index c4f9e52..f63b920 100644 --- a/get_cod_stats.py +++ b/get_cod_stats.py @@ -9,29 +9,82 @@ import requests import json import toml import pprint +import db_model as db +import datetime +from peewee import PostgresqlDatabase, DatabaseError +import os +from sshtunnel.sshtunnel import SSHTunnelForwarder -URL = "https://api.tracker.gg/api/v2/modern-warfare/standard/profile/battlenet/Hofei%232237" -r = requests.get(URL) -data = json.loads(r.text) - -data["userInfo"]["userId"] #userid int -stats = data["segments"][0]["stats"] #Schreibweise verkürzen -stats["kills"]["value"] # Int -stats["deaths"]["value"] # Int -stats["assists"]["value"] # Int -stats["suicides"]["value"] # Int -stats["totalGamesPlayed"]["value"] # Int -stats["wins"]["value"] # Int -stats["losses"]["value"] # Int -stats["ties"]["value"] # Int -stats["timePlayedTotal"]["value"] # int -stats["averageLife"]["value"] # float seconds -stats["careerScore"]["value"] # int -stats["headshots"]["value"] # int -stats["totalShots"]["value"] # int -stats["hits"]["value"] # int -stats["misses"]["value"] # float seconds - - -#pprint.pprint(data) +def load_toml(tomlfile): + with open(tomlfile, encoding="utf-8") as file: + inhalt = toml.loads(file.read()) + return inhalt + + +SKRIPTPFAD = os.path.abspath(os.path.dirname(__file__)) +CONFIG = load_toml(os.path.join(SKRIPTPFAD, "config.toml")) +STANDARD_STATS_URL = "https://api.tracker.gg/api/v2/modern-warfare/standard/profile/battlenet/{}" + + +def get_json_data(url): + r = requests.get(url) + return json.loads(r.text) + + +def check_user(user): + user_db, _ = db.CoDUser.get_or_create( + user=user + ) + + +def get_standard_statistik(url, user): + url = url.format(user) + data = get_json_data(url)["data"] + check_user(user) + + stats = data["segments"][0]["stats"] + db.CoDStandardStats.create( + ts=datetime.datetime.utcnow(), + user=user, + kills=stats["kills"]["value"], + deaths=stats["deaths"]["value"], + assists=stats["assists"]["value"], + suicides=stats["suicides"]["value"], + total_games_played=stats["totalGamesPlayed"]["value"], + wins=stats["wins"]["value"], + losses=stats["losses"]["value"], + ties=stats["ties"]["value"], + time_played_total=stats["timePlayedTotal"]["value"], + average_life=stats["averageLife"]["value"], + career_score=stats["careerScore"]["value"], + headshots=stats["headshots"]["value"], + total_shots=stats["totalShots"]["value"], + hits=stats["hits"]["value"], + misses=stats["misses"]["value"] + ) + + +def main(): + + with SSHTunnelForwarder( + (CONFIG["ssh"]["ip_server"], CONFIG["ssh"]["ssh_port"]), ssh_username=CONFIG["ssh"]["user"], + ssh_password=CONFIG["ssh"]["pw"], remote_bind_address=('127.0.0.1', CONFIG["pg"]["pgport"])) as server: + + db.DATABASE.initialize(PostgresqlDatabase(CONFIG["pg"]["pgdb"], + user=CONFIG["pg"]["pguser"], password=CONFIG["pg"]["pgpw"], + host="127.0.0.1", + port=server.local_bind_port)) + db.create_tables() + try: + db.create_hypertable() + except DatabaseError: + db.DATABASE.rollback() + for user in CONFIG["users"]: + get_standard_statistik(STANDARD_STATS_URL, user) + + +if __name__ == "__main__": + main() + +"https://api.tracker.gg/api/v2/modern-warfare/standard/profile/battlenet/DaZimmi85%232712" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 8fda0d7..4843a0f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ peewee~=3.13.3 requests~=2.23.0 -toml~=0.10.0 \ No newline at end of file +toml~=0.10.0 +paramiko \ No newline at end of file diff --git a/sshtunnel b/sshtunnel new file mode 160000 index 0000000..c6532d0 --- /dev/null +++ b/sshtunnel @@ -0,0 +1 @@ +Subproject commit c6532d0fd8027ca2ffcf1a90a02b6dd47837170a