Beispiel #1
0
def bot_history(username):
    tz = pytz.timezone('America/Los_Angeles')
    bots = mongo.db.bots
    bot = bots.find_one({'username': username}, {'history': 1})
    if not bot:
        return {'error': 'could not find bot %s' % username}

    def hash_history(trade):
        h = hashlib.sha256()
        for item in trade.get('sent', []) + trade.get('received', []):
            h.update(item['name'].encode('utf8'))
            h.update(item['app_id'])
            h.update(item['context_id'])
        return h.hexdigest()

    if request.json:
        mongo.db.bots.update(
            {'_id': bot['_id']},
            {'$set': {'times.history': time.time()}},
        )

        history = request.json['history']
        for trade in history:
            steamid = util.steam.url_to_steamid(trade['friend']['url'])
            trade['friend'] = trade['friend']['name']
            trade['steamid'] = steamid

            t = trade['time']
            now = datetime.now(tz)
            when = datetime(year=now.year, tzinfo=tz, **t)
            # handle new year
            if when.month > 2 and now.month == 1:
                when = when.replace(year=now.year - 1)
            trade['time'] = when
            trade['bot_id'] = bot['_id']
            trade['hash'] = hash_history(trade)
            trade['count'] = 1

            old = mongo.db.history.find_and_modify({
                'steamid': trade['steamid'],
                'bot_id': trade['bot_id'],
                'time': trade['time'],
                'hash': trade['hash'],
            }, {'$inc': {'count': 1}})
            if old:
                user_id = mongo.db.users.find_one({'profile.steamid': old['steamid']}, {'_id': 1})
                if old['invalid'] and not old.get('trade_id') and old['count'] >= 2 and user_id:
                    mongo.db.users.update(
                        {'_id': user_id, 'ban': {'$exists': False}},
                        {'$set': {'ban': {
                            'reason': (
                                "Your account has been flagged for manual review because Steam inventory history "
                                "did not match your recent trades. You probably didn't do anything wrong, we just need "
                                "to look to make sure you don't lose anything. You can email us at [email protected] "
                                "if you have any questions."
                            ),
                            'history_id': old['_id'],
                        }}}
                    )
                    email_admins('History ban', 'user_id: {}, history_id: {}'.format(user_id, old['_id']))
            else:
                mongo.db.history.insert(trade, manipulate=True)
                if trade.get('sent') or trade.get('received'):
                    history_update(trade)

    return {}