First prototype
This commit is contained in:
parent
4cfc74887c
commit
041ec26b19
|
@ -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()
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,15 @@
|
||||||
def main():
|
from aiogram import Bot
|
||||||
print('Hi!')
|
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()
|
|
||||||
|
|
|
@ -26,10 +26,13 @@ def explain_verification(content: str) -> list[tuple[float, str, Match]]:
|
||||||
result.append((score, explanation, match))
|
result.append((score, explanation, match))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def get_random_useragent() -> str:
|
||||||
|
return choice(USER_AGENT)
|
||||||
|
|
||||||
async def verify_link(url: str) -> float:
|
async def verify_link(url: str) -> float:
|
||||||
total_score = 0
|
total_score = 0
|
||||||
async with AsyncClient(
|
async with AsyncClient(
|
||||||
headers={"User-Agent": choice(USER_AGENT)}
|
headers={"User-Agent": get_random_useragent()}
|
||||||
) as client:
|
) as client:
|
||||||
data = await client.get(url)
|
data = await client.get(url)
|
||||||
for score, _, _ in explain_verification(data.text):
|
for score, _, _ in explain_verification(data.text):
|
||||||
|
|
Loading…
Reference in New Issue