erste version

master
Hofei90 5 years ago
parent 9efb14938b
commit af131b89c3

3
.gitmodules vendored

@ -0,0 +1,3 @@
[submodule "sshtunnel"]
path = sshtunnel
url = https://github.com/Hofei90/sshtunnel.git

@ -1,26 +1,44 @@
import peewee import peewee
database = peewee.Proxy() DATABASE = peewee.Proxy()
class BaseModel(peewee.Model): class BaseModel(peewee.Model):
class Meta: class Meta:
database = database database = DATABASE
class CoDUser(BaseModel): class CoDUser(BaseModel):
user = peewee.TextField() user = peewee.TextField()
class CoDStats(BaseModel): class CoDStandardStats(BaseModel):
ts = peewee.DateTimeField() ts = peewee.DateTimeField()
typ = peewee.TextField() user = peewee.TextField()
value = peewee.FloatField() 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: class Meta:
primary_key = peewee.CompositeKey('ts', 'typ') primary_key = peewee.CompositeKey("ts", "user")
def create_tables(): def create_tables():
database.create_tables([TestPeewee]) DATABASE.create_tables([CoDUser, CoDStandardStats])
def create_hypertable():
DATABASE.execute_sql("SELECT create_hypertable('CoDStandardStats', 'ts');")

@ -9,29 +9,82 @@ import requests
import json import json
import toml import toml
import pprint 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"

@ -1,3 +1,4 @@
peewee~=3.13.3 peewee~=3.13.3
requests~=2.23.0 requests~=2.23.0
toml~=0.10.0 toml~=0.10.0
paramiko

@ -0,0 +1 @@
Subproject commit c6532d0fd8027ca2ffcf1a90a02b6dd47837170a
Loading…
Cancel
Save