def get_top_pair(uid: int) -> Optional[User]: replylove__dragon_lovers = CONFIG.get('replylove__dragon_lovers', []) if uid in replylove__dragon_lovers: return User(0, 0, 'drakon', '🐉') replylove__ignore = CONFIG.get('replylove__ignore', []) replylove__ignore_pairs = CONFIG.get('replylove__ignore_pairs', {}).get(str(chat_id), {}).get(str(uid), []) pairs: List[Tuple[str, int]] = sort_dict(db['pair']) for pair, count in pairs: a_uid, b_uid = [get_int(x) for x in pair.split(',')] strast = None if a_uid is None or b_uid is None: continue if count < 5: continue if uid == a_uid and a_uid == b_uid: continue if any(x in replylove__dragon_lovers for x in (a_uid, b_uid)): continue if any(x in replylove__ignore for x in (uid, a_uid, b_uid)): continue if any(x in replylove__ignore_pairs for x in (a_uid, b_uid)): continue if uid == a_uid: strast = User.get(b_uid) if uid == b_uid: strast = User.get(a_uid) if strast: return strast return None
def get(cls, uid) -> typing.Optional['User']: if not uid: return None if isinstance(uid, ChatUser): uid = uid.uid if isinstance(uid, str): uid = get_int(uid) if uid is None: return None cached = cache.get(cls.__get_cache_key(uid)) if cached: return cached logger.debug(f'get_lock {uid}') # лок, чтобы в редис попало то, что в бд with cls.get_lock: try: user = UserDB.get(uid) if user: cache.set(cls.__get_cache_key(uid), user, time=USER_CACHE_EXPIRE) return user except Exception as e: logger.error(e) return None
def get_cards(cls, ids): result = [] for id_str in ids: card_id = get_int(id_str) if not card_id: continue card = cls.get_card(card_id) if not card: continue result.append(card) return result
def get_config_chats() -> List[ChatInConfig]: result = [] for chat_id_str, chat_options in CONFIG.get('chats', {}).items(): chat_id = get_int(chat_id_str) if chat_id is None: continue chat = ChatInConfig(chat_id, chat_options, chat_options.get('enabled_commands', []), chat_options.get('disabled_commands', [])) result.append(chat) return result
def __dump_love(cls, filename, stats) -> None: from collections import OrderedDict header = 'Реплаев в этой паре\tИмя 1\tИмя 2' rows = [] for pair_key, count in stats: uid1, uid2 = [get_int(uid) for uid in pair_key.split(',')] name1, name2 = [ cls.__get_user_fullname(uid1), cls.__get_user_fullname(uid2) ] # добавляем оба варианта, чтобы была полная картина rows.append(f'{count}\t{name1}\t{name2}') rows.append(f'{count}\t{name2}\t{name1}') rows_uniq = list(OrderedDict.fromkeys( rows)) # удаляем дубли, сохраняя порядок строк body = '\n'.join(rows_uniq) cls.__save(filename, f'{header}\n{body}\n')
def send_replytop(bot, chat_id, prev_monday): stats = ReplyTop.get_stats(chat_id, prev_monday) msg = "<b>Кто кого реплаит</b>\n\n" def __get_user_fullname(uid): if uid == bot_id(): return 'Бот 🤖' user = User.get(uid) fullname = uid if not user else user.fullname return fullname def __get_stat_part(header, stat, delimeter, plurals): result = header for i, stat in enumerate(stat, start=1): uid, count = stat comment = " {} {}".format( delimeter, pytils.numeral.get_plural( count, plurals)) if i == 1 else '' fullname = __get_user_fullname(uid) result += "{}. <b>{}</b>{}\n".format(count, fullname, comment) return result msg += __get_stat_part( "Им все пишут:\n", stats['to'], '←', 'реплай получен, реплая получено, реплаев получено') msg += "\n" msg += __get_stat_part( "Они всем пишут:\n", stats['from'], '→', 'реплай отправлен, реплая отправлено, реплаев отправлено') msg += "\n" msg += "Топ страсти ❤:\n" for i, stat in enumerate(stats['pair'], start=1): pair_key, count = stat uid1, uid2 = [get_int(uid) for uid in pair_key.split(',')] names = [__get_user_fullname(uid1), __get_user_fullname(uid2)] random.shuffle(names) msg += f"{count}. <b>{names[0]}</b> ⟷ <b>{names[1]}</b>\n" send_long(bot, chat_id, msg)