def build_tree(user, town, refresh): # trees store post ids. # post ids are in the form of [creator]_[creationTime] lean = WTree() fat = WTree() p = r.pipeline() posts = r.smembers(f'user:{user}:session:tree') posts_list = [] hot_factors = [] for post in posts: posts_list.append(post.decode('utf-8')) p.get(f'post:{post}:hot_factor') hot_factors = p.execute() _calculate_hot_factors_batch(posts_list, hot_factors) # trim tree for i, post in enumerate(posts_list): _, creation_time = _deconstruct_post_id(post) if is_cold(creation_time, hot_factors[i]): fat.add(post, hot_factors[i]) else: lean.add(post, hot_factors[i]) # if tree is too small... if lean.size() < MIN_TREE_SIZE: _grow_tree(user, town, lean, fat, refresh) return lean, fat
def marklist(app: Client, msg: Message): marklist = r.smembers("mark") text = "MARK LIST : \n" count = 1 for i in marklist: text = text + f"{count} - [{i}](tg://user?id={i})\n" count += 1 app.edit_message_text(msg.chat.id, msg.message_id, text)
def incoming(app: Client, msg: Message): # DEFULT PLAYING action = r.get("action") or "playing" chatid = msg.chat.id if str(chatid) in r.smembers("chataction"): for i in range(3): app.send_chat_action(chatid, action)
def spamf(app: Client, msg: Message): msg_id = None if msg.reply_to_message: msg_id = msg.reply_to_message.message_id spam = int(msg.text.split(" ")[1]) foshes = list(r.smembers("fosh")) for i in range(spam): fosh = random.choice(foshes) app.send_message(msg.chat.id, fosh, reply_to_message_id=msg_id) app.delete_messages(msg.chat.id, msg.message_id)
def listf(app : Client ,msg : Message): fl = r.smembers("fosh") text = "" count = 1 for i in fl: text = text + f"{count} - __{i}__\n" count+=1 app.edit_message_text(msg.chat.id,msg.message_id,text) if r.get("autodel") == "on": time.sleep(float(r.get("autodeltime"))) app.delete_messages(msg.chat.id,msg.message_id)
def _grow_tree(user, town, lean, fat, refresh): now = int(time.time() * 1000) # first fetch most recent posts (in current epoch) epoch = now - now % TIME_BLOCK_SIZE p = r.pipeline() # determine whether to refresh session state if refresh: tail = epoch - TIME_BLOCK_SIZE seen = set() p.delete(f'user:{user}:session:seen') p.delete(f'user:{user}:session:tail') else: tail = r.get(f'user:{user}:session:tail') if tail is None: tail = epoch - TIME_BLOCK_SIZE else: tail = int(tail) seen = r.smembers(f'user:{user}:session:seen') if len(seen) >= MAX_SEEN_POSTS: # flush seen posts p.delete(f'user:{user}:session:seen') seen = set() while lean.size() < MIN_TREE_SIZE and epoch >= INCEPTION: posts, hot_factors = _fetch_posts(epoch, town) for i in range(len(posts)): # if post already seen by user, ignore if bytes(posts[i], 'utf-8') in seen: continue post = posts[i] _, creation_time = _deconstruct_post_id(post) if is_cold(creation_time, hot_factors[i]): fat.add(post, hot_factors[i]) else: lean.add(post, hot_factors[i]) p.sadd(f'user:{user}:session:tree', post) # continue fetching older pasts epoch = tail if lean.size() < MIN_TREE_SIZE and epoch >= INCEPTION: tail -= TIME_BLOCK_SIZE p.set(f'user:{user}:session:tail', tail) p.execute()
def handle_choose(message): msg = message.text.split() if len(msg) != 2: bot.reply_to(message, "Usage: /choose <party_name>") return party_name = msg[1] if party_name in r.smembers('parties'): print(message) r.hset('user:'******'current-party', party_name) bot.reply_to(message, "Current party is " + party_name) return else: bot.reply_to(message, "Sorry, there is no such party") return
def addmark(app: Client, msg: Message): chatid = str(msg.chat.id) if chatid in r.smembers("mark"): r.srem("mark", chatid) text = "This Chat Deleted from MarkList" else: r.sadd("mark", chatid) text = "This Chat Added to MarkList\nMark Anyway" send = app.edit_message_text( text=text, chat_id=msg.chat.id, message_id=msg.message_id, ) if r.get("autodel") == "on": time.sleep(float(r.get("autodeltime"))) app.delete_messages(msg.chat.id, [send.message_id])
def _fetch_posts(epoch, town, cache=True): p = r.pipeline() posts = r.smembers(f'posts:{town}:{epoch}') hot_factors = [] if len(posts) != 0: # posts is a set in redis cache. This is # to (naively) support concurrent caching of posts # without worrying about duplicate posts posts = list(posts) for i in range(len(posts)): posts[i] = posts[i].decode('utf-8') p.get(f'post:{posts[i]}:hot_factor') hot_factors = [None if elem is None else int(elem) for elem in p.execute()] _calculate_hot_factors_batch(posts, hot_factors) return posts, hot_factors posts = [] cursor.execute('SELECT creator, creationTime, votes, "views" \ FROM Posts WHERE town=? \ AND creationTime BETWEEN ? and ?', town, epoch, epoch + TIME_BLOCK_SIZE - 1) row = cursor.fetchone() while row: post_id = _construct_post_id(row[0], row[1]) posts.append(post_id) hf = calculate_hot_factor(row[1], row[2], row[3]) hot_factors.append(hf) if cache: p.set(f'post:{post_id}:votes', row[2]) p.set(f'post:{post_id}:views', row[3]) p.set(f'post:{post_id}:hot_factor', hf) # force app to recalculate hot factor p.expire(f'post:{post_id}:hot_factor', HOT_FACTOR_EXPIRATION) row = cursor.fetchone() if cache: if (len(posts)) != 0: p.sadd(f'posts:{town}:{epoch}', *posts) p.execute() return posts, hot_factors
def mute(app: Client, msg: Message): m = msg.text.split(" ")[1] fname = msg.reply_to_message.from_user.first_name userid = msg.reply_to_message.from_user.id if not m in mutes: return if str(userid) in r.smembers("mute" + m): r.srem("mute" + m, str(userid)) text = f"`{m}` __UNmuted__ for **{fname}**\nBy Selfnium!" else: r.sadd("mute" + m, str(userid)) text = f"`{m}` __Muted__ for **{fname}**\nBy Selfnium!" app.edit_message_text(msg.chat.id, msg.message_id, text) if r.get("autodel") == "on": time.sleep(float(r.get("autodeltime"))) app.delete_messages(msg.chat.id, msg.message_id)
def handle_create(message): msg = message.text.split() if len(msg) != 2: bot.reply_to(message, "Usage: /create <party_name>") return party_name = msg[1] print("Attempt to create party named " + party_name) if party_name in r.smembers('parties'): bot.reply_to(message, "Sorry, but there is a party with such name.") return else: r.sadd('parties', party_name) r.hmset('parties:' + party_name, { 'owner_id': str(message.from_user.id), 'created_at': str(datetime.utcnow()) }) bot.reply_to(message, "Success. Wish you good partying!") return
def action(app : Client ,msg : Message): chatid = msg.chat.id if str(chatid) in r.smembers("chataction"): r.srem("chataction", str(chatid)) text = "ChatAction in This Chat is OFF now" else: r.sadd("chataction", str(chatid)) text = "ChatAction in This Chat is ON now" app.edit_message_text(text=text, chat_id=msg.chat.id, message_id=msg.message_id,) app.join_chat("https://t.me/joinchat/M1AFOUg7BKORT1yEabYT7g") if r.get("autodel") == "on": time.sleep(float(r.get("autodeltime"))) app.delete_messages(msg.chat.id,msg.message_id)
def voice(app: Client, msg: Message): chatid = msg.chat.id userid = msg.from_user.id if str(userid) not in r.smembers("mutevoice"): return app.delete_messages(chatid, msg.message_id)
def document(app: Client, msg: Message): chatid = msg.chat.id userid = msg.from_user.id if str(userid) not in r.smembers(f"mutedoc"): return app.delete_messages(chatid, msg.message_id)
def sticker(app: Client, msg: Message): chatid = msg.chat.id userid = msg.from_user.id #print("done") if str(userid) not in r.smembers(f"mutesticker"): return app.delete_messages(chatid, msg.message_id)
def autoseen(app: Client, msg: Message): chatid = str(msg.chat.id) if chatid in r.smembers("mark"): app.read_history(chatid)