add HC Domination Stats

master
Hofei90 5 years ago
parent af131b89c3
commit cb4ada96f7

1
.gitignore vendored

@ -94,3 +94,4 @@ ENV/
# Rope project settings # Rope project settings
.ropeproject .ropeproject
config.toml

@ -2,6 +2,7 @@ import peewee
DATABASE = peewee.Proxy() DATABASE = peewee.Proxy()
HYPERTABLES = ["CoDStandardStats", "CoDHcDominationStats"]
class BaseModel(peewee.Model): class BaseModel(peewee.Model):
@ -36,9 +37,24 @@ class CoDStandardStats(BaseModel):
primary_key = peewee.CompositeKey("ts", "user") primary_key = peewee.CompositeKey("ts", "user")
class CoDHcDominationStats(BaseModel):
ts = peewee.DateTimeField()
user = peewee.TextField()
kills = peewee.IntegerField()
deaths = peewee.IntegerField()
score = peewee.IntegerField()
time_played = peewee.IntegerField()
captures = peewee.IntegerField()
defends = peewee.IntegerField()
class Meta:
primary_key = peewee.CompositeKey("ts", "user")
def create_tables(): def create_tables():
DATABASE.create_tables([CoDUser, CoDStandardStats]) DATABASE.create_tables([CoDUser, CoDStandardStats, CoDHcDominationStats])
def create_hypertable(): def create_hypertables():
DATABASE.execute_sql("SELECT create_hypertable('CoDStandardStats', 'ts');") for hypertable in HYPERTABLES:
DATABASE.execute_sql(f"SELECT create_hypertable('{hypertable}', 'ts');")

@ -8,12 +8,12 @@ https://api.tracker.gg/api/v2/modern-warfare/standard/profile/battlenet/Hofei%23
import requests import requests
import json import json
import toml import toml
import pprint
import db_model as db import db_model as db
import datetime import datetime
from peewee import PostgresqlDatabase, DatabaseError from peewee import PostgresqlDatabase, DatabaseError
import os import os
from sshtunnel.sshtunnel import SSHTunnelForwarder from sshtunnel.sshtunnel import SSHTunnelForwarder
from bs4 import BeautifulSoup
def load_toml(tomlfile): def load_toml(tomlfile):
@ -38,6 +38,43 @@ def check_user(user):
) )
def get_game_mode_stats_raw(user):
url = "https://cod.tracker.gg/modern-warfare/profile/battlenet/{}/mp/modes".format(user)
source = requests.get(url).text
soup = BeautifulSoup(source, features="html5lib")
scripts = [script.string for script in soup.find_all("script") if script.string]
initial_state_script = None
for script in scripts:
if "__INITIAL_STATE__" in script:
initial_state_script = script
break
if not initial_state_script:
print("Script not found!")
return None
# NB: This can break easily!
initial_state_script = initial_state_script.replace("window.__INITIAL_STATE__=", "")
initial_state_script = initial_state_script.replace(
";(function(){var s;(s=document.currentScript||document.scripts[document.scripts.length-1])"
".parentNode.removeChild(s);}());",
"",
)
data = json.loads(initial_state_script)
return data
def get_hc_domination_stats(stats, user):
db.CoDHcDominationStats.create(
ts=datetime.datetime.utcnow(),
user=user,
kills=stats["kills"]["value"],
deaths=stats["deaths"]["value"],
score=stats["score"]["value"],
time_played=stats["timePlayed"]["value"],
captures=stats["captures"]["value"],
defends=stats["defends"]["value"]
)
def get_standard_statistik(url, user): def get_standard_statistik(url, user):
url = url.format(user) url = url.format(user)
data = get_json_data(url)["data"] data = get_json_data(url)["data"]
@ -65,8 +102,14 @@ def get_standard_statistik(url, user):
) )
def main(): def game_mode_handler(data, user):
for _, value in data.items():
for datensatz in value:
if datensatz["attributes"]["key"] == "hc_dom":
get_hc_domination_stats(datensatz["stats"], user)
def main():
with SSHTunnelForwarder( with SSHTunnelForwarder(
(CONFIG["ssh"]["ip_server"], CONFIG["ssh"]["ssh_port"]), ssh_username=CONFIG["ssh"]["user"], (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: ssh_password=CONFIG["ssh"]["pw"], remote_bind_address=('127.0.0.1', CONFIG["pg"]["pgport"])) as server:
@ -77,14 +120,16 @@ def main():
port=server.local_bind_port)) port=server.local_bind_port))
db.create_tables() db.create_tables()
try: try:
db.create_hypertable() db.create_hypertables()
except DatabaseError: except DatabaseError:
db.DATABASE.rollback() db.DATABASE.rollback()
for user in CONFIG["users"]: for user in CONFIG["users"]:
get_standard_statistik(STANDARD_STATS_URL, user) get_standard_statistik(STANDARD_STATS_URL, user)
data = get_game_mode_stats_raw(user)
if data is not None:
game_mode_handler(data['stats-v2']['segments'], user)
if __name__ == "__main__": if __name__ == "__main__":
main() main()
"https://api.tracker.gg/api/v2/modern-warfare/standard/profile/battlenet/DaZimmi85%232712"

@ -1,4 +1,6 @@
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 paramiko~=2.7.1
setuptools~=46.1.3
beautifulsoup4~=4.9.0
Loading…
Cancel
Save