Untitled

 avatar
unknown
python
3 years ago
1.6 kB
5
Indexable
import urllib.request
import requests
from bs4 import BeautifulSoup
from pyrogram import Client, filters
from pyrogram.types import Message
import os
import asyncio
from ..icebot import IceBot


@IceBot.on_message(filters.me & filters.command("books", "."))
async def books(client: Client, message: Message):
    await message.edit_text("Getting books...")
    if not os.path.exists("Books"):
        os.makedirs("Books")
    query = message.text.strip().split(".books")[1].strip()
    r = requests.get(
        f"http://libgen.is/search.php?&res=100&req={query}&phrase=1&view=simple&column=def&sort=year&sortmode=DESC"
    )
    soup = BeautifulSoup(r.content, "html.parser")

    book_urls = soup.find_all("a", {"title": "this mirror"})
    r = soup.select("table.c")[0].select("tr td:nth-child(3)")[1:]
    books = [
        {"book_name": v.find("a").text, "book_url": book_urls[i]["href"]}
        for i, v in enumerate(r)
    ]
    for z in books:
        re = requests.get(z["book_url"])
        soup2 = BeautifulSoup(re.content, "html.parser")
        book_down_link = (
            soup2.select("div#download")[0].select("ul")[0].find("a")["href"]
        )
        book_real_name = (
            +z["book_name"].strip().replace(" ", "_")
            + "."
            + book_down_link[::-1].split(".", 1)[0][::-1],
        )
        urllib.request.urlretrieve(book_down_link, "Books/" + book_real_name)
        await client.send_document(
            chat_id=message.chat.id,
            document="Books/" + book_real_name,
            caption=book_real_name + "\n@ice777_0",
        )
        await asyncio.sleep(2)
Editor is loading...