First prototype

This commit is contained in:
Casey 2024-01-27 12:38:54 +03:00
parent 4cfc74887c
commit 041ec26b19
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
3 changed files with 64 additions and 5 deletions

46
nfuck/__init__.py Normal file
View File

@ -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"<b>{entity.url}</b>")
for score, explanation, match in explain_verification(data):
results.append(f"{match.span()}: {explanation}")
total_score += score
results.append(f"<b>Total score: {total_score}</b>")
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()

View File

@ -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()

View File

@ -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):