Spaces:
Sleeping

File size: 3,675 Bytes
ff3e69e
617a5d8
 
 
 
 
 
 
 
79c0bfe
617a5d8
 
 
 
 
79c0bfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d9cf7b
 
 
 
 
 
 
 
 
 
 
 
617a5d8
 
ff3e69e
 
4d9309a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff3e69e
 
 
 
 
 
 
79c0bfe
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import requests
from bs4 import BeautifulSoup

OPENSTAX_URL = "https://openstax.org/subjects"

def get_openstax_books():
    response = requests.get(OPENSTAX_URL)
    soup = BeautifulSoup(response.text, "html.parser")

    books = {}
    for link in soup.find_all("a", href=True):
        if "/books/" in link["href"]:
            title = link.text.strip()
            books[title] = f"https://openstax.org{link['href']}"

    return books

def get_pdfdrive_books(query):
    url = f"https://www.pdfdrive.com/search?q={query.replace(' ', '+')}"
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")

    books = {}
    for a in soup.find_all("a", href=True):
        if a["href"].startswith("/download"):
            title = a.text.strip()
            books[title] = f"https://www.pdfdrive.com{a['href']}"
    return books

def get_gutenberg_books(query):
    url = f"https://www.gutenberg.org/ebooks/search/?query={query.replace(' ', '+')}"
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")

    books = {}
    for book in soup.find_all("li", class_="booklink"):
        title_elem = book.find("span", class_="title")
        link_elem = book.find("a", href=True)
        if title_elem and link_elem:
            title = title_elem.text.strip()
            link = "https://www.gutenberg.org" + link_elem["href"]
            books[title] = link
    return books

def get_umn_books():
    url = "https://open.umn.edu/opentextbooks"
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")

    books = {}
    for link in soup.find_all("a", href=True):
        if "/opentextbooks" in link["href"]:
            title = link.text.strip()
            books[title] = link["href"]
    return books

def get_archive_books(query):
    books = {}
    try:
        url = f"https://archive.org/search.php?query={query.replace(' ', '+')}"
        response = requests.get(url, timeout=10)
        soup = BeautifulSoup(response.text, "html.parser")

        for item in soup.find_all("div", class_="ttl"):
            link = item.find("a", href=True)
            if link:
                title = link.text.strip()
                books[title] = "https://archive.org" + link["href"]
    except Exception as e:
        print(f"Error fetching from archive.org: {e}")
    return books

def download_pdf(book_url):
    response = requests.get(book_url, stream=True)
    content_type = response.headers.get("Content-Type", "")

    # If the link is HTML, try to extract the actual PDF link
    if "text/html" in content_type:
        soup = BeautifulSoup(response.text, "html.parser")

        # Try common selectors for actual PDF download links
        for a in soup.find_all("a", href=True):
            href = a["href"]
            if href.endswith(".pdf"):
                pdf_url = href if href.startswith("http") else os.path.join(book_url, href)
                return download_pdf(pdf_url)
        raise ValueError("No direct PDF link found on the page.")

    elif "application/pdf" not in content_type:
        raise ValueError(f"The URL does not point to a valid PDF. Content-Type: {content_type}")

    filename = "textbook.pdf"
    with open(filename, "wb") as f:
        for chunk in response.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)
    return filename

def get_all_sources_books(query):
    books = {}
    books.update(get_openstax_books())
    books.update(get_pdfdrive_books(query))
    books.update(get_gutenberg_books(query))
    books.update(get_umn_books())
    books.update(get_archive_books(query))
    return books