From b3891489ce46c086da28d196d07dd8ec706b2bf4 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Fri, 18 Aug 2023 13:14:08 +0300 Subject: [PATCH] added reactions script --- .gitignore | 1 + bots/reactions/README.md | 16 +++++ bots/reactions/reactions.py | 106 ++++++++++++++++++++++++++++++++ bots/reactions/requirements.txt | 2 + 4 files changed, 125 insertions(+) create mode 100644 bots/reactions/README.md create mode 100644 bots/reactions/reactions.py create mode 100644 bots/reactions/requirements.txt diff --git a/.gitignore b/.gitignore index d71613f..ae8c1e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea .env +*.session ### Python template # Byte-compiled / optimized / DLL files diff --git a/bots/reactions/README.md b/bots/reactions/README.md new file mode 100644 index 0000000..5c5991b --- /dev/null +++ b/bots/reactions/README.md @@ -0,0 +1,16 @@ +# reaction sorter + +script to sort the highest rated(telegram reactions) posts from channels + +### Installation +get api_id and api_hash at https://my.telegram.org/apps +```shell +$ pip install -r requirements.txt +$ export api_id=123 +$ export api_hash=abcdef.... +``` + +### Run +```shell +$ python3 reactions.py +``` diff --git a/bots/reactions/reactions.py b/bots/reactions/reactions.py new file mode 100644 index 0000000..1fba992 --- /dev/null +++ b/bots/reactions/reactions.py @@ -0,0 +1,106 @@ +import os +import jellyfish + +from telethon import TelegramClient + +if os.getenv("api_id") is None: + raise ValueError("please set api_id env variable") + +if os.getenv("api_hash") is None: + raise ValueError("please set api_hash env variable") + +api_id = os.getenv("api_id") +api_hash = os.getenv("api_hash") + +source = input("name, username, link or id: ") +source_type = input("c - channel id, s - search: ") +while source_type not in ["c", "s"]: + source_type = input("c - channel id, s - search: ") + + +async def aenumerate(asequence, start=0): + """Asynchronously enumerate an async iterator from a given start value""" + n = start + async for elem in asequence: + yield n, elem + n += 1 + + +async def progress_bar( + iterable, + total, + prefix="", + suffix="", + decimals=1, + length=100, + fill="█", + print_end="\r", +): + # Progress Bar Printing Function + def print_progress_bar(iteration): + percent = ("{0:." + str(decimals) + "f}").format( + 100 * (iteration / float(total)) + ) + filledLength = int(length * iteration // total) + bar = fill * filledLength + "-" * (length - filledLength) + print( + f"\r{prefix} |{bar}| {percent}% ({iteration+1}/{total}) {suffix}", + end=print_end, + ) + + print_progress_bar(0) + async for i, item in aenumerate(iterable): + yield item + print_progress_bar(item.id) + + +async def run(client: TelegramClient): + if source_type == "s": + channels = [] + async for dialog in client.iter_dialogs(): + channels.append((dialog, str(dialog.name))) + similar_channels = [] + for id, name in channels: + similar_channels.append( + (name, id, jellyfish.levenshtein_distance(source.lower(), name.lower())) + ) + similar_channels.sort(key=lambda x: x[2]) + similar_channels = similar_channels[:10] + for i, var in enumerate(similar_channels): + print(f"[{i+1}] - {var[0]}") + n = int(input("Number: ")) + if n > 10: + raise ValueError + entity = similar_channels[n - 1][1] + else: + entity = await client.get_entity(source) + + messages = [] + max_id = 0 + async for message in client.iter_messages(entity): + max_id = message.id + break + + print("Loading.... This might take a while") + eid = str(entity.id) + if eid.startswith("-100"): + eid = eid[4:] + async for message in progress_bar( + client.iter_messages(entity, reverse=True, min_id=0), + max_id, + " " * 4 + f"processing {entity.title}:", + ): + r = message.reactions + if r: + count = sum([x.count for x in r.results]) + messages.append( + (f"https://t.me/c/{eid}/{message.id} - {message.text}", count) + ) + messages.sort(key=lambda x: x[1], reverse=True) + print("done!") + for message, reactions in messages[:20]: + print(f"[{reactions}] - {message}") + + +with TelegramClient("reactions", api_id, api_hash) as client: + client.loop.run_until_complete(run(client)) diff --git a/bots/reactions/requirements.txt b/bots/reactions/requirements.txt new file mode 100644 index 0000000..cc7aa98 --- /dev/null +++ b/bots/reactions/requirements.txt @@ -0,0 +1,2 @@ +telethon +jellyfish \ No newline at end of file