def config(args, db): conf = get_from_db(db, 'config', args.username) if args.password: password = getpass.getpass() hashp256 = hashlib.sha256(password) base_pass = base64.encodestring(hashp256.digest()) conf['userHashPassword'] = base_pass update_dict(conf, args, 'username') update_dict(conf, args, 'countryCode') update_dict(conf, args, 'webapiKey') update_dict(conf, args, 'webapiUrl') if 'sessionHandlePart' not in conf: conf['sessionHandlePart'] = 'uu' if 'countryCode' not in conf: conf['countryCode'] = 1 if 'localVersion' not in conf: conf['localVersion'] = 1 if 'userHashPassword' not in conf: conf['userHashPassword'] = '******' if 'webapiKey' not in conf: conf['webapiKey'] = 'default' if 'webapiUrl' not in conf: conf['webapiUrl'] = 'https://webapi.allegro.pl/service.php?wsdl' update_in_db(db, 'config', conf, args.username)
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
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 add_buy(args, db): buy = get_from_db(db, 'buy', args.id) update_dict(buy, args, 'id') update_dict(buy, args, 'desc') update_dict(buy, args, 'status') if 'status' not in buy: buy['status'] = buy_stats['NOTDONE'] if 'desc' not in buy: buy['desc'] = 'empty desc' update_in_db(db, 'buy', buy, args.id)
def getShiftTime(data): with log_and_ignored(Exception): db = data['db'] username = data['username'] config = get_from_db(db, 'config', username) client = data['client'] config_locker = data['config_locker'] timeRes = client.service.doGetSystemTime(countryId=config['countryCode'], webapiKey=config['webapiKey']) with config_locker: config_u = {} config_u['shiftTime'] = timeRes - nowToEpoch() config_u['status'] = config_stats['INITIALIZED'] update_in_db(db, 'config', config_u, username) logging.debug("serwer time: %s" % (datetime.fromtimestamp(timeRes)))
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
def getConfig(data): with log_and_ignored(Exception): db = data['db'] username = data['username'] config = get_from_db(db, 'config', username) config_locker = data['config_locker'] client = data['client'] g = client.service.doQueryAllSysStatus(config['countryCode'], config['webapiKey']) ver = [x['verKey'] for x in g[0] if x['countryId'] == 1][0] with config_locker: update_in_db(db, 'config', dict(localVersion=str(ver)), username) logRes = client.service.doLoginEnc(userLogin=config['username'], userHashPassword=config['userHashPassword'], countryCode=config['countryCode'], localVersion=config['localVersion'], webapiKey=config['webapiKey']) logRes_u = {} logRes_u['sessionHandlePart'] = str(logRes['sessionHandlePart']) logRes_u['userId'] = str(logRes['userId']) with config_locker: update_in_db(db, 'config', logRes_u, username)
def checkWinning(data, bid_o): with log_and_ignored(Exception): bid_id = bid_o['id'] username = data['username'] config = get_from_db(db, 'config', username) client = data['client'] myId = config['userId'] myId = int(myId) sessionId=config['sessionHandlePart'] items = client.factory.create("ArrayOfLong") items.item = [bid_id] my_bids = client.service.doGetMyBidItems(sessionId=sessionId, itemIds=items) len_my_bids = my_bids['bidItemsCounter'] if len_my_bids > 0: my_bids = my_bids['bidItemsList']['item'] bid = my_bids[0] winnerId = bid['itemHighestBidder']['userId'] logging.info('winnerId (%d) myId (%d)' % (winnerId, myId)) if myId == winnerId: return bid_stats['WINNING'] else: prices = ['itemPrice']['item'] my_max_price = [x for x in prices if x['priceType'] == 5] highest_price = [x for x in prices if x['priceType'] == 2] if highest_price < my_max_price: return bid_stats['WINNING'] return bid_stats['NOTWINNING'] my_not_wons = client.service.doGetMyNotWonItems(sessionId=sessionId, itemIds=items) len_my_not_wons = my_not_wons['notWonItemsCounter'] if len_my_not_wons > 0: return bid_stats['NOTWON'] my_wons = client.service.doGetMyWonItems(sessionId=sessionId, itemIds=items) len_my_wons = my_wons['wonItemsCounter'] if len_my_wons > 0: return bid_stats['WON'] return bid_stats['NOTWON']
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)
def add_bid(args, db): bidbuy = get_from_db(db, 'bid', args.id) update_dict(bidbuy, args, 'whenShot') update_dict(bidbuy, args, 'price') update_dict(bidbuy, args, 'status') update_dict(bidbuy, args, 'quantity') update_dict(bidbuy, args, 'buy_id') update_dict(bidbuy, args, 'id') if 'whenShot' not in bidbuy: bidbuy['whenShot'] = -3 if 'price' not in bidbuy: bidbuy['price'] = 0 if 'status' not in bidbuy: bidbuy['status'] = bid_stats['NOTBIDDED'] if 'quantity' not in bidbuy: bidbuy['quantity'] = 1 update_in_db(db, 'bid', bidbuy, args.id)
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_bid(args, db): bidbuy = get_from_db(db, 'bid', args.id) if len(bidbuy) > 0: del_from_db(db, 'bid', args.id)
def del_buy(args, db): buy = get_from_db(db, 'buy', args.id) if len(buy) > 0: del_from_db(db, 'buy', args.id)
'filename': 'mplog-errors.log', 'mode': 'w', 'level': 'ERROR', 'formatter': 'detailed', }, }, 'root': { 'level': level, 'handlers': ['console', 'file', 'errors'] }, } logging.config.dictConfig(config_initial) update_in_db(db, 'config', dict(status=config_stats['NOTINITIALIZED']), args.username) config = get_from_db(db, 'config', args.username) client = Client(config['webapiUrl'], transport=suds_requests.RequestsTransport()) config_locker = RLock() waiters = {} waiters_bids = {} data = {'db':db, 'username':args.username, 'client':client, 'waiters': waiters, 'waiters_bids':waiters_bids, 'config_locker':config_locker} l = task.LoopingCall(runEverySecond, data) l.start(1.0) # call every second l = task.LoopingCall(runEveryQMinute, data) l.start(15.0) # call every 15 seconds l = task.LoopingCall(runEvery45Minute, data) l.start(45*60) # call every 45 minutes