def breed_multiple_users(): for user in users: if user.name == ACCOUNT_1: continue logger.suc('当前账号:{0}'.format(user.name)) breed = Breed(user) if user.name == ACCOUNT_2: breed.breed_until_max_trade_times(3, 100, 3, False, None) else: breed.breed_until_max_trade_times(4, 500, 5, False, None)
def shelf_multiple_users(): while True: try: for user in users: if user.name == ACCOUNT_0 or user.name == ACCOUNT_1: continue logger.suc('当前账号:{0}'.format(user.name)) shelf = Shelf(user) rare_num_price_dic = {0: 100, 1: 100, 2: 100, 3: 100, 4: 500} shelf.shelf_by_rare_nums_once(rare_num_price_dic) except: time.sleep(60) traceback.print_exc()
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 confirm(self, order_id, nonce): url = 'https://pet-chain.baidu.com/data/order/confirm' headers = self.headers_template headers['Referer'] = 'https://pet-chain.baidu.com/chain/chooseMyDog?appId=1&tpl=' secret = sha256(password) + '|' + order_id + '|' + nonce secret = rsa_encrypt(baidu_pub_key, secret) data = { "appId": 1, 'confirmType': 4, "s": secret, "requestId": int(time.time() * 1000), "tpl": "", } r = requests.post(url, headers=headers, data=json.dumps(data)) response = json.loads(r.content) if response['errorNo'] == '00': logger.suc('繁育确认成功') else: logger.fail('繁育确认失败: {0}'.format(response['errorMsg'])) return response
def create(self, father_pet_id, mother_pet_id, amount, captcha, seed): url = 'https://pet-chain.baidu.com/data/txn/breed/create' headers = self.headers_template headers['Referer'] = 'https://pet-chain.baidu.com/chain/chooseMyDog?appId=1&tpl=' data = { "petId": father_pet_id, "senderPetId": mother_pet_id, "amount": amount, "captcha": captcha, "seed": seed, "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.suc('繁育下单成功') else: logger.fail('繁育下单失败: {0}'.format(response['errorMsg'])) return response
def sign(self): url = 'https://pet-chain.baidu.com/data/user/sign' headers = self.headers_template headers['Referer'] = 'https://pet-chain.baidu.com/chain/personal?appId=1&tpl=' data = { "requestId": int(time.time() * 1000), "appId": 1, "tpl": "", } r = requests.post(url, headers=headers, data=json.dumps(data)) print(r.content) response = json.loads(r.content) if response['errorNo'] != '00': logger.fail('签到失败:{0}'.format(response['errorMsg'])) return sign_amount = response['data']['signAmount'] total_sign_times = response['data']['totalSignTimes'] total_sign_amount = response['data']['totalSignAmount'] if response['data']['isSigned']: logger.suc('已签到过,获得{0}微,签到次数{1} 累计{2}微'.format(sign_amount, total_sign_times, total_sign_amount)) else: logger.suc('签到成功,获得{0}微,签到次数{1} 累计{2}微'.format(sign_amount, total_sign_times, total_sign_amount))
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()
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
self.host = host self.port = port self.user_name = user_name self.password = password self.db_name = db_name def get_mongo_config(): mongo_dic = load_json('mongo.json') config = MongoConfig() config.__dict__ = mongo_dic return config config = get_mongo_config() logger.suc("数据库地址:{0} 端口:{1}".format(config.host, config.port)) mongo_client = MongoClient(config.host, config.port) mongo_client.LaiCiGou.authenticate(config.user_name, config.password) db = mongo_client[config.db_name] # 页面"缓存"数据集 # App 所有页面"缓存"数据,当有请求过来时,自动检查数据是否过期(半天),如超过则清除旧数据,重新统计新数据写入 page_caches = db['pageCaches'] # 用户财产"缓存"数据集 user_asset_caches = db['userAssetCaches'] # 狗狗数据集 pets = db['pets'] # 属性数据集 attributes = db['attributes']
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)
def force_insert_into_db(users): mongo.users.delete_many({}) for user in users: user_dic = {} user_dic.update(user.__dict__) mongo.users.insert(user_dic) def get_user_from_db(user_name): document = mongo.users.find_one({'name': user_name}) if not document: return None document['id'] = str(document['_id']) document.pop('_id') user = User() user.__dict__ = document return user if os == "Windows": force_insert_into_db(get_users()) users = get_users() user = get_user(ACCOUNT) logger.suc('当前账号:{0}'.format(user.name))