Spaces:
Runtime error
Runtime error
json and fixes
Browse files- scraper.py +80 -24
scraper.py
CHANGED
@@ -1,12 +1,16 @@
|
|
|
|
|
|
1 |
import io
|
|
|
2 |
from string import ascii_lowercase
|
3 |
|
4 |
import aiohttp
|
5 |
|
6 |
from client import HybridClient
|
|
|
7 |
from preprocessing import index_pdf
|
8 |
|
9 |
-
grade_map = ascii_lowercase[:
|
10 |
|
11 |
subject_map = {
|
12 |
"science": "esc1",
|
@@ -18,51 +22,103 @@ subject_map = {
|
|
18 |
|
19 |
|
20 |
def get_url(grade, subject, chapter):
|
21 |
-
filename = grade_map[grade] + subject_map[subject] + str(chapter).zfill(2)
|
22 |
url = f"https://ncert.nic.in/textbook/pdf/{filename}.pdf"
|
|
|
23 |
return url
|
24 |
|
25 |
|
26 |
-
async def get_book(grade, subject):
|
27 |
book = {}
|
28 |
-
|
|
|
|
|
29 |
async with aiohttp.ClientSession() as session:
|
30 |
-
|
31 |
-
|
|
|
32 |
|
33 |
-
pdf = download(session, url)
|
34 |
|
35 |
if pdf:
|
36 |
-
collection = f"{grade}_{subject}"
|
|
|
37 |
book[collection] = pdf
|
38 |
else:
|
39 |
break
|
|
|
40 |
return book
|
41 |
|
42 |
|
43 |
-
async def download(session, url):
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
pdf_content = io.BytesIO()
|
49 |
-
async for chunk in r.content.iter_chunked(1000000):
|
50 |
-
pdf_content.write(chunk)
|
51 |
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
|
|
|
|
|
59 |
|
60 |
-
def upload_book(grade, subject):
|
61 |
-
hclient = HybridClient()
|
62 |
|
63 |
-
|
|
|
|
|
|
|
64 |
for collection, pdf in book.items():
|
65 |
-
chunks = index_pdf(pdf)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
hclient.create(collection)
|
68 |
hclient.insert(collection, chunks)
|
|
|
1 |
+
import asyncio
|
2 |
+
import base64
|
3 |
import io
|
4 |
+
import json
|
5 |
from string import ascii_lowercase
|
6 |
|
7 |
import aiohttp
|
8 |
|
9 |
from client import HybridClient
|
10 |
+
from headers import random_headers
|
11 |
from preprocessing import index_pdf
|
12 |
|
13 |
+
grade_map = ascii_lowercase[:12]
|
14 |
|
15 |
subject_map = {
|
16 |
"science": "esc1",
|
|
|
22 |
|
23 |
|
24 |
def get_url(grade, subject, chapter):
|
25 |
+
filename = grade_map[grade - 1] + subject_map[subject] + str(chapter).zfill(2)
|
26 |
url = f"https://ncert.nic.in/textbook/pdf/{filename}.pdf"
|
27 |
+
print(url)
|
28 |
return url
|
29 |
|
30 |
|
31 |
+
async def get_book(grade, subject, chapters=None):
|
32 |
book = {}
|
33 |
+
if not chapters:
|
34 |
+
chapters = range(1, 20)
|
35 |
+
|
36 |
async with aiohttp.ClientSession() as session:
|
37 |
+
print("Downloaded - ", end="")
|
38 |
+
for i in chapters:
|
39 |
+
url = get_url(grade, subject, i)
|
40 |
|
41 |
+
pdf = await download(session, url)
|
42 |
|
43 |
if pdf:
|
44 |
+
collection = f"{grade}_{subject}_{i}"
|
45 |
+
print(i, end="")
|
46 |
book[collection] = pdf
|
47 |
else:
|
48 |
break
|
49 |
+
print()
|
50 |
return book
|
51 |
|
52 |
|
53 |
+
async def download(session: aiohttp.ClientSession, url: str, max_retries: int = 3) -> io.BytesIO | None:
|
54 |
+
for attempt in range(max_retries):
|
55 |
+
try:
|
56 |
+
headers = {"Accept": "application/pdf"} | random_headers()
|
57 |
+
async with session.get(url, headers=headers, timeout=10) as r:
|
58 |
+
r.raise_for_status()
|
59 |
+
content = await r.read()
|
60 |
+
return io.BytesIO(content)
|
61 |
+
except Exception as e:
|
62 |
+
print(f"Attempt {attempt + 1} failed: {e}")
|
63 |
+
if attempt < max_retries - 1:
|
64 |
+
await asyncio.sleep(2 ** (attempt + 1))
|
65 |
+
else:
|
66 |
+
print(f"Max retries reached. Unable to download PDF from {url}")
|
67 |
+
return None
|
68 |
|
|
|
|
|
|
|
69 |
|
70 |
+
async def upload_book(grade, subject, chapters=None):
|
71 |
+
hclient = HybridClient()
|
72 |
|
73 |
+
book = await get_book(grade, subject)
|
74 |
+
print(type(book))
|
75 |
+
for collection, pdf in book.items():
|
76 |
+
print(collection)
|
77 |
+
chunks = index_pdf(pdf, buffer=True)
|
78 |
|
79 |
+
hclient.create(collection)
|
80 |
+
hclient.insert(collection, chunks)
|
81 |
|
|
|
|
|
82 |
|
83 |
+
async def save_book_to_json(grade, subject, chapters=None):
|
84 |
+
book = await get_book(grade, subject, chapters)
|
85 |
+
result = {}
|
86 |
+
|
87 |
for collection, pdf in book.items():
|
88 |
+
chunks = index_pdf(pdf, buffer=True)
|
89 |
+
|
90 |
+
serializable_chunks = []
|
91 |
+
for chunk in chunks:
|
92 |
+
serializable_chunk = {}
|
93 |
+
for key, value in chunk.items():
|
94 |
+
if isinstance(value, bytes):
|
95 |
+
serializable_chunk[key] = base64.b64encode(value).decode("utf-8")
|
96 |
+
else:
|
97 |
+
serializable_chunk[key] = value
|
98 |
+
serializable_chunks.append(serializable_chunk)
|
99 |
+
|
100 |
+
result[collection] = serializable_chunks
|
101 |
+
|
102 |
+
with open(f"{grade}_{subject}.json", "w") as f:
|
103 |
+
json.dump(result, f)
|
104 |
+
|
105 |
+
|
106 |
+
def upload_book_from_json(json_file_path):
|
107 |
+
hclient = HybridClient()
|
108 |
+
|
109 |
+
with open(json_file_path, "r") as f:
|
110 |
+
data = json.load(f)
|
111 |
+
|
112 |
+
for collection, serialized_chunks in data.items():
|
113 |
+
chunks = []
|
114 |
+
for serialized_chunk in serialized_chunks:
|
115 |
+
chunk = {}
|
116 |
+
for key, value in serialized_chunk.items():
|
117 |
+
if isinstance(value, str) and value.startswith("b'") and value.endswith("'"):
|
118 |
+
chunk[key] = base64.b64decode(value[2:-1])
|
119 |
+
else:
|
120 |
+
chunk[key] = value
|
121 |
+
chunks.append(chunk)
|
122 |
|
123 |
hclient.create(collection)
|
124 |
hclient.insert(collection, chunks)
|