def show_config(args, db):
    if args.all:
        for conf in get_all_from_db(db, 'config'):
            print conf
    elif args.username is not None:
        conf = get_from_db(db, 'config', args.username)
        print conf
def show_buy(args, db):
    if args.all:
        for buy in get_all_from_db(db, 'buy'):
            print buy
    elif args.id is not None:
        buy = get_from_db(db, 'buy', args.id)
        print buy
Example #3
0
def doBids(data, buy, waiter):
    with log_and_ignored(Exception):
        buy_u = {} 
        db = data['db']
        username = data['username']
        config = get_from_db(db, 'config', username)
        client = data['client']
        for bid in get_all_from_db(db, 'bid'):
            if bid['status'] == bid_stats['NOTBIDDED'] and bid['buy_id'] == buy['id']:
                bid_u = {}
                serverNow = nowToEpoch() + float(config['shiftTime'])
                session = config['sessionHandlePart']
                logging.debug(bid)
                logging.debug("calculated time: %s" % (datetime.fromtimestamp(serverNow)))

                whenShot = bid['whenShot']
                whenShot = float(whenShot)
                whenShot = round(whenShot)
                
                price = bid['price']
                price = float(price)
                quantity = bid['quantity']
                quantity = int(quantity)
                bid_id = bid['id']

                finishTime = bid.get('finishTime', None)
                if finishTime is None:
                    continue

                finishTime = int(finishTime)
                if serverNow > finishTime + whenShot and serverNow < finishTime + 5:
                    buy_u['status'] = buy_stats['BIDDED']
                    bid_u['status'] = bid_stats['BIDDED']
                    try:
                        logging.info("bidding %s" % (bid_id,))
                        client.service.doBidItem(sessionHandle=session,
                                                 bidItId=bid_id,
                                                 bidUserPrice=price,
                                                 bidQuantity=quantity)
                        ret = checkWinning(data, bid)
                        logging.info('doBids ret(%s) %s' % (ret, bid))
                        if ret == bid_stats['NOTWON']:
                            buy_u['status'] = buy_stats['NOTDONE']
                            bid_u['status'] = bid_stats['FINISHED']
                        if ret == bid_stats['WON']:
                            buy_u['status'] = buy_stats['DONE']
                            bid_u['status'] = bid_stats['FINISHED']
                            update_in_db(db, 'bid', bid_u, bid_id)
                            break
                        if ret == bid_stats['WINNING']:
                            bid_u['status'] = bid_stats['BIDDED']
                            update_in_db(db, 'bid', bid_u, bid_id)
                            break
                    except Exception, e:
                        logging.error("error %s" % (e.message.encode('utf-8')))
                update_in_db(db, 'bid', bid_u, bid_id)
        update_in_db(db, 'buy', buy_u, buy['id'])
        waiter.set()
def show_bid(args, db):
    if 'all' in args and args.all:
        for bid in get_all_from_db(db, 'bid'):
            if 'finishTime' in bid:
                bid['finishTime'] = datetime.fromtimestamp(float(bid['finishTime']))
                bid['finishTime'] = str(bid['finishTime'])
            print bid
    elif 'id' in args and args.id is not None:
        bid = get_from_db(db, 'bid', args.id)
        if 'finishTime' in bid:
            bid['finishTime'] = datetime.fromtimestamp(float(bid['finishTime']))
            bid['finishTime'] = str(bid['finishTime'])
        print bid

    elif 'buy_id' in args and args.buy_id is not None:
        for bid in get_all_from_db(db, 'bid'):
            if bid['buy_id'] == str(args.buy_id):
                if 'finishTime' in bid:
                    bid['finishTime'] = datetime.fromtimestamp(float(bid['finishTime']))
                    bid['finishTime'] = str(bid['finishTime'])
                print bid
Example #5
0
def checkBids(data, buy, waiter):
    db = data['db']
    buy_u = {}
    for bid in get_all_from_db(db, 'bid'):
        if bid['status'] == bid_stats['BIDDED'] and bid['buy_id'] == buy['id']:
            bid_u = {}
            bid_id = bid['id']
            try:
                ret = checkWinning(data, bid)
                logging.debug('checkBids ret(%s) %s' % (ret, bid))
                if ret == bid_stats['NOTWON']:
                    buy_u['status'] = buy_stats['NOTDONE']
                    bid_u['status'] = bid_stats['FINISHED']
                if ret == bid_stats['WON']:
                    buy_u['status'] = buy_stats['DONE']
                    bid_u['status'] = bid_stats['FINISHED']
                    update_in_db(db, 'bid', bid_u, bid_id)
                    break
            except Exception, e:
                logging.error("error %s" % (e.message.encode('utf-8')))
            update_in_db(db, 'bid', bid_u, bid_id)
Example #6
0
def runEverySecond(data):
    with log_and_ignored(Exception):
        logging.debug("resS")
        db = data['db']
        username = data['username']
        config = get_from_db(db, 'config', username)
        client = data['client']
        waiters = data['waiters']
        waiters_bids = data['waiters_bids']

        #logging.debug(client.service.doGetMyWonItems(sessionId=config['sessionHandlePart']))
        for buy in get_all_from_db(db, 'buy'):
            if config['status'] == config_stats['NOTINITIALIZED']:
                break

            if buy['status'] == buy_stats['NOTDONE']:
                buy_id = buy['id']
                logging.debug(buy)
                waiter = waiters.get(buy_id, None)
                if waiter is None or waiter.isSet():
                    logging.debug("we can go")
                    logging.debug(waiter)
                    waiter = threading.Event()
                    waiters[buy_id] = waiter
                    reactor.callInThread(doBids, data, buy, waiter)
                else:
                    logging.debug("we must wait")
            elif buy['status'] == buy_stats['BIDDED']:
                buy_id = buy['id']
                logging.debug(buy)
                waiter = waiters_bids.get(buy_id, None)
                if waiter is None or waiter.isSet():
                    logging.debug("we can go")
                    logging.debug(waiter)
                    waiter = threading.Event()
                    waiters_bids[buy_id] = waiter
                    reactor.callInThread(checkBids, data, buy, waiter)
                else:
                    logging.debug("we must wait")
            reactor.callInThread(checkInfo, data, buy)
Example #7
0
def checkInfo(data, buy):
    with log_and_ignored(Exception):
        db = data['db']
        username = data['username']
        config = get_from_db(db, 'config', username)
        client = data['client']
        for bid in get_all_from_db(db, 'bid'):
            if bid['buy_id'] == buy['id']:
                session = config['sessionHandlePart']
                whenShot = round(float(bid['whenShot']))
                if bid.get('buy_now', None) is None or bid.get('finishTime', None) is None or bid.get('name', None) is None or whenShot >= 0:
                    (finishTime, name, buy_now) = getInfos(bid['id'], config, client)
                    bid_u = {}
                    if finishTime is not None:
                        if whenShot >= 0:
                            serverNow = nowToEpoch() + float(config['shiftTime'])
                            bid_u['whenShot'] = round(serverNow - int(finishTime) + whenShot)
                        bid_u['finishTime'] = str(finishTime)
                    if name is not None:
                        bid_u['name'] = name
                    if buy_now is not None:
                        bid_u['buy_now'] = buy_now
                    update_in_db(db, 'bid', bid_u, bid['id'])
def del_config(args, db):
    if args.all:
        for conf in get_all_from_db(db, 'config'):
            del_from_db(db, 'config', conf['username'])
    elif args.username is not None:
        del_from_db(db, 'config', args.username)