def index_character(character, account): try: logging.info('Synching: Character {0} / {1}'.format(character.name, character.char_id)) item_stats = ItemStats.query(ItemStats.character_key == character.key).fetch_async() orders = MarketOrder.query(MarketOrder.character_key == character.key).fetch_async() api = Api(account.api_id, account.api_vcode) api.authenticate() api_char = api.get_character(character.char_id) row_count = 250 all_items, all_stations = {}, {} character.last_update = datetime.now() last_transaction_id = character.last_transaction_id last_transaction_date = character.last_transaction_date api_wallet_transactions = api_char.WalletTransactions(rowCount=(last_transaction_id is None and 1000 or row_count)) item_stats = dict([(i.type_id, i) for i in item_stats.get_result()]) newest_transaction, oldest_transaction, items, stations = sync_transactions(character, api_wallet_transactions, last_transaction_id, last_transaction_date, item_stats) all_items.update(items or {}) all_stations.update(stations or {}) while last_transaction_id and last_transaction_date and oldest_transaction and \ (datetime.fromtimestamp(oldest_transaction.transactionDateTime) > last_transaction_date or oldest_transaction.transactionID > last_transaction_id): logging.info('Fetching another batch from id {0}'.format(oldest_transaction.transactionID)) api_wallet_transactions = api_char.WalletTransactions(rowCount=row_count, fromID=oldest_transaction.transactionID) newest_transaction, oldest_transaction, items, stations = sync_transactions(character, api_wallet_transactions, last_transaction_id, last_transaction_date, item_stats) all_items.update(items or {}) all_stations.update(stations or {}) sync_orders(character, api_char.MarketOrders(), orders.get_result()) character.put_async() logging.info('Syncing done: Character {0} / {1}'.format(character.name, character.char_id)) return all_items, all_stations except: import traceback logging.error('Error while syncing character {0} / {1}'.format(character.name, character.char_id)) logging.error(traceback.format_exc()) return None
def post(self): api_vcode = self.request.POST.get('api_vcode', None) api_id = self.request.POST.get('api_id', None) if not (api_id and api_vcode) or not(len(api_id) == 6 and len(api_vcode) == 64): env = dict(values=dict(api_id=api_id, api_vcode=api_vcode), errors=dict(api_id=(not api_id or len(api_id) != 6), api_vcode=(not api_vcode or len(api_vcode) != 64))) self.session.add_flash(env, key='env_flash') else: if Account.query().filter(Account.api_id == api_id).count(): self.session.add_flash('This API key has already been added to this profile', key='error_messages') else: api = Api(api_id, api_vcode) api.authenticate() if api.is_authenticated(): accounts_chars = [] for api_char in api.characters: if not filter(lambda c: api_char.charactedID == c.char_id, self.userprofile.characters): accounts_chars.append(Character(user=users.get_current_user(), name=api_char.name, char_id=str(api_char.characterID))) account = Account( user=users.get_current_user(), api_vcode=api_vcode, api_id=api_id, key_name=self.request.POST.get('name', None) ) account.put() for char in accounts_chars: char.account_key = account.key put_multi_async(accounts_chars) #self.userprofile.characters = (self.userprofile.characters or []) + accounts_chars #self.userprofile.put_async() self.session.add_flash('Key added successfully', key='messages') self.redirect('/profile')