from datetime import datetime, time import pytz from django.conf import settings import wargaming import logging import requests from requests.exceptions import RequestException from .util import memcached, log_time, memcache log = logging.getLogger(__name__) wot = wargaming.WoT(settings.WARGAMING_API, 'ru', 'ru') wgn = wargaming.WGN(settings.WARGAMING_API, 'ru', 'ru') def convert_dt(date): return datetime.strptime(date, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=pytz.UTC) def normalize_province_data(province): # convert battles_start_at from str to datetime province['battles_start_at'] = convert_dt(province['battles_start_at']) province['prime_time'] = time(*map(int, province['prime_time'].split(':'))) # convert str to datetime for active battles for battle in province['active_battles']: battle['start_at'] = convert_dt(battle['start_at'])
import requests from datetime import timedelta import datetime import math import wargaming from django.db.models.signals import pre_save from django.db.models import Q from django.contrib.postgres.fields import JSONField from django.dispatch import receiver from django.conf import settings from django.core.exceptions import ObjectDoesNotExist from django.utils.functional import cached_property wot = wargaming.WoT(settings.WARGAMING_KEY, language='ru', region='ru') wgn = wargaming.WGN(settings.WARGAMING_KEY, language='ru', region='ru') def utc_now(): return datetime.datetime.now(tz=pytz.UTC) def combine_dt(date, time): return datetime.datetime.combine(date, time) class TournamentInfo(dict): def __init__(self, province_id, seq=None, **kwargs): super(TournamentInfo, self).__init__(seq=None, **kwargs) # {u'applications_decreased': False,
class Command(BaseCommand): _appID = '3bab986f0bd5a381dfacf4ca9b639fa4' wgwot = wargaming.WoT(_appID, region='eu', language='en') wgwgn = wargaming.WGN(_appID, region='eu', language='en') anv_clanid = 500004323 pomlcky_clanid = 500054759 anv_v_clanid = 500075469 def add_arguments(self, parser): # use local files parser.add_argument( '--folder', default='wot/testdata', nargs='?', help= 'Use files from local directory instead downloading data from WG API', ) # json output parser.add_argument( '--json', default='wot_clean_data.json', # nargs=1, help='Save output to json', ) def handle(self, *args, **options): """ Main command function :param args: arguments :param options: optional arguments """ # clean all data and store it in dictionary clans = self.merge_and_clean(*args, **options) # dump the the result to json and quit if options['json']: filename = os.path.relpath(options['json']) with open(filename, 'w+') as outf: json.dump(clans, outf, indent=2, sort_keys=True, separators=(',', ': ')) rtn = "Saved to file {fn}".format(fn=filename) self.stdout.write(rtn) return # update the database with the records self.update_database(clans) def update_database(self, clans): """ Updates database entries :param clans: output from merge_and_clean """ for clan_id, clan_data in clans.items(): # create clans clan, created = Clan.objects.update_or_create( clan_id=clan_id, name=clan_data['name'], tag=clan_data['tag']) if created: log = 'Created clan {tag}, {cid}'.format(tag=clan.tag, cid=clan.clan_id) self.stdout(log) downloaded_member_ids = set(member['account_id'] for member in clan_data['members']) stored_member_ids = set(clan.get_members().values_list( 'account_id', flat=True)) newcomers = downloaded_member_ids - stored_member_ids left_clan = stored_member_ids - downloaded_member_ids self.remove_leavers(left_clan) # create / update player data for member_id, member_data in clan_data['members'].items(): player, created = Player.objects.update_or_create( account_id=member_data['account_id'], account_name=member_data['account_name'], clan=clan) if created: log = 'Created player {nick}, {aid}'.format( nick=player.account_name, aid=player.account_id) self.stdout(log) jcd = timezone.datetime.fromtimestamp( float(member_data['joined_clan_date'])) lbt = timezone.datetime.fromtimestamp( float(member_data['last_battle_time'])) player_data, created = PlayerData.objects. \ update_or_create(player=player, joined_clan_date=jcd, last_battle_time=lbt, role_in_clan=member_data['role'], battles_on_random=member_data['statistics']['random']['battles'], battles_all=member_data['statistics']['all']['battles'], battles_stronghold=member_data['stronghold']['stronghold_skirmish']['battles'], total_resources_earned=member_data['stronghold']['total_resources_earned'], # week_resources_earned=wk, ) if created: log = 'Created player data {nick}, {date}'.format( nick=player.account_name, date=player_data.created) self.stdout(log) def merge_and_clean(self, *args, **options): data = {} # keys # clans['clan_id'] # members, players ['account_id'] # return dict # {'clan_id': {'tag': 'TAG', # 'name': 'CLANNAME', # 'members': {'account_id': {'stronghold': {'stronghold_skirmish': {'battles': 2219}, # 'total_resources_earned': 32502, # 'week_resources_earned': 0}, # 'last_battle_time': 1483682632, # 'statistics': {'all': {'battles': 70507}, # 'random': {'battles': 67313} # }, # 'account_id': 'number', # 'account_name': 'NickNam3', # 'joined_at': 1447589992, # 'role': 'Private' # }, # 'player2': {}, # } # } # } # 1.st request if options['folder']: folder = os.path.relpath(options['folder']) with open(os.path.join(folder, 'wg_clans_response.json'), 'r') as icf: clans = json.load(icf, parse_constant=str, parse_int=str, parse_float=str) else: # response containing new members clans = self.clean_clans( [self.anv_v_clanid, self.anv_clanid, self.pomlcky_clanid], fields=['tag', 'name', 'members']) for clan_id, clan_data in clans.items(): data[clan_id] = { 'members': {}, 'tag': clan_data['tag'], 'name': clan_data['name'] } downloaded_member_ids = [ member['account_id'] for member in clan_data['members'] ] # 2.nd request n times clan if options['folder']: pl_filename = 'wg_{tag}_players_response.json'.format( tag=clan_data['tag']) with open(os.path.join(folder, pl_filename), 'r') as pif: players_info = json.load(pif, parse_constant=str, parse_int=str, parse_float=str) str_filename = 'wg_{tag}_stronghold_response.json'.format( tag=clan_data['tag']) with open(os.path.join(folder, str_filename), 'r') as sif: player_stronghold = json.load(sif, parse_constant=str, parse_int=str, parse_float=str) else: players_info = self.clean_players( account_id=downloaded_member_ids, fields=[ 'last_battle_time', 'statistics.all.battles', 'statistics.random.battles', ], extra=['statistics.random']) # 3.rd request n times clan player_stronghold = self.clean_player_stronghold( account_id=downloaded_member_ids, fields=[ 'stronghold_skirmish.battles', 'total_resources_earned', 'week_resources_earned', ]) # iterate through members and merge data from clan, stronghold and info for idx, member_id in enumerate(downloaded_member_ids): account_id = str(member_id) if player_stronghold[account_id]['stronghold_skirmish'] is None: # print(account_id) # print(player_stronghold[account_id]['stronghold_skirmish']) player_stronghold[account_id]['stronghold_skirmish'] = { "battles": 0 } # print(player_stronghold[account_id]['stronghold_skirmish']) player = { 'stronghold': player_stronghold[account_id], 'last_battle_time': players_info[account_id]['last_battle_time'], 'statistics': players_info[account_id]['statistics'], 'account_id': account_id, 'role': clan_data['members'][idx]['role_i18n'], 'joined_clan_date': clan_data['members'][idx]['joined_at'], 'account_name': clan_data['members'][idx]['account_name'] } data[clan_id]['members'][account_id] = player return data def clean_clans(self, clan_id_list, fields): """ Download clans, tags, names, members+details :param clan_id_list: list of clan_ids :return: dictionary containing the data """ default_clan = {'tag': 't', 'name': 'd', 'clan_id': 0} clans = self.wgwgn.clans.info(clan_id=clan_id_list, fields=fields) return clans if isinstance(clans, dict) else dict(clans) def clean_players(self, account_id, fields, extra): """ Return players data in dict :param account_id: list of account_id :return: dictionary containing the data """ players_info = self.wgwot.account.info(account_id=account_id, fields=fields, extra=extra).data return players_info if isinstance(players_info, dict) else dict(players_info) def clean_player_stronghold(self, account_id, fields): """ Return player stronghold data :param account_id: :param account_id: list of account_id :param fields: fields from wg api :return: dictionary containing the data """ player_stronghold = self.wgwot.stronghold.accountstats( account_id=account_id, fields=fields) return player_stronghold if isinstance( player_stronghold, dict) else dict(player_stronghold) def remove_leavers(self, leavers): players = Player.objects.filter(account_id__in=leavers) pass
def __init__(self, args): self._wot = wargaming.WoT(args.appid, region='na', language='en') player = self.wot.account.list(search=args.player) self._account_id = player[0]['account_id'] self._vehicles = Vehicles.Vehicles(self.wot, self.account_id) self.vehicles.current_vehicle_stats(6913)
from itertools import count import wargaming wot = wargaming.WoT('demo', region='ru', language='ru') def list_all_provinces(): """List provinces from all fronts using WG Public API""" # get fronts list fronts = wot.globalmap.fronts() # iterate through fronts for front in fronts: # provinces method return no more than 100 provinces per page, # adding pagination iteration for page_no in count(start=1): # provinces method require 2 parameters - front_id and page_no provinces = wot.globalmap.provinces(front_id=front['front_id'], page_no=page_no) # if no provinces on this page, then we got all provinces on the front if len(provinces) == 0: break # iterate through provinces list for province in provinces: print(province['province_name']) try:
import wargaming from datetime import datetime, timedelta # enable_parser enables timestamp fields from WG API converted to # datetime object in python. wot = wargaming.WoT('demo', language='ru', region='ru', enable_parser=True) wgn = wargaming.WGN('demo', language='ru', region='ru') def show_clan_activity(clan_tag): clan = wgn.clans.list(search=clan_tag)[0] clan_id = str(clan['clan_id']) clan_info = wgn.clans.info(clan_id=clan['clan_id'])[clan_id] members_ids = [i['account_id'] for i in clan_info['members']] accounts = wot.account.info(account_id=members_ids) now = datetime.now() stats = { i: filter(lambda p: p['last_battle_time'] > now - timedelta(days=i), accounts.values()) for i in [1, 7, 30, 180] } total_accounts = len(accounts) print("Players in clan '%s': %s" % (clan_tag, total_accounts)) for i in sorted(stats): print("Players played in last %s days: %s (%0.2f%%)" % (i, len(stats[i]), 100. * len(stats[i]) / total_accounts))
import os from flask import Flask, render_template_string, request, redirect, make_response import wargaming app = Flask(__name__) wot = wargaming.WoT(os.environ['WARGAMING_API'], language='ru', region='ru') HTML_TEMPLATE = """<!doctype html> <title>Wargaming AUTH</title> {% if error %} <p style="color: red">Error: {{ error }}</p> {% endif %} {% if nickname %} <h1>Hello {{ nickname }}!</h1> <p>access_token: {{ access_token }}</p> <p>nickname: {{ nickname }}</p> <p>account_id: {{ account_id }}</p> <p>expires_at: {{ expires_at }}</p> <a href="/prolongate">Prolongate session</a> <a href="/logout">Logout</a> {% else %} <h1><a href="/login">Please login</a></h1> {% endif %} """ @app.route("/") def index(): return render_template_string(HTML_TEMPLATE, **request.cookies)