def comandos(msg): if 'text' in msg: if msg['text'].startswith('/run'): if msg['from']['id'] == config.sudo: cmd = msg['text'].replace('/run ','') redis.hset('run', 'status', '{}'.format(cmd)) sendMessage(msg['chat']['id'], 'Alterando as configurações para *{}*'.format(cmd),"markdown") return if redis.hgetall('run')['status'] == 'on': regras.regras(msg) # modulo de regras myid.my_id(msg) # modulo do comando /id newmember.welcome(msg) # modulo de boas vindas promover.promover(msg) # mudulo de divulgação start.start(msg) # modulo de inicialização banhammer.banhammer(msg) # modulo de administração printar.printar(msg) # modulo do comando /print comands.comands(msg) # modulo do comando /comandos conversa(msg) # modulo de inteligencia artificial tradutor.traduzir(msg) #modulo de tradução qr.make_qr(msg) # modulo co comando /qr
def put_contact(redis, contact_nickname): if 'phone' in request.form: redis.hset('contacts:' + contact_nickname, REDIS_HASH_PARAM_PHONE, request.form['phone']) if 'imgur' in request.form: redis.hset('contacts:' + contact_nickname, REDIS_HASH_PARAM_IMGUR, request.form['phone']) return '', 204
def logosticker(m): rank = bot.get_chat_member(channel,m.from_user.id).status print rank if rank == "creator" or rank == "administrator" or rank == "member": redis.hset("logosticker","{}".format(m.from_user.id),"{}".format(m.text)) markup = types.InlineKeyboardMarkup() c = types.InlineKeyboardButton("طرح ساده(انگلیسی)",callback_data='1') v = types.InlineKeyboardButton("طرح ساده(انگلیسی)",callback_data='2') markup.add(c,v) q = types.InlineKeyboardButton("طرح ساده(انگلیسی)",callback_data='3') w = types.InlineKeyboardButton("طرح ساده(انگلیسی)",callback_data='4') markup.add(q,w) e = types.InlineKeyboardButton("طرح ساده(انگلیسی)",callback_data='5') r = types.InlineKeyboardButton("طرح ساده(انگلیسی)",callback_data='6') markup.add(e,r) t = types.InlineKeyboardButton("طرح ساده(فارسی)",callback_data='7') y = types.InlineKeyboardButton("طرح ساده(فارسی)",callback_data='8') markup.add(t,y) u = types.InlineKeyboardButton("طرح ساده(فارسی)",callback_data='9') z = types.InlineKeyboardButton("طرح ساده(فارسی)",callback_data='10') markup.add(u,z) hg = types.InlineKeyboardButton("طرح ساده(فارسی)",callback_data='11') gh = types.InlineKeyboardButton("طرح ساده(فارسی)",callback_data='12') markup.add(hg,gh) kj = types.InlineKeyboardButton("1️⃣",callback_data='s1') jh = types.InlineKeyboardButton("صفه بعد▶️",callback_data='next') markup.add(kj,jh) bot.send_message(m.chat.id, "لطفا طرح خود را انتخاب کنید:", disable_notification=True, reply_markup=markup, parse_mode="Markdown") else: markup = types.InlineKeyboardMarkup() ch = types.InlineKeyboardButton('عضومیشوم',url='https://t.me/joinchat/AAAAAEHdKsQ8JZTUke0umw') markup.add(ch) bot.send_message(m.chat.id,'سلام\nبرای استفاده از قابلیت های ربات و مطلع شدن ازاپدیت ها و...\n باید در کانال عضوشودیدو مجدد /start را بزنید',reply_markup=markup)
def get_all_url_from_redis_set(self): urls = redis.hkeys("url_title") for i in urls: if len(redis.hget("url_title", i)) != 2: redis.hset("can_use_urls", i.decode("utf8"), redis.hget("url_title", i)) print("set {} ok!".format(i.decode("utf8")))
def run(self): # Store a metadoc global nb_thread time_start = time.time() tx_hash = store_var_contract.functions.addMetadocMap( self.body['path'], self.body['creation_date'], self.body['blocks'], self.body['original_size'], self.body['entangling_blocks']).transact() time_mid = time.time() receipt = wait_for_receipt(w3, tx_hash, 0.1) redis.hset("files:{:s}".format(self.body['path']), "flag", 1) time_end = time.time() nb_thread = nb_thread - 1 time_transaction = time_mid - time_start time_receipt = time_end - time_start print("Thread finished for " + self.body['path'] + " / Time receipt: " + str(time_receipt)) fichier.write("files:" + self.body['path'] + " " + str(time_end) + " " + str(time_receipt) + "\n") if ((self.cpt % 100) == 0): print( "\n\n" + str(self.cpt) + " files in the blockchain ! Check disk space on nodes ;)\n\n")
def set_numpy(redis:redis.Redis, key:str, value:np.ndarray): arr_dict = dict( value=value.tobytes(), dtype=value.dtype.name, shape=np.array(value.shape,"i").tobytes() ) redis.hset(key, mapping=arr_dict)
def run(self): time_redis = redis.time() # Send statistics to the test container : START link = "http://stats:80/api/receive.php?request=" + self.body[ 'path'] + "///" + str(time_redis[0]) + "." + str( time_redis[1]) + "///Start" requests.get(link) print("Thread started for metadoc ", self.body['path'], "currently time in redis: ", time_redis) tx_hash = store_var_contract.functions.addMetadocMap( self.body['path'], self.body['creation_date'], self.body['blocks'], self.body['original_size'], self.body['entangling_blocks']).transact() receipt = wait_for_receipt(w3, tx_hash, 1) redis.hset("files:{:s}".format(self.body['path']), "flag", 1) time_redis = redis.time() print("Thread finished for metadoc ", self.body['path'], "currently time in redis: ", time_redis) # Send statistics to the test container : FINISH link = "http://stats:80/api/receive.php?request=" + self.body[ 'path'] + "///" + str(time_redis[0]) + "." + str( time_redis[1]) + "///Finish" requests.get(link)
def new_user(redis, username): user_id = str(int(redis.hget("counter", "user")) + 1) redis.hset("user:"******"username", username) redis.hset("user:"******"created", time.time()) redis.hset("user:"******"karma", 0) redis.hset("user:"******"comments", 0) redis.hset("user:"******"submissions", 0)
def run(self): dbJson = list() redis = open_connection_redis(self.schema_conv_output_option.host, self.schema_conv_output_option.username, self.schema_conv_output_option.password, self.schema_conv_output_option.dbname) mongoConnection = open_connection_mongodb(self.schema_conv_init_option) # TODO: Map nhe lai cai nay thanh check schema roi loop tren kia. for dataAndSchema in mongoConnection.list_collection_names(): data = dataAndSchema["data"] if dataAndSchema["schema"]["collection"] == "string": for item in data: redis.set(item["key"], item["value"]) elif dataAndSchema["schema"]["collection"] == "list": for item in data: redis.rpush(item["key"], *item["value"]) elif dataAndSchema["schema"]["collection"] == "set": for item in data: redis.sadd(item["key"], *item["value"]) elif dataAndSchema["schema"]["collection"] == "hash": for item in data: redis.hset(item["key"], mapping=item["value"]) elif dataAndSchema["schema"]["collection"] == "sortedSet": for item in data: redis.zadd(item["key"], item["value"]) else: for item in data: # TODO: bitmap hyperlog redis.set( dataAndSchema["schema"]["collection"] + "_" + item["key"], item["value"])
def hashes_redis(): import redis redis = redis.Redis(host='127.0.0.1', port=6379, db=0, charset="utf-8", decode_responses=True) print("-------------") print("HASH") print("-------------") #hmset(name, mapping) hget(name, key) hgetall(name) redis.hmset("user.1", {"name": "peter", "email": "*****@*****.**"}) print("map.1: ", redis.hgetall("user.1")) print("name.1:", redis.hget("user.1", "name")) print("email.1:", redis.hget("user.1", "email")) print("-------------") #hset(key, field, value) hget() redis.hset("user.2", "name1", "peter") print("map.2: ", redis.hgetall("user.2")) print("type map.2: ", redis.type("user.2")) print("name.2:", redis.hget("user.2", "name1")) print("-------------") #delete all keys for key in redis.scan_iter("prefix:*"): redis.delete(key)
def run(self): #time_redis = redis.time() #delta_time1 = time_redis # Send statistics to the test container : START global nb_thread #link="http://cluster_stats:80/api/receive.php?request="+self.body['path']+"///"+str(time_redis[0])+"."+str(time_redis[1])+"///Start" #requests.get(link) #print("Thread started for metadoc ", self.body['path'], "currently time in redis: ", time_redis) time_start = time.time() tx_hash = store_var_contract.functions.addMetadocMap( self.body['path'], self.body['creation_date'], self.body['blocks'], self.body['original_size'], self.body['entangling_blocks']).transact() time_mid = time.time() receipt = wait_for_receipt(w3, tx_hash, 0.1) redis.hset("files:{:s}".format(self.body['path']), "flag", 1) time_end = time.time() time_transaction = time_mid - time_start time_receipt = time_end - time_start print("Thread finished for " + self.body['path'] + ". Time start: " + str(time_start) + " / Time receipt: " + str(time_receipt)) fichier.write("files:" + self.body['path'] + " " + str(time_start) + " " + str(time_receipt) + "\n") nb_thread = nb_thread - 1
def hb_post(): hb = request.form.to_dict() redis.hset('NodeHB', hb['node_id'], hb) ts = int(time.time()) redis.hset(hb['node_id'], ts, hb) content = str(hb) return Response_headers(content)
def doWelfareById(redis, uid, account, id): """ 福利 """ today = datetime.now().strftime("%Y-%m-%d") if id == '2': playerCoin = redis.hget(FORMAT_USER_TABLE % uid, 'gold') playerCoin = playerCoin if playerCoin else 0 if int(playerCoin) >= SIGN_LINE: return {'code': 1, 'msg': u'未达到低保线无法领取'} key = WELFARE_USER_INSURANCE % (account, today) if redis.llen(key) >= SIGN_MAX: return {'code': 1, 'msg': u'已经领取了 {0} 次'.format(SIGN_MAX)} redis.lpush(key, SIGN_COINNUM) player_add_gold(redis, account, SIGN_COINNUM) elif id == '1': # 新手礼包 if redis.hget(GOLD_REWARD_NEW_PRESENT_HASH, account) == MESSION_STATUS_OVER: return {'code': 1, 'msg': u'您已领取,无法再次领取'} redis.hset(GOLD_REWARD_NEW_PRESENT_HASH, account, MESSION_STATUS_OVER) elif id == '0': # 每日首冲奖励 if not redis.sismember(DAILY_USER_GOLD2_SET % today, account): return {'code': 1, 'msg': u'您今日还未完成首冲'} elif redis.hget(GOLD_REWARD_DAY_BUY_GOLD_HASH % today, account) == MESSION_STATUS_OVER: return {'code': 1, 'msg': u'您已领取,无法再次领取'} redis.hget(GOLD_REWARD_DAY_BUY_GOLD_HASH % today, account, MESSION_STATUS_OVER) return {'code': 0, 'msg': u'领取成功'}
def get_page_url(date, num, page): print(date, num, page) result = requests.get( 'https://pacaio.match.qq.com/openapi/json?key=news:%d&num=%d&page=%d&expIds=&callback=__jp0' % (date, num, page)) # 得到的返回值被故意加了一层,把这一层切掉才是json的标准格式 j = json.loads(result.text[6:-1]) # 页数超过最后一页后,没有数据,直接跳出返回False停止下一页请求 if j['data'] == None: return False # 得到正常返回值后遍历所有'data'节点 for jj in j['data']: title = jj['title'] url = jj['url'] print(title, url) # 判断redis是否已经存在该文章标题,如果不存在,将它加进Redis中 if not redis.hexists(hashKey, title): # 使用Hash是为了防止加入重复数据 # List是起到消息队列的作用,消费者从队列右侧pop即可 redis.hset(hashKey, title, url) redis.lpush(listKey, title) # 如果标题已经存在,有可能是以下情况: # 1. 在之前已经爬过这一天数据了,数据没有更新 # 2. 在之前爬过这一天数据了,数据更新了,但是之前的循环已经把更新的数据搞定了 # 这两种情况中,无论剩下的还是后面页的所有数据都已经存入Redis了,所以直接跳出并且终止下一页的请求 else: return False # 本页数据都存成功了,请求下一页 return True
def handle_unfollow(event): sourceId = getSourceId(event.source) redis.hset(sourceId, 'voted', 'N') current = redis.hget(sourceId, 'current') if current != '-': remove_member(current, sourceId) redis.hset(sourceId, 'current', '-')
def callback_inline(call): if call.message: if call.data == "farsi": redis.hset("lang:{}".format(call.message.chat.id),"farsi",True) redis.hdel("lang:{}".format(call.message.chat.id),"english") bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="زبان شما با موفقیت به فارسی انتخاب شد\n\nلطفا یکدام از دکمه های زیر را انتخاب کنید👇", reply_markup=markupstartfa) bot.answer_callback_query(callback_query_id=call.id,text="خوش آمدید😊") if call.message: if call.data == "english": redis.hset("lang:{}".format(call.message.chat.id),"english",True) redis.hdel("lang:{}".format(call.message.chat.id),"farsi") bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Your language selected to english🇺🇸\nPlease select one of the button👇", reply_markup=markupstarten) bot.answer_callback_query(callback_query_id=call.id,text="Wellcome😊") if call.message: if call.data == "joke": f = open("joke.db") text = f.read() text1 = text.split(",") last = random.choice(text1) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="{}\n\n[😂ربات جک😂](https://telegram.me/FunJokeBot)".format(last), reply_markup=markupjoke, parse_mode="Markdown") if call.message: if call.data == "chuk": url = "http://tambal.azurewebsites.net/joke/random" res = urllib.urlopen(url) parsed_json = json.loads(res.read()) joke = parsed_json['joke'] bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="{}\n\n[😂Joke bot😂](https://telegram.me/FunJokeBot)".format(joke), reply_markup=markupchuk, parse_mode="Markdown") if call.message: if call.data == "avazfa": bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="زبان خود را انتخاب کنید 👇\nSelect your language👇", reply_markup=markupstart) if call.message: if call.data == "avazen": bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Select your language👇\nزبان خود را انتخاب کنید 👇", reply_markup=markupstart) if call.message: if call.data == "getjoke": f = open("joke.db") text = f.read() text1 = text.split(",") last = random.choice(text1) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="{}\n\n\n[😂ربات جک😂](https://telegram.me/FunJokeBot)".format(last), reply_markup=markupjoke, parse_mode="Markdown") if call.message: if call.data == "sendjoke": bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="برای ارسال جک یا انتقاد یا پیشنهاد به ما طبق دستورالعمل زیر عمل کنید👇\n/send متن", reply_markup=markupback) if call.message: if call.data == "getjokeen": url = "http://tambal.azurewebsites.net/joke/random" res = urllib.urlopen(url) parsed_json = json.loads(res.read()) joke = parsed_json['joke'] bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="{}\n\n\n[😂Joke bot😂](https://telegram.me/FunJokeBot)".format(joke), reply_markup=markupchuk, parse_mode="Markdown") if call.message: if call.data == "sendjokeen": bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="For send us rate or joke please send👇\n/Send Text", reply_markup=markupbacken) if call.message: if call.data == "back": bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="به عقب برگشتید🔙\n\nلطفا یکدام از دکمه های زیر را انتخاب کنید👇", reply_markup=markupstartfa) if call.message: if call.data == "backen": bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Come backed🔙\nPlease select one of the button👇", reply_markup=markupstarten)
def add_acount(): while True: name = input("Your name? ") if not redis.exists(name): redis.sadd(name, name) redis.hset(MemMessages, name, "") break print("That name is already taken.", end=" ")
def create(username, password): user = User(username) pw_hash = generate_password_hash(password) ok = redis.hsetnx(user.key(), 'password_hash', pw_hash) if not ok: return False redis.hset('users:{}'.format(username), 'score', 0) redis.rpush('users', username) return user
def surrend(message): try: redis.hset(message.from_user.username, 'state', 'finish') bot.reply_to(message, ' شانس خود را از دست دادی {}'.format( message.from_user.first_name), reply_markup=markup) except: pass
def sendtext(): url = request.form['url'] ID = request.form['ID'] user = request.form['user'] redis.hset(user, ID, url) # リスト内包表記でサクッと取得&整形 return jsonify(res='ok')
def update_tweet_history(self, url): if isinstance(url, list): urls = url else: urls = [url] redis = self.redis_client() for u in urls: redis.hset(REDIS_HASH_NAME, u, 1)
def remove_member(number, sourceId): if redis.scard(number) == 1: redis.srem(number, sourceId) redis.delete(number + '_member') else: redis.srem(number, sourceId) redis.hset(sourceId, 'current', '-') redis.hset(sourceId, 'voted', 'N') redis.hdel(sourceId, 'status')
def serialize_dict(type_name, dictionary): hashcode = str(uuid4()) namehash = "%s:%s" % (type_name, hashcode) metahash = "%s:linked_fields:%s" % (type_name, hashcode) for key in dictionary: try: dictionary[key].__iter__ redis.hset(metahash, key, serialize(type_name, dictionary[key])) except Exception, e: redis.hset(namehash, key, dictionary[key])
def countRateOfAgent(redis, agentId, roomcardNumber, unitPrice, lowerRate=0): curTime = datetime.now() date = curTime.strftime("%Y-%m-%d") parentTable = AGENT_TABLE % (agentId) parentType, parentrate, parentId = redis.hmget( parentTable, ('type', 'shareRate', 'parent_id')) if not parentrate: AGENT_RATE_TABLE = AGENT_COMPAY_RATE_DATE % (agentId, date) else: AGENT_RATE_TABLE = AGENT_RATE_DATE % (agentId, parentrate, unitPrice, date) redis.hincrby(AGENT_RATE_TABLE, 'number', amount=roomcardNumber) redis.hset(AGENT_RATE_TABLE, 'unitPrice', unitPrice) redis.hset(AGENT_RATE_TABLE, 'rate', parentrate) if parentType == '1': RemainPrice = float(unitPrice) - float(lowerRate) if RemainPrice > 0: redis.hincrbyfloat(AGENT_RATE_TABLE, 'rateTotal', amount=RemainPrice * roomcardNumber) else: remainPrice = float(unitPrice) - float(lowerRate) if remainPrice <= 0: countRateOfAgent(redis, parentId, roomcardNumber, unitPrice, lowerRate) else: parent1Type, parent1Rate = redis.hmget(AGENT_TABLE % (parentId), ('type', 'shareRate')) if parent1Type != '1': if parentrate > parent1Rate: parentrate = parent1Rate firstRemainPrice = float(unitPrice) - float(parentrate) if firstRemainPrice <= 0: Rate = float(unitPrice) - float(lowerRate) redis.hincrbyfloat(AGENT_RATE_TABLE, 'rateTotal', amount=Rate * roomcardNumber) redis.hincrbyfloat(AGENT_RATE_TABLE, 'meAndNextTotal', amount=float(unitPrice) * roomcardNumber) countRateOfAgent(redis, parentId, roomcardNumber, unitPrice, parentrate) else: Rate = float(parentrate) - float(lowerRate) redis.hincrbyfloat(AGENT_RATE_TABLE, 'rateTotal', amount=Rate * roomcardNumber) redis.hincrbyfloat(AGENT_RATE_TABLE, 'meAndNextTotal', amount=float(parentrate) * roomcardNumber) redis.hincrbyfloat(AGENT_RATE_TABLE, 'superRateTotal', amount=firstRemainPrice * roomcardNumber) countRateOfAgent(redis, parentId, roomcardNumber, unitPrice, parentrate)
def redisKey(request): if request.method == 'GET': keyword = request.GET.get('keyword', '') elif request.method == 'POST': keyword = request.POST.get('keyword', '') keys = redis.hkeys(KEYWORDS) if keyword in keys: redis.hincrby(KEYWORDS, keyword, amount=1) else: redis.hset(KEYWORDS, keyword, 2) return HttpResponse()
def test(questiontype,user_id,questionid): #由於只有100題,所以超過之後回傳一個訊息 if (questionid==101): #並將它歸零 redis.hset(user_id,questionid,0) #回覆訊息 reply_message_list = [ TextSendMessage(text="Congratulation!!!!\nYou already finish 100 question about %s" % (questiontype)), ] return reply_message_list #api server接口位置 a = answer(questiontype,questionid) #這邊使用quick reply的方式 quickreply = TextSendMessage( text='Choose your answer:', quick_reply=QuickReply( items=[ QuickReplyButton( #使用postback action類似按鈕的概念 action=PostbackAction(label="A", #這邊使用true_answer()來幫助result data="type=answer&question_type=%s&question_id=%s&result=%s" % (questiontype,questionid,true_answer(a,'A')), text='choose:A' ) ), QuickReplyButton( action=PostbackAction(label="B", data="type=answer&question_type=%s&question_id=%s&result=%s" % (questiontype,a['question_id'],true_answer(a,'B')), text='choose:B' ) ), QuickReplyButton( action=PostbackAction(label="C", data="type=answer&question_type=%s&question_id=%s&result=%s" % (questiontype,a['question_id'],true_answer(a,'C')), text='choose:C' ) ), QuickReplyButton( action=PostbackAction(label="D", data="type=answer&question_type=%s&question_id=%s&result=%s" % (questiontype,a['question_id'],true_answer(a,'D')), text='choose:D' ) ) ])) #全部回傳的list reply_message_list = [ TextSendMessage(text=a["question_content"]), TextSendMessage(text=a["answer1_content"]+"\n\n"+a["answer2_content"]+"\n\n"+a["answer3_content"]+"\n\n"+a["answer4_content"]), #回傳quick reply選單 quickreply ] return reply_message_list
def test(questiontype, user_id, questionid): #由於只有100題,所以超過之後回傳一個訊息 if (questionid == 101): #並將它歸零 redis.hset(user_id, questionid, 0) #回覆訊息 reply_message_list = [ TextSendMessage( text= "Congratulation!!!!\nYou already finish 100 question about %s" % (questiontype)), ] return reply_message_list #api server接口位置 a = answer(questiontype, questionid) #這邊使用quick reply的方式 quickreply = TextSendMessage( text='Choose your answer:', quick_reply=QuickReply(items=[ QuickReplyButton( #使用postback action類似按鈕的概念 action=PostbackAction( label="A", #這邊使用true_answer()來幫助result data="type=answer&question_type=%s&question_id=%s&result=%s" % (questiontype, questionid, true_answer(a, 'A')), text='choose:A')), QuickReplyButton(action=PostbackAction( label="B", data="type=answer&question_type=%s&question_id=%s&result=%s" % (questiontype, a['question_id'], true_answer(a, 'B')), text='choose:B')), QuickReplyButton(action=PostbackAction( label="C", data="type=answer&question_type=%s&question_id=%s&result=%s" % (questiontype, a['question_id'], true_answer(a, 'C')), text='choose:C')), QuickReplyButton(action=PostbackAction( label="D", data="type=answer&question_type=%s&question_id=%s&result=%s" % (questiontype, a['question_id'], true_answer(a, 'D')), text='choose:D')) ])) #全部回傳的list reply_message_list = [ TextSendMessage(text=a["question_content"]), TextSendMessage(text=a["answer1_content"] + "\n\n" + a["answer2_content"] + "\n\n" + a["answer3_content"] + "\n\n" + a["answer4_content"]), #回傳quick reply選單 quickreply ] return reply_message_list
def handle_msg_all(self, msg): if not self.robot_switch and msg['msg_type_id'] != 1: return if msg['msg_type_id'] == 1 and msg['content'][ 'type'] == 0: # reply to self self.auto_switch(msg) elif msg['msg_type_id'] == 4 and msg['content'][ 'type'] == 0: # text message from contact is_schedule_auto_switch_cmd, flag = self.is_schedule_auto_switch_cmd( msg) if is_schedule_auto_switch_cmd: self.schedule_auto_switch(msg, flag) else: if self.is_schedule_status_open(msg): name = self.get_name_by_user_id(msg['user']['id']) redis.hset('schedule', name, msg['content']['data']) # self.contact_schedule_dict[msg['user']['id']] = msg['content']['data'] self.send_msg_by_uid( self.auto_reply(msg['user']['id'], msg['content']['data'], msg_from='contact'), msg['user']['id']) elif msg['msg_type_id'] == 3 and msg['content'][ 'type'] == 0: # group text message if 'detail' in msg['content']: my_names = self.get_group_member_name( msg['user']['id'], self.my_account['UserName']) if my_names is None: my_names = {} if 'NickName' in self.my_account and self.my_account[ 'NickName']: my_names['nickname2'] = self.my_account['NickName'] if 'RemarkName' in self.my_account and self.my_account[ 'RemarkName']: my_names['remark_name2'] = self.my_account['RemarkName'] is_at_me = False for detail in msg['content']['detail']: if detail['type'] == 'at': for k in my_names: if my_names[k] and my_names[k] == detail['value']: is_at_me = True break if is_at_me: src_name = msg['content']['user']['name'] reply = 'to ' + src_name + ': ' if msg['content']['type'] == 0: # text message reply += self.auto_reply(msg['content']['user']['id'], msg['content']['desc'], msg_from='group') else: reply += u"对不起,只认字,其他杂七杂八的我都不认识,,,Ծ‸Ծ,," self.send_msg_by_uid(reply, msg['user']['id'])
def sendStorjStatServer(status): try: redis.hset('NodeHB', status['node_id'], status) ts = int(time.time()) redis.hset(status['node_id'], ts, status) # url = 'http://' + Config.SERVER_IP +":" + str(Config.SERVER_PORT) + "/hb" # r = requests.post(url, data = status) # logging.debug(r.text) except Exception, e: logging.error(e) return False
def add_cart(): goods_id_list = request.form.getlist("goodsList[]") goods_count = request.form.getlist("goodsCount[]") res = zip(goods_id_list, goods_count) # 将数据保存在redis user_id = g.current_user.id redis = client_redis() redis.delete(user_id) for i in res: good = DishesInfoModel.query.filter_by(goods_id=i[0]).first() goods = dict(good) redis.hset(user_id, json.dumps(goods), i[1]) return jsonify({"status": "true", "message": "添加成功"})
def ResetUsedCache(redis, ranges): try: redis.delete("TODAY_USED_RANGE") for (k, v) in ranges.items(): if not v: continue s = json.dumps(v) redis.hset("TODAY_USED_RANGE", k, s) return True except Exception, e: print " RESET USED CACHE FAILED:%r" % e traceback.print_exc() return False
def update_user(self, member): """ 更新用户的信息到Redis中 :param member: 用户对象 """ redis_structure_name = Constant.MEMBER + Constant.COLON + member.ID member_value = {"ID": member.ID, "CELL_PHONE": member.cell_phone, "NICK_NAME": member.nick_name, "PASSWORD": member.password, "SESSION_ID": member.session_id, "LASTEST_LOGIN": member.lastest_login, "ACCOUNT_NUMBER": member.account_number, "GRADE": member.grade, "STATUS": member.status, "IS_ONLINE": member.is_online, "GENDER": member.gender, "PIC": member.pic, "EMAIL_ADDR": member.email_addr, "TYPE": member.type} # update the session id redis.hset(redis_structure_name, member_value)
def check(message, num): g = questionsdoc[questionlist[sighuplist[message.from_user.username] ['questionnumbers'][-2]]][num] if (time.time() - float(redis.hget(message.chat.username, 'timer')) ) == 45 or (time.time() - float(redis.hget(message.chat.username, 'timer'))) > 45: bot.send_message(message.chat.id, 'متاسفانه وقت شما تمام شد') elif g == questionsdoc[questionlist[sighuplist[ message.from_user.username]['questionnumbers'][-2]]][answerlist[ sighuplist[message.from_user.username]['questionnumbers'][-2]] - 1]: data1 = int(redis.hget(message.from_user.username, 'point')) redis.hset(message.from_user.username, 'point', data1 + 20) else: pass
def run(self): # Store a metablock global nb_thread time_start = time.time() tx_hash = store_var_contract.functions.addMetablockMap(self.body['key'], self.body['creation_date'], self.body['providers'], self.body['block_type'], self.body['checksum'], self.body['size'], self.body['entangled_with']).transact() time_mid = time.time() receipt = wait_for_receipt(w3, tx_hash, 0.1) redis.hset("blocks:{:s}".format(self.body['key']), "flag", 1) time_end = time.time() time_transaction = time_mid - time_start time_receipt = time_end - time_start #print("Thread finished for "+ self.body['key'] +" / Time receipt: "+str(time_receipt)) nb_thread = nb_thread - 1
def send_ui_reply(): sms_id = request.form.get('sms_id') sms_reply = request.form.get('action') print "Here I am: SMS Id - %s" % sms_id if ((sms_reply != "") and (not sms_id.startswith("del"))): src = redis.hget(sms_id,'From') plivo_did = redis.hget(sms_id,'Plivo DID') redis.hset(sms_id,"Replied",'y') redis.save() send_auto_response(src, plivo_did, sms_reply, 'True') elif ((sms_reply != "") and (sms_reply == "del")): redis.delete(sms_id[4:]) redis.save() return "OK"
def get_matches_for_champion(players, champ, begin_time=datetime.utcnow() - timedelta(weeks=1), cache_ignore=False): if not cache_ignore: keys = redis.hkeys('player_matches') if keys: champ_keys = [x for x in keys if "_{}".format(champ['id']) in x] if len(champ_keys) > 10: matches = [] for k,v in redis.hscan_iter('player_matches', '*_{}'.format(champ['id'])): if v: matches += json.loads(v) return matches matches = [] last_week = int(time.mktime(begin_time.timetuple())) * 1000 for region in players.keys(): for player in players[region]: if not cache_ignore: this_player = redis.hget('player_matches', "{}_{}".format(player['id'], champ['id'])) if this_player: print u"CACHE HIT - {}'s {} matches".format(player['name'], champ['name']).encode("utf-8") matches += this_player continue print u"NETWORK - {}'s {} matches".format(player['name'], champ['name']).encode("utf-8") this_player = [] page = riot.get_match_list(player['id'], region=region, champion_ids=champ['id'], ranked_queues='TEAM_BUILDER_DRAFT_RANKED_5x5', begin_time=last_week) while 'matches' in page.keys() and page['matches']: for m in page['matches']: if m['champion'] != champ['id'] or m['queue'] == 'CUSTOM': continue this_player.append({'lane': m['lane'], 'matchId': m['matchId'], 'region': m['region'], 'role': m['role']}) if len(this_player) == page['totalGames']: break time.sleep(1) print u"NETWORK INNER - {}'s {} matches".format(player['name'], champ['name']).encode("utf-8") page = riot.get_match_list(player['id'], region=region, champion_ids=champ['id'], ranked_queues='TEAM_BUILDER_DRAFT_RANKED_5x5', begin_time=last_week, begin_index=page['endIndex']) if this_player: redis.hset('player_matches', "{}_{}".format(player['id'], champ['id']), json.dumps(this_player)) matches += this_player time.sleep(2) return matches
def getbyinstance(args): conn = MySQLHandler(host=args['host'], port=args['port'], user=dbuser, pwd=dbpwd) sql = "show databases" dbs = conn.find(sql) print args['class'] for db in dbs: if db['Database'] in ('information_schema','test','mysql','performance_schema','percona'): continue print db['Database'] redis.hset(args['class'], db['Database'], '') sql = "show table status" conn.select_db(db['Database']) tbs = conn.find(sql) for tb in tbs: redis.hset(args['class'] + '.' + db['Database'], tb['Name'], '') k = args['class'] + '.' + db['Database'] + '.' + tb['Name'] v = tb['Rows'] redis.set(k,v) redis.set(tb['Name'], args['class'] + '.' + db['Database'] ) redis.set(db['Database'], args['class']) k = args['class'] + '.' + tb['Name'] redis.set(k,v) redis.set('rows_version', int(time.time())) findpksql = "select * from information_schema.COLUMNS where table_schema='%s' and EXTRA='auto_increment' and COLUMN_KEY='PRI' and table_name='%s'" % (db['Database'], tb['Name']) findpkdata = conn.find(findpksql) if len(findpkdata) == 0: pkname='' pktype='' pkid=0 else: pkname=findpkdata[0]['COLUMN_NAME'] pktype=findpkdata[0]['COLUMN_TYPE'] findpkidsql = "select * from information_schema.tables where table_schema='%s' and table_name='%s'" % (db['Database'], tb['Name']) findpkiddata = conn.find(findpkidsql) if len(findpkiddata) > 0: pkid=findpkiddata[0]['AUTO_INCREMENT'] else: pkid=0 sql = "insert into dba_stats.statics_table_rows (classname,dbname,tbname,rows, pkname, pktype, pkid) values ('%s','%s','%s','%s','%s','%s','%s')" % (args['class'], db['Database'], tb['Name'], tb['Rows'], pkname, pktype, pkid) logdb.execute(sql)
def test_hash(redis, keys, fields, retry): print "generating data..." for i in range(fields): redis.hset("key", ''.join([random.choice(string.ascii_letters + string.digits) for i in range(8)]), 1) for i in range(keys - 1): redis.hset(''.join([random.choice(string.ascii_letters + string.digits) for i in range(8)]), ''.join([random.choice(string.ascii_letters + string.digits) for i in range(8)]), 1) delays = [] print "testing..." for i in range(retry): t1 = datetime.datetime.now() redis.hgetall("key") t2 = datetime.datetime.now() td = t2 - t1 delays.append(td.days * 24 * 3600 * 1000 + td.seconds * 1000 + td.microseconds / 1000.0) result = pd.Series(delays) result.to_csv("hash_%d_%d.csv" % (fields, retry)) print result.describe()
def admin(): global gl_pass_code pass_code = gl_pass_code db_pass_code = redis.hget('temp_pass_code', 'pass_code') db_time_left = redis.hget('temp_pass_code', 'time_left') db_time_set = redis.hget('temp_pass_code', 'time_set') print db_time_set time_left = "Not Yet Set" if db_pass_code and db_pass_code != "": pass_code = db_pass_code time_left = 1200 - int(time.time() - float(db_time_set)) if int(time_left) <= 0: print time_left redis.hset('temp_pass_code','time_left','expired') time_left = "Expired!" response = make_response(render_template("sendsms_admin.html", pass_code = pass_code, time_left = time_left)) response.headers['Content-type'] = 'text/html' return response
def get_players(cache_ignore=False): if not cache_ignore: players_by_region = redis.hgetall('players_by_region') if players_by_region and len(players_by_region.keys()) == 3: players_by_region = {x: json.loads(y) for x,y in players_by_region.iteritems()} if all([len(y) > 0 for x,y in players_by_region.iteritems()]): return players_by_region players_by_region = {NORTH_AMERICA: [], KOREA: [], EUROPE_WEST: []} for region in players_by_region.keys(): players = [] challengers = riot.get_challenger(region=region) masters = riot.get_master(region=region) for p in challengers['entries'] + masters['entries']: player = {'name': p['playerOrTeamName'], 'id': p['playerOrTeamId']} players.append(player) redis.hset('players_by_region', region, json.dumps(players)) players_by_region[region] = players return players_by_region
def gen_pass_code(): global gl_pass_code gl_pass_code = uuid.uuid4() redis.hset('temp_pass_code','pass_code',gl_pass_code) redis.hset('temp_pass_code','time_left','1200.0') redis.hset('temp_pass_code','time_set',time.time())
def wrapper(*args, **kwargs): if len(args) == 1: handler = args[0] blog_id = handler.get_argument("blog_id", None) if not blog_id: blog_id = redis.zrange('blog.list', -1, -1) if blog_id: blog_id = redis.zrange('blog.list', -1, -1)[0] else: handler.write("请先在管理后台添加预发布的文章") handler.finish() return else: blog_id = args[1] blog = redis.hgetall(blog_id) if not blog: with open(utils.decrypt(blog_id), 'r') as p: lines = p.readlines() or [''] title = lines[0] create_at = int(os.path.getctime(utils.decrypt(blog_id)) or 1504724902) content = utils.md_parse(''.join(lines[2:-1])) redis.hset(blog_id, 'title', title) redis.hset(blog_id, 'create_at', create_at) redis.hset(blog_id, 'content', content) return func(*args, **kwargs)
def accept_and_reply(): text = request.form.get('Text','') src = request.form.get('From','') plivo_did = request.form.get('To','') time_sms_received = time.asctime(time.localtime(time.time())) sms_key = "sms_%s" % uuid.uuid4() redis.hset(sms_key,"From", src) redis.hset(sms_key,"Plivo DID", plivo_did) redis.hset(sms_key,"Text", text) redis.hset(sms_key,"Time Received", time_sms_received) redis.hset(sms_key,"Replied",'n') redis.save() send_auto_response(src,plivo_did,text) email_SMS(text,src,plivo_did) response = make_response("OK") response.headers['Content-type'] = 'text/html' return response
def __setitem__(self, key, value): self.has_changed = True self.__delitem__(key) try: value.__iter__ if type(key) == IntType: redis.lset(self.root_node[0], key, serialize(self.type_name, value)) redis.rpush(self.root_node[1], key) else: redis.hset(self.root_node[1], key, serialize(self.type_name, value)) except Exception, e: if type(key) == StringType: if type(value) == FloatType or type(value) == IntType: redis.hset(self.root_node[0], key, value) else: redis.hset(self.root_node[0], key, value) else: if type(value) == FloatType or type(value) == IntType: redis.lset(self.root_node[0], key, value) else: redis.lset(self.root_node[0], key, value)
def set_title(job_id, url): """Sets the job's page_title field using the url's <title>.""" r = requests.get(url) tree = lxml.html.fromstring(r.content) title = tree.findtext('.//title')[:-10] # Removing suffix: " - YouTube" redis.hset(jobkey(job_id), 'page_title', title)
# File: passwords.py --- IMPORTANT only a template file # # fill in correct data # run first thing after startup # we are assuming that mongodb, redis.io, and neo4j are # set up with no athenication and run locally on the target machine # if not then addition fields will have to be setup # ---- IMPORTANT: Donot put the actual version of this file in # the configuration, ie, not in new_python directory tree import redis import json redis = redis.StrictRedis( host = "127.0.0.1", port=6379, db = 1 ) # need to establish free account on Send Grid redis.hset("MEDIA_DRIVERS","SendGrid",json.dumps({"user_name":"xxxx","password":'******',"test_user":"******"})) # need to establish free account on trillo redis.hset("MEDIA_DRIVERS","Trillo",json.dumps({"accountSID":"xxx","authToken":'xxx',"twilio_phone_number":"1xxxxxxx","test_number":"xxxx"})) # need to install package tweepy # need to go to tweeter app and configure the app and install it into the ranch account redis.hset("MEDIA_DRIVERS","TWITTER",json.dumps({"title":"xxxx", 'consumer_key':"xxxxx", # generated by twitter 'consumer_secret':"xxxxx", # generated by twitter 'access_token':"xxxx", # generated by twitter 'access_token_secret':"xxxx"})) redis.hset("MEDIA_DRIVERS","TRELLO",json.dumps({"title":"xxxx", 'key':"xxxxx", 'secret_key':"xxxxx", "token":"xxxxx", "application_name":"xxxxx", "user_name":"xxxxx"
while True: if last_update == None: updates = bot.getUpdates() else: updates = bot.getUpdates(offset=last_update) for u in updates: user = u.message.from_user.first_name message = u.message.text chat_id = u.message.from_user.id if not redis.hexists('bot:session:'+str(chat_id), 'user'): #start session print 'new session!!!' redis.hset('bot:session:'+str(chat_id), 'user', user) wall_id = None #check for commands words = message.split(' ') if words[0] == '/start': print 'COMMAND START' if len(words) > 1: wall_name = words[1] #find wall name wall_id = wall_name redis.hset('bot:session:'+str(chat_id), 'wall_id', wall_id) bot.sendMessage(chat_id=chat_id, text="Welcome to our LED Wall! You can start sending text or images right now to wall " + str(wall_id)) elif words[0] == '/wall' and len(words) == 2:
def _seve_last_tid(self): redis.hset('mtgox', 'last_date', self._get_last_date())
def hset(self, name, key, value): redis = self._get_redis(name) return redis.hset(name, key, value)
def Update(key,item,val,redis): d,lst = Query() if key in d.keys() and redis.hexists(key,item): old_val = redis.hget(key,item) redis.hset(key,item,val) r.publish(src[0],src[1])
def modify_properties( self, redis_key, new_properties): for i in new_properties.keys(): redis.hset(redis_key,i, new_properties[i] )
# # # File: passwords.py # This is an example file. # Execute once during startup # Move out of distro directory # import redis import json redis = redis.StrictRedis( host = "127.0.0.1", port=6379, db = 1 ) redis.hset("gateway", "user_name", 'xxxxx' ) redis.hset("gateway", "password", 'xxxxx') redis.hset("gateway", "vhost", 'xxxxx' ) redis.hset("gateway", "queue", 'xxxx') redis.hset("gateway", "port", 5671 ) redis.hset("gateway", "server", 'xxxxx' ) redis.hset("alert", "user_name", 'xxxxx' ) redis.hset("alert", "password", 'xxxxx') redis.hset("alert", "vhost", 'xxxxx' ) redis.hset("alert", "queue", 'xxxx') redis.hset("alert", "port", 5671 ) redis.hset("alert", "server", 'xxxxx' ) redis.hset("web","crt_file", 'xxxxx')
tfidf = CountVectorizer(tokenizer=get_tokens, stop_words='english') tfs = tfidf.fit_transform(token_dict.values()) #redis_pipe = redis.pipeline() freq_hash = {} idf_hash = {} feature_names = tfidf.get_feature_names() for col in tfs.nonzero()[1]: if feature_names[col] in freq_hash: freq_hash[feature_names[col]] += 1 else: freq_hash[feature_names[col]] = 1 print len(freq_hash) print 'article count', article_count for k in freq_hash: idf_hash[k] = math.log(article_count/(freq_hash[k])) redis.hset('tfidf', k, idf_hash[k]) print idf_hash print 'done'
# delete SYS FILES DATA if len(sys_list) > 0: redis.hdel("FILES:SYS", sys_list) files = [f for f in listdir(app_files)] # load app files for i in files: fileName, fileExtension = os.path.splitext(i) if fileExtension == ".json": f = open(app_files + i, "r") data = f.read() data = base64.b64encode(data) redis.hset("FILES:APP", i, data) # load sys files files = [f for f in listdir(sys_files)] for i in files: print "i", i fileName, fileExtension = os.path.splitext(i) if fileExtension == ".json": f = open(sys_files + i, "r") data = f.read() data = base64.b64encode(data) redis.hset("FILES:SYS", i, data) print "data", "done" ####
def errors(conditions, paging): print 'brquery.errors()' print "conditions:" for k in conditions: print "%s=%s"%(k,conditions[k]) print "paging:" for k in paging: print "%s=%s"%(k,paging[k]) #Check token if 'token' in conditions: token=conditions.pop('token') else: return {"error":{"code":12,"msg":"Invalid token"}} redis = brconfig.getRedis() pipe = redis.pipeline(transaction=True) pagingCached=False pagingSet=None page=int(paging['page']) records=int(paging['records']) totalRecords=None paging_token=None if 'paging_token' in paging: paging_token=paging['paging_token'] pagingSet="tmp_paging_%s"%paging_token if redis.exists(pagingSet): pagingCached=True ret = [] if not pagingCached: print "not paging!" sets = [] #temporary set names accessibleSet='tmp_%s'%str(uuid.uuid4()) timeFilteredSet='tmp_%s'%str(uuid.uuid4()) appSet='tmp_%s'%str(uuid.uuid4()) resultIdsSet='tmp_%s'%str(uuid.uuid4()) paging_token=str(uuid.uuid4()) pagingSet="tmp_paging_%s"%paging_token redis.hset("paging_token",paging_token,int(time.time())+30*60) #Get accessible set accessibleSet=getAccessibleSet(token,accessibleSet) if accessibleSet==None: print "Accessible Set is None!" return ret else: sets.append(accessibleSet) print "accessible sets[]:%s"%sets #Get time filtered set starttime=0 endtime=0 ntf=False if 'starttime' in conditions: starttime=conditions.pop('starttime') ntf=True if 'endtime' in conditions: endtime=conditions.pop('endtime') ntf=True if ntf: timeFilteredSet=getTimeFilteredSet(starttime,endtime,timeFilteredSet) if timeFilteredSet==None: print "Time Filtered Set is None!" return ret else: sets.append(timeFilteredSet) print "filtered sets[]:%s"%sets #For error type if 'e_type' in conditions: sets.append('ids:b:%s' % conditions.pop('e_type')) else: sets.append('ids:e')#All errors without call crop. #Other query conditions for k, v in conditions.items(): if k == 'name': sets_app = [] sets_app.append('ids:b:FORCE_CLOSE:%s' % v) sets_app.append('ids:b:ANR:%s' % v) sets_app.append('ids:b:CORE_DUMP:%s' % v) sets_app.append('ids:b:MANUALLY_REPORT:%s' % v) pipe.sunionstore(appSet,sets_app) result=pipe.execute() if result[0]==0: return [] else: sets.append(appSet) else: sets.append('ids:i:%s:%s' % (k,v)) print "last sets[]:%s"%sets #Intersect all the sets #TODO: Is here the best place to retrieve all the set members? pipe.sinterstore(pagingSet,sets) pipe.sort(pagingSet,desc=True,start=(page-1)*records,num=records) result=pipe.execute() totalRecords=result[0] ret=list(result[1]) print "totalRecords:%s"%totalRecords print "pagingSet:%s"%pagingSet #Delete all temporary set pipe.delete(accessibleSet) pipe.delete(timeFilteredSet) pipe.delete(appSet) pipe.delete(resultIdsSet) pipe.execute() else: print "has paging!" pipe.hset("paging_token",paging_token,int(time.time())+30*60) pipe.card(pagingSet) pipe.sort(pagingSet,desc=True,start=(page-1)*records,num=records) result=pipe.execute() totalRecords=result[1] ret=list(result[2]) if len(ret)==0: print "Return set is empty!" return {} recordList=proxy.records(ret,token) print "Records:%s"%recordList paging['totalrecords']=totalRecords remainder=totalRecords%records if remainder>0: paging['totalpages']=totalRecords/records+1 else: paging['totalpages']=totalRecords/records paging['paging_token']=paging_token results={'paging':paging,'data':recordList} return results
flat_d = tune.flat_d(tune.get_id3(), flat_d) if len(flat_d["album"]) is not 1: continue # @todo log {{path}}... album = flat_d["album"][0] artist = "VA" if len(flat_d["artist"]) is not 1 else flat_d["artist"][0] genre = "" if len(flat_d["genre"]) is not 1 else flat_d["genre"][0] year = "" if len(flat_d["year"]) is not 1 else flat_d["year"][0] album_id3 = {"album": album, "artist": artist, "genre": genre, "year": year} album_key = RdsAlbum.get_key(artist, album) album_id3_key = RdsAlbum.get_id3_key(album_key) for key, value in album_id3.iteritems(): redis.hset(album_id3_key, key, value) album_tunes_key = RdsAlbum.get_tunes_key(album_key) album_images_key = RdsAlbum.get_images_key(album_key) redis.sadd(albums_key, album_key) for tune in tunes: album, title, artist = tune.id3gw.get_album(), tune.id3gw.get_title(), tune.id3gw.get_artist() if not album or not title or not artist: continue # @todo log {{path}}... tune_key = RdsTune.get_key(album, title, artist) tune_id3_key = RdsTune.get_id3_key(tune_key) tune_audio_key = RdsTune.get_audio_key(tune_key)
def turn_off_spider(oj_name): if redis.exists(switch_key): redis.hset(switch_key, oj_name, 0) log_spider_status()
def save_matches_info(matches, champ): for m in matches: if "{}_{}_{}".format(m['region'], m['matchId'], champ['id']) in redis.hkeys('match_details'): continue match = redis.hget('match_infos', '{}_{}'.format(m['region'], m['matchId'])) if match: match = json.loads(match) else: match = riot.get_match(m['matchId'], region=m['region'].lower(), include_timeline=True) redis.hset('match_infos', '{}_{}'.format(m['region'], m['matchId']), json.dumps(match)) participantId = None participant = None for p in match['participants']: if p['championId'] == champ['id']: participant = p participantId = p['participantId'] break team = None for t in match['teams']: if t['teamId'] == participant['teamId']: team = t break won = team['winner'] stats = participant['stats'] kills, deaths, assists = stats['kills'], stats['deaths'], stats['assists'] first_blood = stats['firstBloodKill'] first_blood_assist = stats['firstBloodAssist'] tanks_friendly_team = 0 tanks_enemy_team = 0 lane_partner = None for p in match['participants']: friendly = p['teamId'] == participant['teamId'] tags = champion_data_by_id[p['championId']] if 'Tank' in tags: if friendly: tanks_friendly_team += 1 else: tanks_enemy_team += 1 if friendly: continue lane, role = None, None for t in p['timeline']: if 'lane' not in t or 'role' not in t: continue lane = t['lane'] role = t['role'] break if not (lane and role): continue raise ValueError('{}, {}'.format(lane, role)) if lane == m['lane'] and role == m['role']: lane_partner = p break if lane_partner: lane_partner_champ = champion_data_by_id[lane_partner['championId']] lane_partner_ad = lane_partner_champ['info']['attack'] > lane_partner['info']['magic'] else: lane_partner_ad = False items = [] for e in match['timeline']['frames']: if 'events' not in e.keys(): continue for ev in e['events']: if ev['eventType'] == 'ITEM_PURCHASED' and ev['participantId'] == participantId: # I don't care about biscuits or health potions or wards or trinkets if ev['itemId'] in (2003, 2010, 2043, 3340, 3341, 3361, 3362, 3363, 3364): continue item_info = item_data[ev['itemId']] # Don't care about base items, only upgrades if 'depth' not in item_info: continue # Don't care about level 3 boot upgrades if 'group' in item_info and 'boots' in item_info['group'].lower(): continue items.append(ev['itemId']) trimmed_items = [] for i, item_id in enumerate(items): item = item_data[item_id] prev_items = trimmed_items[:] if prev_items and 'from' in item: # if the last few items all build into this item, but they're # different parts of the tree (i.e. they don't upgrade into each other) # we shouldn't have the entire build path in the item list from_items = item['from'] last_item = prev_items.pop() while str(last_item) in from_items: if 'from' in item_data[last_item]: from_items += item_data[last_item]['from'] trimmed_items.remove(last_item) if not prev_items: break last_item = prev_items.pop() prev_items = trimmed_items[:] if prev_items and 'from' in item: # if the N-1th or N-2nd item is something that upgrades into this, skip it last_item = prev_items.pop() last_last_item = prev_items.pop() if prev_items else None if str(last_item) in item['from']: trimmed_items.remove(last_item) if str(last_last_item) in item['from']: trimmed_items.remove(last_last_item) trimmed_items.append(item_id) items = trimmed_items details = { 'championId': champ['id'], 'won': won, 'duration': match['matchDuration'], 'kills': kills, 'deaths': deaths, 'assists': assists, 'first_blood_kill': first_blood, 'first_blood_assist': first_blood_assist, 'lane_enemy_ad': lane_partner_ad, 'purchases': items } redis.hset('match_details', "{}_{}_{}".format(m['region'], m['matchId'], champ['id']), json.dumps(details)) print "Parsed and saved match {}\n".format(m['matchId']) pprint(details) print "-" * 30