81 lines
2.9 KiB
Python
81 lines
2.9 KiB
Python
#!/usr/bin/env python
|
|
# coding: utf-8
|
|
|
|
from flask.ext.script import Manager
|
|
from flask.ext.migrate import Migrate, MigrateCommand
|
|
|
|
from wots import app, db
|
|
|
|
migrate = Migrate(app, db)
|
|
|
|
# Инициализируем менеджер
|
|
manager = Manager(app)
|
|
# Регистрируем команду, реализованную в виде потомка класса Command
|
|
manager.add_command('db', MigrateCommand)
|
|
|
|
@manager.command
|
|
def wot_harvest_accounts():
|
|
"""Creates the admin user."""
|
|
import requests
|
|
from datetime import datetime
|
|
from sqlalchemy import func
|
|
from wotstats.models.wotaccounts import WotAccounts
|
|
from wotstats.models.wotaccountsstats import WotAccountsStats
|
|
|
|
app_id = app.config['WG_APPLICATION_ID']
|
|
url = 'https://api.worldoftanks.ru/wot/account/info/'
|
|
# Default account_id
|
|
account_id = 35
|
|
|
|
# TODO: get max account_id from wot_accounts
|
|
_account_id = db.session.query(func.max(WotAccounts.account_id)).scalar()
|
|
if _account_id:
|
|
account_id = _account_id
|
|
|
|
for i in db.session.query(WotAccountsStats.id, WotAccountsStats.last_battle_time, WotAccountsStats.data).filter(WotAccountsStats.last_battle_time == None):
|
|
print i.id
|
|
x = db.session.query(WotAccountsStats).filter_by(id=i.id).first()
|
|
|
|
# x = WotAccountsStats.query.filter_by(id=i.id)
|
|
# print x.account_id
|
|
x.last_battle_time = datetime.fromtimestamp(int(i.data.get('last_battle_time'))).strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
# i.last_battle_time = datetime.fromtimestamp(int(i.data.get('last_battle_time'))).strftime('%Y-%m-%d %H:%M:%S')
|
|
db.session.commit()
|
|
# db.session.flush()
|
|
# return
|
|
|
|
while True:
|
|
account_id += 1
|
|
__ = requests.get('{}?application_id={}&account_id={}'.format(url, app_id, account_id)).json()
|
|
if not __.get('data', {}).get("{}".format(account_id)):
|
|
print('account_id: {} SKIPPED'.format(account_id))
|
|
continue
|
|
# copy results
|
|
account_statistics = __.get('data', {}).get("{}".format(account_id)).get('statistics', {})
|
|
account_data = __['data']["{}".format(account_id)]
|
|
account_data.pop('statistics', None)
|
|
|
|
db.session.add(WotAccounts(account_id=account_id, nickname=account_data.get('nickname')))
|
|
db.session.commit()
|
|
db.session.flush()
|
|
|
|
ws = WotAccountsStats()
|
|
ws.account_id = account_id
|
|
ws.created_at = datetime.now()
|
|
try:
|
|
ws.last_battle_time = datetime.fromtimestamp(int(account_data.get('last_battle_time'))).strftime('%Y-%m-%d %H:%M:%S')
|
|
except Exception as e:
|
|
print('>> Error: {}'.format(e))
|
|
ws.data = account_data
|
|
ws.statistics = account_statistics
|
|
db.session.add(ws)
|
|
db.session.commit()
|
|
db.session.flush()
|
|
|
|
print('account_id: {} nickname: {} OK'.format(account_id, account_data.get('nickname')))
|
|
|
|
# break
|
|
|
|
if __name__ == '__main__':
|
|
manager.run()
|