Skip to content

Instantly share code, notes, and snippets.

@x0x8x
Last active April 9, 2020 11:33
Show Gist options
  • Save x0x8x/519234db810fb1803211dbf2e52bcb55 to your computer and use it in GitHub Desktop.
Save x0x8x/519234db810fb1803211dbf2e52bcb55 to your computer and use it in GitHub Desktop.
from pyrogram import Filters, Message, Client
class custom(dict):
def __missing__(self, key):
return 0
COMMAND = "wcount"
@Client.on_message(Filters.command(COMMAND, ".") & Filters.me)
async def word_count(client: Client, message: Message):
chat = message.chat.id
check1 = str(message.command[0])
check2 = str(message.command[-1])
if message.chat.type == "supergroup":
channelname = str(message.chat.id)
messageid = str(message.message_id)
linkid = channelname.replace("-100", "")
linkd = "https://t.me/c/" + linkid + "/" + messageid
link = f"<a href='{linkd}'>{message.chat.title}</a>\n"
elif message.chat.type == "private":
username = message.chat.id
title = message.chat.first_name
linkd = "tg://user?id=" + str(username)
link = f"<a href='{linkd}'>{title}</a>\n"
elif message.chat.type == "bot":
username = message.chat.id
title = message.chat.first_name
linkd = "tg://user?id=" + str(username)
link = f"<a href='{linkd}'>{title}</a>\n"
if check2 == COMMAND:
await client.send_message(
chat,
"<b>Wrong input!</b>\nCorrect Syntax:\n.wcount 100 20\nOut of <b>100</b> Posts back the Top<b>20</b>",
)
await message.delete()
return
else:
limit = int(message.command[1])
await message.delete()
toplist = int(message.command[-1])
words = custom()
progress = await client.send_message(chat, "`processed 0 messages...`")
total = 0
async for msg in client.iter_history(chat, limit):
total += 1
if total % 200 == 0:
await progress.edit_text(f"`processed {total} messages...`")
if msg.text:
for word in msg.text.split():
words[word.lower()] += 1
if msg.caption:
for word in msg.caption.split():
words[word.lower()] += 1
freq = sorted(words, key=words.get, reverse=True)
out = f"Word counter from ({link}).\nOut of {limit} Posts the Top{toplist}:\n"
for i in range(toplist):
out += f"{i+1}. {words[freq[i]]}: {freq[i]}\n"
await progress.edit_text(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment