From 041ec26b19dcaa49fcab01c80bf8947cdffbf071 Mon Sep 17 00:00:00 2001 From: hkc Date: Sat, 27 Jan 2024 12:38:54 +0300 Subject: [PATCH] First prototype --- nfuck/__init__.py | 46 ++++++++++++++++++++++++++++++++++++++++++ nfuck/__main__.py | 18 +++++++++++++---- nfuck/link_verifier.py | 5 ++++- 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 nfuck/__init__.py diff --git a/nfuck/__init__.py b/nfuck/__init__.py new file mode 100644 index 0000000..e9ef936 --- /dev/null +++ b/nfuck/__init__.py @@ -0,0 +1,46 @@ +from aiogram import Dispatcher +from aiogram.types import Message +from aiogram.filters import Command +from httpx import AsyncClient + +from nfuck.link_verifier import explain_verification, get_random_useragent, verify_link + +dp = Dispatcher() + +@dp.message(Command("check")) +async def on_check(message: Message): + results = [] + for entity in message.entities or []: + if entity.type in ("text_link", "url") and message.text: + if entity.type == "url": + entity.url = message.text[entity.offset : entity.offset + entity.length] + if not entity.url: + continue + async with AsyncClient( + headers={"User-Agent": get_random_useragent()} + ) as client: + data = (await client.get(entity.url)).text + total_score = 0 + results.append(f"{entity.url}") + for score, explanation, match in explain_verification(data): + results.append(f"{match.span()}: {explanation}") + total_score += score + results.append(f"Total score: {total_score}") + results.append("") + await message.reply(str.join("\n", results), parse_mode="html") + + +@dp.message() +async def on_message(message: Message): + for entity in message.entities or []: + if entity.type in ("text_link", "url") and message.text: + if entity.type == "url": + entity.url = message.text[entity.offset : entity.offset + entity.length] + if not entity.url: + continue + confidence = await verify_link(entity.url) + if confidence > 0.75: + await message.reply(f"Holy smokes, another one (~{confidence*100:.0f}% sure)") + await message.delete() + + diff --git a/nfuck/__main__.py b/nfuck/__main__.py index 5c725e3..8e8ff5e 100644 --- a/nfuck/__main__.py +++ b/nfuck/__main__.py @@ -1,5 +1,15 @@ -def main(): - print('Hi!') +from aiogram import Bot +from aiosqlite import connect as asqlite +from os import environ +from nfuck import dp + +async def main(): + bot = Bot(environ["TG_BOT_TOKEN"]) + db = await asqlite(environ["DB_PATH"]) + await dp.start_polling(bot, db=db) + await db.close() + +if __name__ == "__main__": + from asyncio import run + run(main()) -if __name__ == '__main__': - main() diff --git a/nfuck/link_verifier.py b/nfuck/link_verifier.py index a603676..52eb915 100644 --- a/nfuck/link_verifier.py +++ b/nfuck/link_verifier.py @@ -26,10 +26,13 @@ def explain_verification(content: str) -> list[tuple[float, str, Match]]: result.append((score, explanation, match)) return result +def get_random_useragent() -> str: + return choice(USER_AGENT) + async def verify_link(url: str) -> float: total_score = 0 async with AsyncClient( - headers={"User-Agent": choice(USER_AGENT)} + headers={"User-Agent": get_random_useragent()} ) as client: data = await client.get(url) for score, _, _ in explain_verification(data.text):