You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
4.7 KiB
Python
137 lines
4.7 KiB
Python
"""
|
|
Links:
|
|
https://api.tracker.gg/api/v2/modern-warfare/standard/profile/battlenet/Hofei%232237
|
|
https://api.tracker.gg/api/v2/modern-warfare/standard/profile/battlenet/Hofei%232237/segments/killstreak?
|
|
https://api.tracker.gg/api/v2/modern-warfare/standard/profile/battlenet/Hofei%232237/segments/weapon?
|
|
"""
|
|
|
|
import requests
|
|
import json
|
|
import toml
|
|
import db_model as db
|
|
import datetime
|
|
from peewee import PostgresqlDatabase, DatabaseError
|
|
import os
|
|
from sshtunnel.sshtunnel import SSHTunnelForwarder
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
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, headers={"User-Agent": "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:82.0) "
|
|
"Gecko/20100101 Firefox/82.0"})
|
|
return json.loads(r.text)
|
|
|
|
|
|
def check_user(user):
|
|
user_db, _ = db.CoDUser.get_or_create(
|
|
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):
|
|
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 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(
|
|
(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_hypertables()
|
|
except DatabaseError:
|
|
db.DATABASE.rollback()
|
|
for user in CONFIG["users"]:
|
|
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__":
|
|
main()
|