Last active
December 17, 2015 09:48
-
-
Save MichaelMayorov/5589799 to your computer and use it in GitHub Desktop.
Script takes player's statistic from server and put it to sqlite3 db
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
import urllib | |
import json | |
import sqlite3 | |
import sys | |
if len(sys.argv) == 2: | |
DB_NAME = sys.argv[1] | |
else: | |
print sys.argv[0], "</path/to/db>" | |
sys.exit(1) | |
API_INFO = { | |
"player_id": 12178714, | |
"api_ver": "1.8", | |
"token": "Intellect_Soft-WoT_Mobile-unofficial_stats", | |
} | |
URL = "http://worldoftanks.ru/uc/accounts/%d/api/%s/?source_token=%s" % ( | |
API_INFO["player_id"], | |
API_INFO["api_ver"], | |
API_INFO["token"] | |
) | |
# DB_NAME = "playersdb.sqlite" | |
def update_needed(db_con, player_stat): | |
cur = db_con.cursor() | |
cur.execute("SELECT * FROM player_stat WHERE player_id=? and updated_at=?", | |
(API_INFO["player_id"], player_stat["updated_at"]) | |
) | |
res = cur.fetchone() | |
cur.close() | |
if res is not None: | |
return False | |
return True | |
def update_player_stat(db_con, stat): | |
args = ( | |
None, # for primary key | |
API_INFO["player_id"], | |
stat["updated_at"], | |
stat["battles"]["spotted"], | |
stat["battles"]["hits_percents"], | |
stat["battles"]["capture_points"], | |
stat["battles"]["damage_dealt"], | |
stat["battles"]["frags"], | |
stat["battles"]["dropped_capture_points"], | |
stat["summary"]["wins"], | |
stat["summary"]["losses"], | |
stat["summary"]["battles_count"], | |
stat["summary"]["survived_battles"], | |
stat["experience"]["xp"], | |
stat["experience"]["battle_avg_xp"], | |
stat["experience"]["max_xp"], | |
) | |
cur = db_con.cursor() | |
cur.execute("INSERT INTO player_stat VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", args) | |
cur.close() | |
return | |
def new_player(db_con, stat): | |
args = ( | |
API_INFO["player_id"], | |
stat["name"], | |
stat["created_at"], | |
) | |
cur = db_con.cursor() | |
cur.execute("INSERT INTO player VALUES (?,?,?)", args) | |
cur.close() | |
return | |
if __name__ == "__main__": | |
print "[!] Connecting to", DB_NAME | |
db_con = sqlite3.connect(DB_NAME) | |
db_con.execute("PRAGMA foreign_keys=ON") | |
print "[!] Getting statistic from server" | |
f = urllib.urlopen(URL) | |
response = json.loads(f.read()) | |
player_stat = response["data"] | |
if update_needed(db_con, player_stat) is True: | |
try: | |
print "[!] Updating player's statistic" | |
update_player_stat(db_con, player_stat) | |
except sqlite3.IntegrityError: # Player not exists | |
print "[!] Player not exists, creating..." | |
new_player(db_con, player_stat) | |
update_player_stat(db_con, player_stat) | |
db_con.commit() | |
else: | |
print "[!] Base is already up to date" | |
db_con.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment