def save(proxies): for proxy in proxies: redis.set(Proxy._get_redis_key(str(proxy)), json.dumps(proxy._proxy_info)) redis.sadd(Proxy._get_set_key(), str(proxy)) redis.zadd(Proxy._get_sorted_set_key(), {str(proxy): int(datetime.now().timestamp())})
def start(m): s = bot.get_chat_member(channel, m.chat.id) mn = redis.sismember("bans", m.from_user.id) if mn == False: if s.status == "member" or s.status == "creator" or s.status == "administrator": markup = types.InlineKeyboardMarkup() a = types.InlineKeyboardButton("🇮🇷فارسی🇮🇷", callback_data='pars') b = types.InlineKeyboardButton("🇬🇧English🇬🇧", callback_data='eng') markup.add(a, b) bot.send_message( m.chat.id, "🇮🇷لطفا زبان خود با انتخاب کنید :\n➖➖➖➖➖➖➖➖\n🇬🇧Please select your language :", reply_markup=markup) else: markup = types.InlineKeyboardMarkup() start4 = types.InlineKeyboardButton( "👈 عضویت در کانال 👉", url="https://telegram.me/joinchat/AAAAAEC-CTpnz_0J0dplCg") markup.add(start4) bot.send_message( m.chat.id, "⚠️ کاربر عزیز جهت استفاده از این ربات ابتدا باید در کانال ما عضو شوید.\n\n🔅 برای عضویت در کانال بر روی لینک زیر کلیک کنید و دکمه join را بزنید👇\n<a href='https://telegram.me/joinchat/AAAAAEC-CTpnz_0J0dplCg'>@tgMember</a>\n<a href='https://telegram.me/joinchat/AAAAAEC-CTpnz_0J0dplCg'>@tgMember</a>\n\nپس از عضویت در کانال بر روی 👈 /start کلیک کنید تا ربات فعال شود!", parse_mode="HTML", reply_markup=markup) redis.sadd('mbrs_bot', '{}'.format(m.from_user.id))
def process_links(project, url, spider): if redis.sismember("seen", json.dumps([project, url, spider])): print "Skipping %s; it has already been processed (For %s)" % (url.encode("utf-8"), project.encode("utf-8")) else: print "Processing %s for urls (For %s)" % (url.encode("utf-8"), project.encode("utf-8")) resp = session.get(url, timeout=15) resp.raise_for_status() html = lxml.html.document_fromstring(resp.content) if spider: for link in itertools.chain(html.find_rel_links("download"), html.find_rel_links("homepage")): try: link.make_links_absolute(url) except ValueError: continue if "href" in link.attrib and not installable(project, link.attrib["href"]): parsed = urlparse.urlparse(link.attrib["href"]) if parsed.scheme.lower() in ["http", "https"]: redis.rpush("queue", json.dumps([project, link.attrib["href"], False])) # Process all links in html for installable items for link in html.xpath("//a"): try: link.make_links_absolute(url) except ValueError: continue if "href" in link.attrib and installable(project, link.attrib["href"]): redis.rpush("results", json.dumps([project, url, link.attrib["href"]])) redis.sadd("seen", json.dumps([project, url, spider]))
def _all_(m): if m.chat.type == "group" or m.chat.type == "supergroup": uid = m.from_user.id cid = m.chat.id uname = m.from_user.first_name or "ادمین" status = redis.get(key + str(cid) + ":status") or "off" if bot.get_chat_member(cid, uid).status == "member": if status == "on": if redis.sismember(key + str(cid) + ":allow_list", uid) == False: bot.delete_message(cid, m.message_id) max_add = redis.get(key + str(cid) + ":max_add") or 3 if redis.sismember(key + str(cid) + ":warn_list", uid) == False: text = """کاربر [{}](tg://user?id={}): برای چت کردن در این گروه باید 3 نفر را به گروه دعوت کنید!""".format( uname, uid, max_add) bot.send_message(cid, text, parse_mode="Markdown") redis.sadd(key + str(cid) + ":warn_list", uid) else: if m.text == "/panel": bot.delete_message(cid, m.message_id) text = "[{}](tg://user?id={}):\nبا دکمه های زیر حد اکثر تعداد دعوت ممبر ها را تایین کنید🎈😁".format( uname, uid) bot.send_message(cid, text, parse_mode="Markdown", reply_markup=panel(cid))
def route_post_ad(slot): if not advertiser_id(): return '', 404 asset = request.files['asset'] id = next_ad_id() key = ad_key(slot, id) type = fetch(request.form, 'type') if not type: type = asset.mimetype if not type: type = 'video/mp4' redis = get_redis() redis.hmset( key, { 'slot': slot, 'id': id, 'title': fetch(request.form, 'title'), 'type': type, 'advertiser': advertiser_id(), 'destination': fetch(request.form, 'destination'), 'impressions': 0 }) redis.set(asset_key(slot, id), asset.read()) redis.rpush(slot_key(slot), id) redis.sadd(advertiser_key(advertiser_id()), key) return jsonify(get_ad(slot, id))
def search_request(message): # Create request request = { "id": message.id, "type": "comment" if message.was_comment else "message", "author": str(message.author), "link": f"https://www.reddit.com{message.context}" if message.was_comment else f"https://www.reddit.com/message/messages/{message.id}", "retries": 0 } # Check for duplicates util.already_processed_check(redis, request) lock = util.get_lock(request['id']) if redis.exists(lock): raise CurrentlyProcessing(request['link']) # Lock request to avoid duplicates redis.set(lock, "") request_json = json.dumps(request) # Enqueue for filtering redis.sadd(config['REDIS_REQUESTS_FILTER'], request_json) message.mark_read() logging.info(f"Found new request {request['id']} : {request['link']}.")
def main(): request_json = redis.spop(current_set) if request_json: request = json.loads(request_json.decode('utf-8')) try: reply_to_request(request) except InvalidRequest as ir: util.open_lock(redis, request['id']) logging.info(ir) except AlreadyProcessed as ap: util.open_lock(redis, request['id']) logging.error(ap) except CommentingFailed as cf: util.handle_failed_request(redis, request, current_set, cf) logging.error(cf) except RedditAPIException as rae: if "NOT_WHITELISTED_BY_USER_MESSAGE" in str(rae): redis.sadd(config['REDIS_NOT_WHITELISTED_USERS'], request['author']) util.open_lock(redis, request['id']) logging.error( f"User {request['author']} needs to whitelist me. Adding to {config['REDIS_NOT_WHITELISTED_USERS']}" ) else: util.handle_failed_request(redis, request, current_set, rae) logging.error( f"{type(rae).__name__} occurred while replying to request {request['id']} : {request['link']} : {rae}." ) except Exception as e: util.handle_failed_request(redis, request, current_set, e) logging.error( f"{type(e).__name__} occurred while replying to request {request['id']} : {request['link']} : {e}." )
def callback(ch,method,properties,body): print ("[x] Received %s" %(body.decode('utf-8'),)) strPara = str(body.decode('utf-8')) d = json.loads(strPara) print(" process %s " % (d['batFileName'],)) batFile =batFilePath+ str(d['batFileName'])+'.bat' dbname=str(d['dbName']) dbip =str(d['dbip']) uid =str(d['dbUser']) pwd =str(d['dbPwd']) exp = expFilePath+str(d['expFileName'])+'.csv' try: if os.path.isfile(batFile): cmd = batFile+' '+dbname+' '+exp+' '+dbip+' '+uid+' '+pwd os.system(cmd) #return os shell cmd result #stream = os.popen(cmd) print(" [x] %s Done" % (batFile,)) channel.basic_ack(delivery_tag=method.delivery_tag) else: print(" %s is not exist "%(batFile,)) #logging.info('bat: %s not get %s '%((batFile+dbname),exp)) redis.sadd('NoExport',batFile+'-'+dbname) channel.basic_ack(delivery_tag=method.delivery_tag) except e: print(e)
def doPatchSign(redis, account, date): """ 补签接口 补签需要消耗2张房卡 """ user_info = get_user_info(redis, account) uid = user_info['uid'] agentid = user_info['parentAg'] roomcard = redis.get(USER4AGENT_CARD % (agentid, uid)) roomcard = int(roomcard) if roomcard else 0 if roomcard < PATCH_SIGN_FEE: return {'code': 1, 'msg': "钻石不足"} month = datetime.strptime(date, '%Y-%m-%d').month key = WELFARE_USER_PATCH_SIGN % (account, month) if redis.sismember(key, date): return {'code': 1, 'msg': "已补签过"} times = redis.scard(key) # 每月补签次数是否达到上限 if times and int(times) > PATCH_SIGN_MAX: return {'code': 1, 'msg': "当月补签已达上限"} redis.sadd(key, date) return {'code': 0, 'msg': "补签成功"}
def getInfoBySid(redis, sid): """ 通过Sid获取SessionTable, account, uid, verfiySid信息 """ SessionTable = FORMAT_USER_HALL_SESSION % (sid) # 判断SID是否生效 if not SessionTable: client_ip = request.environ.get('HTTP_X_FORWARDED_FOR') redis.hincrby("access:number:ip:hset", client_ip, 1) print(u"远程地址:%s 头部:%s" % (client_ip, request.headers)) if not client_ip or client_ip == "183.60.133.160": return None, None, None, None number = redis.hget("access:number:ip:hset", client_ip) number = int(number) if number else 0 if number >= 3: redis.sadd("remote:disbled:ip:set", client_ip) return None, None, None, None account, uid = redis.hmget(SessionTable, ('account', 'uid')) verfiySid = redis.get(FORMAT_USER_PLATFORM_SESSION % (uid)) curTime = getNowTime() log_util.debug( '[%s][SessionTable][info] account[%s] sessionKey[%s] verfiyKey[%s]' % (curTime, account, sid, verfiySid)) return SessionTable, account, uid, verfiySid
def on_data(self, data): d = json.loads(data) print("The twitter gods gave us new data from", d["user"]["screen_name"]) point = 0 for potential_point in d["entities"]["hashtags"]: p = potential_point["text"].lower() if p == "1hp": point = 1 elif p == "2hp": point = 2 elif p == "5hp": point = 5 elif p == "10hp": point = 10 elif p == "25hp": point = 25 elif p == "50hp": point = 50 if point > 0: user = d["user"]["screen_name"] userdata = { "name": d["user"]["screen_name"], "fullname": d["user"]["name"], "pic": d["user"]["profile_image_url"], "src": "twitter" } print("Adding new score by", userdata) redis.sadd(user, json.dumps(userdata)) redis.zincrby("leaderboard", user, point) return True
def check_pm(m): if m.chat.type == 'supergroup': redis.sadd(m.content_type, m.message_id) if redis.sismember('groups', m.chat.id): if not redis.get('expire' + str(m.chat.id)): bot.reply_to(m, '🔴 اعتبار گروه به پایان رسیده است 🔴') redis.srem('groups', m.chat.id) bot.leave_chat(m.chat.id) else: check(m) if m.chat.type == 'private': if m.reply_to_message.forward_from: if is_sudo(m.from_user.id): id = m.reply_to_message.forward_from.id if m.text == 'مسدود': redis.set('ban' + str(id), True) bot.reply_to( m, 'این کاربر دیگر قادر به استفاده از پیوی ربات نیست ❌') elif m.text: bot.send_message(id, m.text) bot.reply_to(m, 'رفت براش') elif m.sticker: bot.send_sticker(id, m.sticker.file_id) bot.reply_to(m, 'رفت براش') elif m.photo: bot.send_photo(id, m.photo[-1].file_id) bot.reply_to(m, 'رفت براش')
def parse_result(resp): for insta in resp['data']: if redis.sismember("insta", insta.id): print("Mr T says: No new fun for you!") break else: print("We found ourselfs new data from", insta.user.username) point = 0 for potential_point in insta.tags: p = potential_point.name.lower() if p == "1hp": point = 1 elif p == "2hp": point = 2 elif p == "5hp": point = 5 elif p == "10hp": point = 10 elif p == "25hp": point = 25 elif p == "50hp": point = 50 if point > 0: user = insta.user.username userdata = {"name":insta.user.username,"fullname":insta.user.full_name, "pic": insta.user.profile_picture, "src":"insta"} print("Adding new score by", userdata) redis.sadd(user, json.dumps(userdata)) redis.zincrby("leaderboard", user, point) redis.sadd("insta", insta.id)
def saveBuyGoldRecord(redis, account, data): """ 保存金币流水 """ try: if not redis.sismember(GOLD_ACCOUNT_SET_TOTAL, account): redis.sadd(GOLD_ACCOUNT_SET_TOTAL, account) prredis = getPrivateRedisInst(redis, MASTER_GAMEID) num = prredis.incr(GOLD_BUY_RECORD_COUNT_TABLE) record_key = GOLD_BUY_RECORD_TABLE % num pipe = prredis.pipeline() data['account'] = account pipe.hmset(record_key, data) pipe.expire(record_key, GOLD_ROOM_MAX_TIME) pipe.lpush(GOLD_BUY_RECORD_ACCOUNT_LIST % account, record_key) pipe.lpush(GOLD_BUY_RECORD_LIST_TOTAL, record_key) pipe.incr(GOLD_BUY_RECORD_ACCOUNT_GOLD_SUM % account, data['gold']) pipe.incr(GOLD_BUY_RECORD_ACCOUNT_MOENY_SUM % account, data['money']) pipe.execute() user_info = get_user_info(redis, account) if not user_info: return agentid = user_info['parentAg'] gold = user_info['gold'] prredis.zadd(GOLD_MONEY_RANK_WITH_AGENT_ZSET % agentid, account, gold) except Exception, ex: traceback.print_exc()
def init(queue): count = 0 while count< 5000: count +=1 try: header = {'User-Agent': 'Mozilla/5.0'} url = queue.pop() # print count if not redis.exists("%s:%s" % (key, url)): # print "crawling %s ... " % (url) req = urllib2.Request(url, headers=header) page = urllib2.urlopen(req) soup = BeautifulSoup(page) links = soup.find_all("a") ret_links = get_in_links(url, links) queue = ret_links + queue redis.set("%s:%s" % (key, url), 1) redis.sadd(key, url) dict = parse(url, soup) if dict: print dict else: # print "%s already crawled ....." % (url) pass except: print "error ........." pass
def personal(msg): try: if msg.from_user.id == naji and msg.text: if re.match("/fwd", msg.text) and msg.reply_to_message: for i in redis.smembers("bot:all", msg.chat.id): try: bot.forward_message(i, msg.chat.id, msg.reply_to_message.id) except Exception as e: print(e) if re.match("/stats", msg.text): bot.send_message( msg.chat.id, "آمار\nگروه ها : {}\nخصوصی ها : {}".format( redis.scard("bot:gps"), redis.scard("bot:pvs"))) else: bot.send_message( msg.chat.id, "رباتی برای افزایش اعضای گروه\nفقط کافیه به گروهت دعوتش کنی و ادمینش کنی" ) if not redis.sismember("bot:all", msg.chat.id): redis.sadd("bot:pvs", msg.chat.id) redis.sadd("bot:all", msg.chat.id) except Exception as e: print(e) pass
def verify_ip(self, proxy_ip): print("开始代理IP检测...") proxies = { "http": "http://%(proxy)s/" % { "proxy": proxy_ip }, "https": "http://%(proxy)s/" % { "proxy": proxy_ip }, } try: resp = requests.get('https://mockbin.org/request', proxies=proxies, timeout=1) if resp.status_code == 200: print(resp.json()) print("代理IP {} 检测成功, 入库中...".format(proxy_ip)) redis.sadd("PROXY_IPS", proxy_ip) return False except Exception as err: print(err) print("代理IP {} 检测已失效, 清理中...".format(proxy_ip))
def post(self): ip = self.get_body_argument('ip', default=None, strip=False) port = self.get_body_argument('port', default='3244', strip=False) name = self.get_body_argument('name', default=None, strip=False) key = self.get_body_argument('key', default=None, strip=False) sign = self.get_body_argument('sign', default=None, strip=False) logger.info( '----get_proxy_ip: ip:{}, port:{}, name:{}, key:{}, sign:{}'. format(ip, port, name, key, sign)) if sign == get_sign(key) and ip: proxy = ip + ':' + port redis.set('proxy_' + name, proxy) redis.sadd('use_ips', ip) response = { 'message': 'SUCCESS', 'reason': '', } elif sign != get_sign(key): response = { 'message': 'FALSE', 'reason': 'Wrong Token', } else: response = { 'message': 'FALSE', 'reason': 'No Client ip', } self.write(json.dumps(response, ensure_ascii=False))
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 add_user(message): try: user_id = int(message.text) except ValueError: bot.send_message(message.chat.id, 'Вы неправильно ввели user id') else: redis.sadd('allowed_users_id', user_id) ALLOWED_BARS_USER_IDS.add(user_id)
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 login(self, cell_phone_number): redis_structure_name = Constant.MEMBER + cell_phone_number member_id = self.cell_phone_number_exist(cell_phone_number) if member_id == 0: raise UserNotExistException # 用户存在,直接放在Redis的对象中 redis.sadd(redis_structure_name, cell_phone_number) return True
def get_next_callee(): index = redis.incr(CALLEE_COUNTER_KEY) - 1 callee = CALLEES[index] if redis.sismember(CALLED_NUMBERS_SET_KEY, callee['phone']): store_event('skipped_repeat_number', callee) return get_next_callee() else: redis.sadd(CALLED_NUMBERS_SET_KEY, callee['phone']) return index, callee
def set_cards_participant(hash_position: str) -> None: """ Cria as cartelas do participante. Parameters: hash_position: número do usuário. """ while redis.scard(CARD + hash_position) < 15: redis.sadd(CARD + hash_position, redis.srandmember(CARDS))
def add_favor(self, member_id, shop_id): """ 把喜爱的商铺放到收藏夹 :param member_id 用户ID :param shop_id 关注的商铺ID :return """ redis_structure_name = Constant.FAVORS + Constant.COLON + member_id redis.sadd(redis_structure_name, shop_id)
def start(message): id = message.chat.id redis.sadd('startmebot',id) if redis.hget("lang:{}".format(message.chat.id),"farsi"): bot.send_message(message.chat.id, 'زبان فعلی شما فارسی است🇮🇷\nمیتوانید با زدن دکمه ی زیر زبان خود را تغییر دهید🇺🇸', reply_markup=markupavfa) elif redis.hget("lang:{}".format(message.chat.id),"english"): bot.send_message(message.chat.id, 'Your language now is english🇺🇸\nYou can press down button to set persian language🇮🇷', reply_markup=markupaven) else: bot.send_message(message.chat.id, "زبان خود را انتخاب کنید👇\nSelect your language👇", reply_markup=markupstart)
def add_worker(self, client_id): """Register a WebSocket connection for Redis updates.""" # Check if we have already registered this client if not redis.get(client_id): return False redis.persist(client_id) redis.sadd(XROOTD_CLIENT, client_id) self.clients[client_id] = 1 return True
def add_server(self, server_id): """Register a WebSocket connection for Redis updates.""" # Check if we have already registered this client if not redis.get(server_id): return False redis.persist(server_id) redis.sadd(XROOTD_SERVER, server_id) self.servers[server_id] = 1 return True
def listen_domain(self, storage_handler, domain, listen_myself = False): try: self._domain_handlers[domain].add(storage_handler) except KeyError: self._domain_handlers[domain] = set([storage_handler]) redis = self._get_redis(domain) redis.sadd('#' + domain, self._data_to_string((self._myself.serialize(), listen_myself))) if not listen_myself: redis.srem('#' + domain, self._data_to_string((self._myself.serialize(), True)))
def add(user): u = { "name": user, "fullname": user, "pic": "/assets/img/user.png", "src": "own" } redis.sadd(user, json.dumps(u)) return "added user: " + user
def check_pm(m): if m.chat.type == 'supergroup': redis.sadd(m.content_type, m.message_id) if redis.sismember('groups', m.chat.id): if not redis.get('expire' + str(m.chat.id)): bot.reply_to(m, '🔴 اعتبار گروه به پایان رسیده است 🔴') redis.srem('groups', m.chat.id) bot.leave_chat(m.chat.id) else: check(m)
def generate_cards(hash: str) -> None: """ Cria cartelas com os números de 1-99. Parameters: hash: hash das cartelas. """ redis.set(COUNT, 0) while int(redis.get(COUNT)) < 99: redis.sadd(hash, int(redis.incr(COUNT)))
def listen_key(self, storage_handler, key, domain = 'default', listen_myself = False): name = self._make_name(key, domain) try: self._key_handlers[name].add(storage_handler) except KeyError: self._key_handlers[name] = set([storage_handler]) redis = self._get_redis(name) redis.sadd('@' + name, self._data_to_string((self._myself.serialize(), listen_myself))) if not listen_myself: redis.srem('@' + name, self._data_to_string((self._myself.serialize(), True)))
def redis_sadd(redis, key, i): start = i * 5000 end = (i + 1) * 5000 v = start while (v < end): redis.sadd(key, v) v = v + 1 print(threading.currentThread().ident, threading.currentThread().name, r.scard(key))
def incr(self, name, timestamp=None, value=1, filters={}): timestamp = timestamp or datetime.datetime.now() try: redis = self.redis redis.sadd(self.data_key, self.serialize("incr", name, timestamp, value, filters)) redis.publish(self.channel, json.dumps({"a": "notification"})) except Exception, e: print(e) logger.warn(e)
def get_file(self, fid, stop_event, driver=None, restart=False): """Transfers a file from a Driver to another. """ redis = self.plug.redis metadata = Metadata.get_by_id(self.plug, fid) filename = metadata.filename transfer_key = 'drivers:{}:transfers:{}'.format(self.plug.name, fid) if driver: redis.sadd('drivers:{}:transfers'.format(self.plug.name), fid) redis.hmset(transfer_key, {'from': driver, 'offset': 0}) offset = 0 self.logger.info("Starting to get '{}' from {}", filename, driver) else: transfer = redis.hgetall(transfer_key) driver = transfer['from'] offset = int(transfer['offset']) self.logger.info("Restarting transfer of '{}' from {}", filename, driver) dealer = self.context.socket(zmq.DEALER) port = redis.get('drivers:{}:router'.format(driver)) dealer.connect('tcp://localhost:{}'.format(port)) end = metadata.size chunk_size = self.plug.options.get('chunk_size', 1 * 1024 * 1024) if not restart: self._call('start_upload', metadata) while offset < end: if stop_event.is_set(): # another transaction for the same file has # probably started self.logger.info("Aborting transfer of '{}' from {}", filename, driver) return dealer.send_multipart((filename, str(offset), str(chunk_size))) chunk = dealer.recv() self.logger.debug("Received chunk of size {} from {} for '{}'", len(chunk), driver, filename) self._call('upload_chunk', filename, offset, chunk) offset = redis.hincrby(transfer_key, 'offset', len(chunk)) self._call('end_upload', metadata) redis.delete(transfer_key) redis.srem('drivers:{}:transfers'.format(self.plug.name), fid) self.logger.info("Transfer of '{}' from {} successful", filename, driver)
def new_speaker(): check_post_param('name') new_name = request.form['name'] rand_bits = random.getrandbits(SPEAKER_HASH_LENGTH) rand_hash = base64.b64encode(bytes(str(rand_bits), 'ascii')) prefix = new_name[:min(4, len(new_name))] new_id = "{}:{}".format(prefix, rand_hash.decode('utf-8')) redis.sadd(USER_IDS_SET, new_id) redis.hmset(hm_data(new_id), {USER_NAME: new_name}) return new_id
def do_report(pipeline, redis): process_report = { 'id': pipeline_id, 'hostname': hostname, 'fqdn': fqdn, 'pid': pid, 'version': VERSION, 'mem_usage': psutil.virtual_memory().percent, 'disk_usage': psutil.disk_usage(pipeline.data_dir).percent } redis.hmset(pipeline_id, process_report) redis.sadd('pipelines', pipeline_id)
def enrol(self, cell_phone_number, pwd): # not exist the cell phone, can go on enrol is_exist = self.cell_phone_number_exist(cell_phone_number) if is_exist != 0 : # TODO(JIAS): put the new user object into 'Member:memberId' redis object redis_structure_name = Constant.MEMBER + cell_phone_number redis.hmset(redis_structure_name, "CELL_PHONE", cell_phone_number) redis.hmset(redis_structure_name, "PASSWORD", pwd) # generate session id # TODO(JIAS): SessionId应该一到MySQL的DAO中处理,然后存储到Redis中 session_id = utils.SessionGenerator.session_generate() redis.hmset(redis_structure_name, "SESSION_ID", session_id) # 把号码放置到Member:cellphone中,标识此号码已经被注册过了 redis.sadd(Constant.MEMBER_CELL_PHONE, cell_phone_number)
def get_animal(): animal = redis.spop('animals') if not animal: redis.sadd('animals', 'human', 'bird', 'dog', 'deer', 'mouse', 'fish', 'snake', 'bear', 'cat', 'duck') animal = redis.spop('animals') return animal
def check_price_and_notify(delay): while 1: print "Notification is triggered!" # loading latest products in memory # build list product with decreasing prices products_prices_before = build_hash_from_json(json.loads(redis.get('optimusprice.prices'))) status_code, prices = get_all_prices() if (status_code == 200): products_prices_now = build_hash_from_json(prices) else: products_prices_now = products_prices_before users = redis.smembers("optimusprice.subscriptions.users") print "Subscribed users: ", users for chat_id in users: user_id = redis.get("optimusprice.mapping." + str(chat_id)) # updating user subscriptions status_code, products = get_likes_for_user(user_id) if status_code == 200: for p in products: redis.sadd("optimusprice.subscriptions." + str(chat_id), p) products_ids_to_notify = [] print products_prices_before.keys() print products_prices_now.keys() for p in products_prices_before.keys(): if products_prices_now.get(p) is not None: if float(products_prices_now.get(p)['price']) < float(products_prices_before.get(p)['price']): products_ids_to_notify.append(p) print "products_ids_to_notify ", products_ids_to_notify users = redis.smembers("optimusprice.subscriptions.users") print "subscribed users ", users for u in users: products_likes = map(int, redis.smembers("optimusprice.subscriptions." + str(u))) print "likes for user %s : %s" %(str(u), str(products_likes)) products_intersection = set(products_ids_to_notify).intersection(products_likes) print "products_intersection for user %s : %s" %(str(u), str(products_intersection)) if len(products_intersection) > 0: print "Notifying %s about %s" % (u,str(products_intersection)) for pp in products_intersection: bot.sendMessage(u, "Prices has changed for :" + products_prices_before.get(pp)['name']) f = open('images/%s.jpg' % pp, 'rb') # some file on local disk bot.sendPhoto(u, f) time.sleep(delay)
def inital(): """ inital project data to redis :return: """ for path in get_files(settings.PROJECT_DIR, ".md"): # 文件创建时间 # 有序结合保存所有文档,查看所有文章时,可以按照时间顺序查看 create_time = os.path.getctime(path) redis.zadd('admin.blog.list', create_time, utils.encrypt(path)) # 把文件的上级目录当作标签 #有序set存放该标签下的文章,相当于文章按标签分类,用有序集合的原因是可以按时间顺序显示 tag_name = os.path.basename(os.path.dirname(path)) redis.zadd('blog.tag.%s' % tag_name, create_time, utils.encrypt(path)) #set集合存放所有标签 redis.sadd('blog.tag', tag_name)
def tweet_and_shout(api_session, redis, key, timeout=600): """""" for tweet_id in redis.lrange("%s:%s" % (LOLCOIFFEURS_LIST, key), 0, -1): tweet_dict = redis.hgetall("%s:tweet:%s" % (LOLCOIFFEURS_LIST, tweet_id)) # Tracking answered tweets in a brand new set, and posting # a reply to it print "replying tweet : %s" % (tweet_id) redis.sadd((LOLCOIFFEURS_LIST + ":%s:answered" % (key)), tweet_id) # api_session.PostUpdate("@%s %s" % (tweet_dict["username"], RESPONSE), in_reply_to_status_id=tweet_id) # Popping out element from the left of the list # as we answer it redis.rpop("%s:%s" % (LOLCOIFFEURS_LIST, key)) # Wait timeout before replying again sleep(timeout) return
def load(): # see: http://python-fitbit.readthedocs.org/en/latest/#fitbit-api fb = fitbit.Fitbit( os.getenv('CONSUMER_KEY'), os.getenv('CONSUMER_SECRET'), user_key=os.getenv('USER_KEY'), user_secret=os.getenv('USER_SECRET')) redis.delete('fitbit') if True: sleepData = dict(); sl1 = fb.time_series('sleep/startTime', period='max')['sleep-startTime'] sl2 = fb.time_series('sleep/timeInBed', period='max')['sleep-timeInBed'] sl3 = fb.time_series('sleep/minutesAsleep', period='max')['sleep-minutesAsleep'] sl4 = fb.time_series('sleep/minutesAwake', period='max')['sleep-minutesAwake'] sl5 = fb.time_series('sleep/minutesToFallAsleep', period='max')['sleep-minutesToFallAsleep'] sl6 = fb.time_series('sleep/minutesAfterWakeup', period='max')['sleep-minutesAfterWakeup'] sl7 = fb.time_series('sleep/efficiency', period='max')['sleep-efficiency'] for sl in range(len(sl1)): if sl1[sl]['value'] != '': sleepData['date'] = sl1[sl]['dateTime'] sleepData['startTime'] = sl1[sl]['value'] sleepData['timeInBed'] = sl2[sl]['value'] sleepData['minutesAsleep'] = sl3[sl]['value'] sleepData['minutesAwake'] = sl4[sl]['value'] sleepData['minutesToFallAsleep'] = sl5[sl]['value'] sleepData['minutesAfterWakeup'] = sl6[sl]['value'] sleepData['efficiency'] = sl7[sl]['value'] sleepData['timezone'] = fb.user_profile_get()['user']['timezone'] sleepData['offsetFromUTCMillis'] = fb.user_profile_get()['user']['offsetFromUTCMillis'] s = json.dumps(sleepData) redis.sadd('fitbit', s) print s
def skip_turn(self, player): """ Causes a player to vote to skip the current artist. A majority is required. """ # Make sure the artist isn't skipping artist = self._get_artist() if artist == player.name: self._error('artist voted to skip') return # Add the player to the list of voted players if redis.sadd(self.skip_key, player.name): self.send(Message('SKIPPED', player_name=player.name))
def enqueue_url(u): url = format_url(u) return redis.sadd("listing_url_queue", url)
def authorize(chat_id, user_id): fsm_approve(chat_id) bot.sendMessage(chat_id, MSG_3) # add user to subscribed users redis.sadd("optimusprice.subscriptions.users", str(chat_id)) redis.set("optimusprice.mapping." + str(chat_id), str(user_id))
# -*- coding: utf-8 -*- import redis import os redis = redis.StrictRedis(host="106.187.34.51", port=6379, password="******", db=0) joke_fp = open("./j.txt", "r") joke_items = joke_fp.readlines() joke_fp.close() for item in joke_items: if redis.sadd("joke:text", item): print "ok"
def push_servers(urls): for url in urls: redis.sadd(servers_key, url)
def push_sets(sets, ip, url): for set in sets: redis.sadd(set, ip) redis.set(ip, url)
def check_pulse(self, signum, _): ekg = redis.lrange('EKG', 0, -1) redis.delete('active_feeds') for i in ekg: redis.sadd('active_feeds', i)
import redis redis = redis.StrictRedis(host='localhost', port=6379, db=0) nodes = open('nodes.txt', 'r') for node in nodes : node = int(node.rstrip()) redis.sadd('nodes', node)
def accept(self, u): redis.sadd("match:%s:accepted" % self.id, u.id)
def get_next(redis): from time import time from ambry.identity import DatasetNumber delay_factor = 2 ip = str(request.remote_addr) now = time() next_key = "next:"+ip delay_key = "delay:"+ip ipallocated_key = "allocated:"+ip # # The assignment class determine how long the resulting number will be # which namespace the number is drawn from, and whether the user is rate limited # The assignment_class: key is assigned and set externally # access_key = request.query.access_key assignment_class = None if access_key: assignment_class_key = "assignment_class:"+access_key assignment_class = redis.get(assignment_class_key ) if not assignment_class: raise exc.NotAuthorized('Use an access key to gain access to this service') # The number space depends on the assignment class. number_key = "dataset_number:"+assignment_class authallocated_key = "allocated:"+assignment_class nxt = redis.get(next_key) delay = redis.get(delay_key) # Adjust rate limiting based on assignment class if assignment_class == 'authoritative': since, nxt, delay, wait, safe = (0,now-1,0,0,0) elif assignment_class == 'registered': delay_factor = 1.1 ok, since, nxt, delay, wait, safe = request_delay(nxt,delay,delay_factor) with redis.pipeline() as pipe: redis.set(next_key, nxt) redis.set(delay_key, delay) logger.info("ip={} ok={} since={} nxt={} delay={} wait={} safe={}" .format(ip, ok, since, nxt, delay, wait, safe)) if ok: number = redis.incr(number_key) dn = DatasetNumber(number, None, assignment_class) redis.sadd(ipallocated_key, dn) redis.sadd(authallocated_key, dn) else: number = None raise exc.TooManyRequests("Requests will resume in {} seconds".format(wait)) return dict(ok=ok, number=str(dn), assignment_class=assignment_class, wait=wait, safe_wait=safe, nxt=nxt, delay=delay)
from sqlalchemy.ext.automap import automap_base from sqlalchemy.orm import sessionmaker # get student/studentcode in learning on base db , then send into redis use set type engine = create_engine('oracle://*****:*****@10.100.134.177:1521/orcl') tables = ['eas_schroll_student'] metadata=MetaData() metadata.reflect(engine,only=tables) Base = automap_base(metadata=metadata) Base.prepare() student = Base.classes.eas_schroll_student #print(student.__table__) # get data then import redis only save in redis db1 redis = redis.Redis(host='10.100.134.160',port=6380,db=1) DBsession = sessionmaker(bind=engine) session = DBsession() redisKey = "student.6510000" for studentcode in session.query(student.studentcode).filter_by(learningcentercode='6510000').\ filter_by(enrollmentstatus='1'): #print(studentcode[0]) redis.sadd(redisKey,studentcode[0]) session.close() print (redis.scard(redisKey))
def route_post_ad(slot): if not advertiser_id(): return '', 404 asset = request.files['asset'] id = next_ad_id() key = ad_key(slot, id) type = fetch(request.form, 'type') if not type: type = asset.mimetype if not type: type = 'video/mp4' redis = get_redis() redis.hmset(key, { 'slot': slot, 'id': id, 'title': fetch(request.form, 'title'), 'type': type, 'advertiser': advertiser_id(), 'destination': fetch(request.form, 'destination'), 'impressions': 0 }) redis.set(asset_key(slot, id), asset.read()) redis.rpush(slot_key(slot), id) redis.sadd(advertiser_key(advertiser_id()), key) return jsonify(get_ad(slot, id))