def get_parents_from_private_collection(self): father, mother = None, None cursor = private_collection.find({'user': self.user.name}) for pet in cursor: pet = self.get_pet_info_on_market(pet['petId']) pet['rareAmount'] = self.get_rare_amount(pet['attributes']) if pet['isCooling'] or pet['lockStatus'] == 1: continue if not father: father = pet logger.info('选中狗狗父亲:{0}'.format(father['petId'])) continue if not mother: mother = pet logger.info('选中狗狗母亲:{0}'.format(mother['petId'])) break if father and mother: break cursor.close() if not father or not mother: return (father, mother) # 确保父亲狗狗稀有度低于母亲狗狗,上架时可降低价格,从而减少成本,同时也减少由于上架高稀有度狗狗而被狗友抢走繁殖的可能 if father['rareAmount'] > mother['rareAmount']: father, mother = mother, father logger.warn('父亲狗狗稀有度大于母亲狗狗,交换之~') return (father, mother)
def wrapper(*args, **kwargs): left_times = times call_state, ret = False, None while left_times > 0 and call_state is False: try: if left_times != times: logger.warn('重试第 {0} 次 ===> {1}({2})'.format( times - left_times, function.__name__, args)) ret = yield function(*args, **kwargs) if isinstance(ret, bool): call_state = ret elif not ret: call_state = False else: call_state = True if not call_state: yield gen.sleep(duration) except Exception as e: logger.error(e) finally: left_times -= 1 if call_state is False: message = '<After try {0} times> def {1}({2}) call fail'.format( times, function.__name__, args) logger.error(message) return ret
def buy_pets_until_max_trade_times(self, price_limit, rare_degree, max_generation, max_count): count = 0 while True: try: pets = self.get_pets_with_condition(rare_degree) for pet in pets: price = float(pet["amount"]) if price > price_limit: continue generation = pet['generation'] if generation > max_generation: continue response = self.buy(pet) if response['errorNo'] == '00': count = count + 1 logger.info('已购买 {0} 条'.format(count)) # 购买已达最大数量限制 if count == max_count: return # 10018:您今日交易次数已超限,明天再试试吧 if response['errorNo'] == '10018': logger.warn('达到最大交易次数时已购买 {0} 条'.format(count)) return time.sleep(5) except: traceback.print_exc()
def sale_all(self, rare_num_price_dic, include_angel=False): page_size = 10 total = self.get_pets_count() pages = total // page_size if total % page_size == 0 else ( total // page_size + 1) for page_no in range(pages): page_no = page_no + 1 pets = self.get_pets(page_no, page_size, pages, total) for pet in pets: cooling_interval = pet['coolingInterval'] if '分钟' in cooling_interval: continue cooling_interval = int(cooling_interval.replace('天')) if cooling_interval <= 4: # 只卖休息时间大于4天的狗狗 continue if pet['shelfStatus'] != 0 or pet['lockStatus'] == 1: continue pet_info = self.get_pet_info_on_market(pet['petId']) rare_num = self.get_rare_amount(pet_info['attributes']) if rare_num not in rare_num_price_dic: continue if not include_angel: physique = self.get_attribute(pet_info['attributes'], '体型') if physique == '天使': logger.warn('天使狗狗不卖: {0}'.format(pet['petId'])) continue self.sale(pet['petId'], rare_num_price_dic[rare_num]) time.sleep(5)
def shelf_by_rare_nums_once(self, rare_num_price_dic=None): if rare_num_price_dic is None: logger.warn('没有设置价格字典!') return page_size, total = 10, self.get_idle_pets_count() pages = math.ceil(total / page_size) logger.info('共 {0} 条狗狗,每页 {1} 条,共 {2} 页'.format( total, page_size, pages)) for page in range(pages): logger.info('处理第{0}页:'.format(page + 1)) pets = self.get_idle_pets(page + 1, page_size) for pet in pets: # 珍藏的狗狗不上架 if private_collection.find_one({ 'user': self.user.name, 'petId': pet['petId'] }): logger.warn('珍藏的狗狗不上架:{0}'.format(pet['petId'])) continue pet_info = self.get_pet_info_on_market(pet['petId']) rare_num = self.get_rare_amount(pet_info['attributes']) if rare_num not in rare_num_price_dic: continue price = rare_num_price_dic[rare_num] logger.info('挂出繁育 {0},{1}稀,价格 {2}'.format( pet['petId'], rare_num, price)) self.shelf(pet['petId'], price) # 百度控制的上架时间间隔目前约为10秒,少于10秒会被拒绝 self.random_sleep(10, 15)
def buy_pets_until_max_trade_times(self, price_limit, rare_degree, max_generation, max_count): count = 0 page_no = 1 while True: try: pets = self.get_pets_with_condition(page_no, rare_degree) logger.warn('第 {0} 页'.format(page_no)) page_no = page_no + 1 for pet in pets: price = float(pet["amount"]) if price > price_limit: page_no = 1 generation = pet['generation'] if generation > max_generation: continue response = self.buy(pet) if response['errorNo'] == '00': count = count + 1 logger.info('已购买 {0} 条'.format(count)) self.random_sleep(180, 240) # 购买已达最大数量限制 if count == max_count: return # 10018:您今日交易次数已超限,明天再试试吧 if response['errorNo'] == '10018': logger.warn('达到最大交易次数时已购买 {0} 条'.format(count)) return self.random_sleep(30, 60) except: self.random_sleep(30, 60) traceback.print_exc()
def count(): while True: try: if create_breed_info(): return except KeyboardInterrupt: logger.warn("强制停止") return except: traceback.print_exc()
def get_captcha_and_seed(self): url = 'https://pet-chain.baidu.com/data/captcha/gen' headers = self.headers_template headers[ 'Referer'] = 'https://pet-chain.baidu.com/chain/chooseMyDog?appId=1&tpl=' data = {"requestId": int(time.time() * 1000), "appId": 1, "tpl": ""} r = requests.post(url, headers=headers, data=json.dumps(data)) response = json.loads(r.content) if response['errorNo'] != '00': logger.warn('获取验证码失败:{0}'.format(response['errorMsg'])) return None, None return response['data']['seed'], response['data']['img']
def get_save_pets_forever(self): rare_degree = 0 while True: try: self.get_save_pets(rare_degree) rare_degree = rare_degree + 1 if rare_degree > 5: rare_degree = 0 except KeyboardInterrupt: logger.warn("强制停止") return except: traceback.print_exc()
def check_duplicate_pet(): duplicate = [] index = 0 for pet in mongo.pet_collection.find(): index = index + 1 count = mongo.pet_collection.find({'petId': pet['petId']}).count() if count > 1: duplicate.append(pet['petId']) logger.warn('检查第 {0} 条狗狗 {1} :有重复'.format(index, pet['petId'])) else: logger.info('检查第 {0} 条狗狗 {1} :无重复'.format(index, pet['petId'])) logger.info(duplicate)
async def users_create(user: UserCreate, db: Session = Depends(get_db)): logger.debug(f'user: {user}') logger.debug(f'db: {db}') already_user = user_service.get_user_by_user_name(db, user.username) logger.debug(f'already_user: {already_user}') if already_user is not None: logger.warn(f'already_user: {already_user}') raise ValidationException("登録済みのユーザがいます") user_service.create_user(db, user) return {'result':'success'}
def prepare_parents(self, father_rare_num, father_price, mother_rare_num): while True: try: father, mother = self.get_parents(father_rare_num, mother_rare_num) if not father or not mother: logger.warn('无满足条件的繁育双亲, 一分钟后重试') time.sleep(60) continue # 未上架繁育,将其上架 if father['shelfStatus'] == 0: logger.info('父亲狗狗{0}处于未上架繁育状态,将其上架'.format(father['petId'])) shelf = Shelf(self.cookie) shelf.shelf(father['petId'], father_price) # 等待3分钟避免错误:专属分享,3分钟后可购买 time.sleep(3 * 60) # 出售中,将其下架然后上架繁育 elif father['shelfStatus'] == 1: logger.info('父亲狗狗{0}处于售卖中, 将其下架, 三分钟后再挂出繁育'.format(father['petId'])) sale = Sale(self.cookie) sale.unsale(father['petId']) # 3分钟后再挂出繁育,避免上下架过频繁 time.sleep(3 * 60) logger.info('挂出繁育父亲狗狗{0}'.format(father['petId'])) shelf = Shelf(self.cookie) shelf.shelf(father['petId'], father_price) # 出售中,将其下架 if mother['shelfStatus'] == 1: logger.info('母亲狗狗{0}处于出售状态,将其下架然'.format(mother['petId'])) sale = Sale(self.cookie) sale.unsale(mother['petId']) # 挂出繁育中,将其下架 elif mother['shelfStatus'] == 2: logger.info('母亲狗狗{0}处于挂出繁育状态,将其下架'.format(mother['petId'])) shelf = Shelf(self.cookie) shelf.off_shelf(mother['petId']) # 再次获取狗狗双亲信息,保证信息是最新的 father = self.get_pet_info_on_market(father['petId']) mother = self.get_pet_info_on_market(mother['petId']) return (father, mother) except: traceback.print_exc()
async def authenticate(user: UserLogin, db: Session = Depends(get_db)): logger.debug(f'user: {user}') logger.debug(f'db: {db}') login_user = user_service.get_user_by_user_login(db, user) logger.debug(f'login_user: {login_user}') if login_user is None: logger.warn(f'user: {user}') raise ValidationException("ユーザが見つかりません") tokens = user_service.create_token(db, login_user.id) logger.debug(f'tokens: {tokens}') return {'access_token': tokens['access_token'], 'refresh_token': tokens['refresh_token']}
def db_copy(name, local_to_remote=True): if local_to_remote: src_client = MongoClient() src_db = src_client['LaiCiGou'] src_coll = src_db[name] des_db = mongo.db des_coll = des_db[name] else: src_db = mongo.db src_coll = src_db[name] des_client = MongoClient() des_db = des_client['LaiCiGou'] des_coll = des_db[name] last_process_id = get_last_process_id('db_copy') if last_process_id: # 有处理记录, 接着上次继续处理 logger.suc('继续上次的处理,上次最后处理的id为:{0}'.format(last_process_id)) query = {'_id': {'$gt': ObjectId(last_process_id)}} total = src_coll.find(query).sort('_id', pymongo.ASCENDING).count() # 设置no_cursor_timeout为真,避免处理时间过长报错:pymongo.errors.CursorNotFound: Cursor not found, cursor id: xxxxxxxxx cursor = src_coll.find(query, no_cursor_timeout=True).sort( '_id', pymongo.ASCENDING) else: # 无处理记录, 清除数据从头开始处理 logger.warn('无上次处理记录, 强制清除数据从头开始处理') mongo.breed_info.drop() total = src_coll.find({}).sort('_id', pymongo.ASCENDING).count() # 设置no_cursor_timeout为真,避免处理时间过长报错:pymongo.errors.CursorNotFound: Cursor not found, cursor id: xxxxxxxxx cursor = src_coll.find({}, no_cursor_timeout=True).sort( '_id', pymongo.ASCENDING) index = 0 for doc in cursor: index = index + 1 des_coll.insert(doc) insert_update_last_process_id('db_copy', doc['_id']) if index % 100 == 0: logger.info('一共 {0} 份文档,已迁移 {1} 条'.format(total, index)) logger.info('一共 {0} 份文档,已迁移 {1} 条'.format(total, index)) cursor.close()
def create_attributes_int_ids(): last_process_id = get_last_process_id('create_attributes_int_ids') if last_process_id: # 有处理记录, 接着上次继续处理 logger.suc('继续上次的处理,上次最后处理的id为:{0}'.format(last_process_id)) total = mongo.pets.find({ '_id': { '$gt': ObjectId(last_process_id) } }).sort('_id', pymongo.ASCENDING).count() cursor = mongo.pets.find({ '_id': { '$gt': ObjectId(last_process_id) } }, no_cursor_timeout=True).sort( '_id', pymongo.ASCENDING) else: # 无处理记录, 清除数据从头开始处理 logger.warn('无上次处理记录, 强制清除数据从头开始处理') total = mongo.pets.find({}).sort('_id', pymongo.ASCENDING).count() cursor = mongo.pets.find({}, no_cursor_timeout=True).sort( '_id', pymongo.ASCENDING) index = 0 for pet in cursor: attributes = pet['attributes'] aIds = list() for attribute in attributes: doc = mongo.attributes.find_one(attribute) aIds.append(doc['intId']) mongo.pets.update_one({'_id': pet['_id']}, {'$set': { 'aIds': aIds }}, upsert=False) insert_update_last_process_id('create_attributes_int_ids', pet['_id']) logger.info('一共 {0} 条狗狗,已统计处理 {1} 条'.format(total, index)) index = index + 1 logger.info('一共 {0} 条狗狗,已统计处理 {1} 条'.format(total, index)) cursor.close()
def shelf_by_rare_nums_once(self, rare_num_price_dic=None): if rare_num_price_dic is None: logger.warn('没有设置价格字典!') return pages = 0 while True: pets = self.get_idle_pets(1, 10) if len(pets) == 0: break pages = pages + 1 logger.info('处理第{0}页:'.format(pages)) for pet in pets: # 先到本地数据库中查询 exist = mongo.pet_collection.find_one({'petId': pet['petId']}) if exist: rare_num = exist['rareAmount'] else: pet_info = self.get_pet_info_on_market(pet['petId']) rare_num = self.get_rare_amount(pet_info['attributes']) # 如果本地数据库还没有收录,则将其收录 pet_collector = Collector(self.cookie) pet_collector.query_save_pet_and_ancestors(pet['petId']) if rare_num not in rare_num_price_dic: continue time.sleep(10) # 百度控制的上架时间间隔目前约为10秒,少于10秒会被拒绝 price = rare_num_price_dic[rare_num] logger.info('挂出繁育 {0},{1}稀,价格 {2}'.format( pet['petId'], rare_num, price)) order_id, nonce = self.create(pet['petId'], price) if order_id: self.confirm(pet['petId'], order_id, nonce) time.sleep(5)
def count(): last_process_id = get_last_process_id('twins_count') if last_process_id: # 有处理记录, 接着上次继续处理 logger.suc('继续上次的处理,上次最后处理的id为:{0}'.format(last_process_id)) total = mongo.pets.find({ '_id': { '$gt': ObjectId(last_process_id) } }).sort('_id', pymongo.ASCENDING).count() # 设置no_cursor_timeout为真,避免处理时间过长报错:pymongo.errors.CursorNotFound: Cursor not found, cursor id: xxxxxxxxx cursor = mongo.pets.find({ '_id': { '$gt': ObjectId(last_process_id) } }, no_cursor_timeout=True).sort( '_id', pymongo.ASCENDING) else: # 无处理记录, 清除数据从头开始处理 logger.warn('无上次处理记录, 强制清除数据从头开始处理') mongo.twins.drop() total = mongo.pets.find({}).sort('_id', pymongo.ASCENDING).count() # 设置no_cursor_timeout为真,避免处理时间过长报错:pymongo.errors.CursorNotFound: Cursor not found, cursor id: xxxxxxxxx cursor = mongo.pets.find({}, no_cursor_timeout=True).sort( '_id', pymongo.ASCENDING) index = 0 for pet in cursor: check_pet(pet) insert_update_last_process_id('twins_count', pet['_id']) index = index + 1 if index % 100 == 0: logger.info('一共 {0} 条狗狗,已统计处理 {1} 条'.format(total, index)) logger.info('一共 {0} 条狗狗,已统计处理 {1} 条'.format(total, index)) cursor.close()
# 无处理记录, 清除数据从头开始处理 logger.warn('无上次处理记录, 强制清除数据从头开始处理') mongo.twins.drop() total = mongo.pets.find({}).sort('_id', pymongo.ASCENDING).count() # 设置no_cursor_timeout为真,避免处理时间过长报错:pymongo.errors.CursorNotFound: Cursor not found, cursor id: xxxxxxxxx cursor = mongo.pets.find({}, no_cursor_timeout=True).sort( '_id', pymongo.ASCENDING) index = 0 for pet in cursor: check_pet(pet) insert_update_last_process_id('twins_count', pet['_id']) index = index + 1 if index % 100 == 0: logger.info('一共 {0} 条狗狗,已统计处理 {1} 条'.format(total, index)) logger.info('一共 {0} 条狗狗,已统计处理 {1} 条'.format(total, index)) cursor.close() if __name__ == '__main__': # total = mongo.pets.find({}).count() # logger.info(total) i = 0 while True: try: count() except: i = i + 1 logger.warn('异常 {0}'.format(i))
def create_breed_info(): last_process_id = get_last_process_id('breed_info') if last_process_id: # 有处理记录, 接着上次继续处理 logger.suc('继续上次的处理,上次最后处理的id为:{0}'.format(last_process_id)) query = { '_id': { '$gt': ObjectId(last_process_id) }, 'fatherId': { '$ne': None } } total = mongo.pets.find(query).sort('_id', pymongo.ASCENDING).count() # 设置no_cursor_timeout为真,避免处理时间过长报错:pymongo.errors.CursorNotFound: Cursor not found, cursor id: xxxxxxxxx cursor = mongo.pets.find(query, no_cursor_timeout=True).sort( '_id', pymongo.ASCENDING) else: # 无处理记录, 清除数据从头开始处理 logger.warn('无上次处理记录, 强制清除数据从头开始处理') mongo.breed_info.drop() total = mongo.pets.find({ 'fatherId': { '$ne': None } }).sort('_id', pymongo.ASCENDING).count() # 设置no_cursor_timeout为真,避免处理时间过长报错:pymongo.errors.CursorNotFound: Cursor not found, cursor id: xxxxxxxxx cursor = mongo.pets.find({ 'fatherId': { '$ne': None } }, no_cursor_timeout=True).sort( '_id', pymongo.ASCENDING) index = 0 for pet in cursor: index = index + 1 father = mongo.pets.find_one({'petId': pet['fatherId']}) mother = mongo.pets.find_one({'petId': pet['motherId']}) if not father or not mother: continue data = { 'childRareAmount': pet['rareAmount'], 'fatherRareAmount': father['rareAmount'], 'motherRareAmount': mother['rareAmount'], } exist = mongo.breed_info.find_one(data) if exist: mongo.breed_info.update_one({'_id': exist['_id']}, {'$inc': { 'childAmount': 1 }}, upsert=False) else: data['childAmount'] = 1 mongo.breed_info.insert(data) insert_update_last_process_id('breed_info', pet['_id']) logger.info('一共 {0} 条狗狗,已统计处理 {1} 条'.format(total, index)) # if index % 100 == 0: # logger.info('一共 {0} 条狗狗,已统计处理 {1} 条'.format(total, index)) logger.info('一共 {0} 条狗狗,已统计处理 {1} 条'.format(total, index)) cursor.close() return total == index
def random_sleep(self, min, max): interval = random.randint(min, max) logger.warn('随机暂停 {0} 秒'.format(interval)) time.sleep(interval)
def __init__(self, msg): logger.warn(f'TokenExceptionが発生しました。 (msg: {msg})') super(TokenException, self).__init__(status_code=401, detail=msg)
def __init__(self, msg): logger.warn(f'ValidationExceptionが発生しました。 (msg: {msg})') super(ValidationException, self).__init__(status_code=422, detail=msg)
def prepare_parents(self, father_rare_num, father_price, mother_rare_num, from_private_collection, father_price_dic): while True: try: if from_private_collection: father, mother = self.get_parents_from_private_collection() if not father or not mother: logger.warn('没有可用的狗狗,请使用其他方式选择繁殖') return father, mother father_price = father_price_dic[father['rareAmount']] logger.warn('父亲狗狗上架繁育价格为:{0}'.format(father_price)) else: father, mother = self.get_parents(father_rare_num, mother_rare_num) if not father or not mother: logger.warn('无满足条件的繁育双亲, 一分钟后重试') self.random_sleep(60, 90) continue # 未上架繁育,将其上架 if father['shelfStatus'] == 0: logger.info('父亲狗狗{0}处于未上架繁育状态,将其上架'.format( father['petId'])) shelf = Shelf(self.user) shelf_success = shelf.shelf(father['petId'], father_price) if not shelf_success: self.random_sleep(10, 15) continue # 等待3分钟避免错误:专属分享,3分钟后可购买 time.sleep(3 * 60) # 出售中,将其下架然后上架繁育 elif father['shelfStatus'] == 1: logger.info('父亲狗狗{0}处于售卖中, 将其下架, 三分钟后再挂出繁育'.format( father['petId'])) sale = Sale(self.user) sale.unsale(father['petId']) # 3分钟后再挂出繁育,避免上下架过频繁 time.sleep(3 * 60) logger.info('挂出繁育父亲狗狗{0}'.format(father['petId'])) shelf = Shelf(self.user) shelf_success = shelf.shelf(father['petId'], father_price) if not shelf_success: continue # 出售中,将其下架 if mother['shelfStatus'] == 1: logger.info('母亲狗狗{0}处于出售状态,将其下架然'.format(mother['petId'])) sale = Sale(self.user) sale.unsale(mother['petId']) # 挂出繁育中,将其下架 elif mother['shelfStatus'] == 2: logger.info('母亲狗狗{0}处于挂出繁育状态,将其下架'.format(mother['petId'])) shelf = Shelf(self.user) shelf.off_shelf(mother['petId']) # 再次获取狗狗双亲信息,保证信息是最新的 father = self.get_pet_info_on_market(father['petId']) mother = self.get_pet_info_on_market(mother['petId']) return (father, mother) except: traceback.print_exc() self.random_sleep(30, 60)
def buy_pets(self, max_count): count = 0 page_no = 1 while True: try: if page_no > 200: page_no = 1 # 使用无狗狗数据的账号 self.user = get_user_from_db(ACCOUNT_1) condition = {'rareAmount': 4, 'attributes': { '体型': ['天使', '角鲸'], '眼睛': ['小对眼'], '嘴巴': ['樱桃', '大胡子', '长舌头', '橄榄', '甜蜜蜜'], '身体色': ['高级黑', '米色'] }, 'price': 2000000} conditions = [ {'rareAmount': 4, 'attributes': { '体型': ['天使', '角鲸'], '眼睛': '小对眼', '嘴巴': ['樱桃', '大胡子', '长舌头', '橄榄', '甜蜜蜜'], '身体色': ['高级黑', '米色'] }, 'price': 20000}, {'rareAmount': 5, 'attributes': { '体型': ['天使', '角鲸'], '眼睛': '小对眼', '嘴巴': ['樱桃', '大胡子', '长舌头', '橄榄', '甜蜜蜜'], '身体色': ['高级黑', '米色'] }, 'price': 50000} ] logger.info('第{0}页, 账号:{1}'.format(page_no, self.user.name)) pets = self.get_pets_with_condition_1(page_no, 3) page_no = page_no + 1 for pet in pets: logger.info('狗狗价格:{0}'.format(pet["amount"])) price = float(pet["amount"]) if price > condition['price']: page_no = 1 continue pet_info = self.get_pet_info_on_market(pet['petId']) rare_amount = self.get_rare_amount(pet_info['attributes']) if condition['rareAmount'] != rare_amount: continue msg = '' is_the_pet = True for key in condition['attributes']: value = self.get_attribute(pet_info['attributes'], key) msg = msg + "{0}:{1}, ".format(key, value) if value not in condition['attributes'][key]: is_the_pet = False break if not is_the_pet: logger.warn('狗狗id: {0} 属性不符:{1}'.format(pet['petId'], msg)) continue else: logger.suc('狗狗id: {0} 属性符合:{1}'.format(pet['petId'], msg)) # # user切换回去 # self.user = user # logger.info('切回账号:{0}'.format(self.user.name)) # response = self.buy(pet) # if response['errorNo'] == '00': # count = count + 1 # logger.info('已购买 {0} 条'.format(count)) # self.random_sleep(180, 240) # # # 购买已达最大数量限制 # if count == max_count: # return # # # 10018:您今日交易次数已超限,明天再试试吧 # if response['errorNo'] == '10018': # logger.warn('达到最大交易次数时已购买 {0} 条'.format(count)) # return # self.random_sleep(30, 60) except: traceback.print_exc() self.random_sleep(30, 60)