-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrapper.py
133 lines (115 loc) · 4.21 KB
/
scrapper.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
from pathlib import Path
import requests
from bs4 import BeautifulSoup
_URL = "https://www.icai.org"
def makeDir(_path: str):
path = Path(_path.replace(":", "-"))
if not os.path.isdir(path):
os.makedirs(path)
return path
def getTableContents(url):
res = requests.get(url)
soup = BeautifulSoup(res.text, "html.parser")
table = soup.find("div", {"class": "table-responsive"})
if table:
return table.find_all("li") # type: ignore
return []
def downloadFile(url: str, fileName: str):
response = requests.get(url)
extension = url.split(".")[-1]
file = fileName.strip().replace(":", "-") + f".{extension}"
os.makedirs(os.path.dirname(Path(file)), exist_ok=True)
with open(Path(file), "wb+") as out:
out.write(response.content)
def handleChild(child, path):
for i in child:
_sub = i.find("a")
if not _sub:
continue
name = _sub.text
_path = path + f"/{name}"
url = _sub["href"]
if url.startswith("https://resource.cdn"):
downloadFile(url, _path)
print(f"Downloading: {url} to {_path}", flush=True)
else:
makeDir(_path)
if url.startswith(".."):
link = _URL + url.lstrip("..")
else:
link = url
child = getTableContents(link)
if not child:
handleTableOldStyle(link, _path)
handleChild(child, _path)
def handleTableOldStyle(url, path: str):
res = requests.get(url)
soup = BeautifulSoup(res.text, "html.parser")
table = soup.find("div", {"class": "table-responsive"})
if table:
table = table.find_all("tr") # type: ignore
else:
return
_first = _second = path
first, second = _first, _second
__first, __second = first, second
for i in table:
head = i.find_all("strong")
if head:
if len(head) > 1:
first = makeDir(_first + f"/{head[0].text}")
second = makeDir(_second + f"/{head[1].text}")
elif head[0].text.strip():
__first = makeDir(first + f"/{head[0].text}")
__second = makeDir(second + f"/{head[0].text}")
else:
columns = i.find_all("td")
if len(columns) == 2:
material, practiceManual = columns
else:
material, practiceManual = columns[0], []
links = material.find_all("a")
for i in links:
url = i["href"]
if url.startswith("https://resource.cdn"):
downloadFile(i["href"], f"{__first}/{i.text}")
if practiceManual:
links = practiceManual.find_all("a") # type: ignore
for i in links:
url = i["href"]
if url.startswith("https://resource.cdn"):
downloadFile(i["href"], f"{__second}/{i.text}")
COURSES = {
1: {"Foundation New": "foundation-course"},
2: {"Intermediate New": "intermediate-course"},
3: {"Intermediate Old": "intermediate-integrated-professional-competence-course"},
4: {"Final New": "final-course-new-scheme-of-education-and-training"},
5: {"Final Old": "final-course-old-scheme-of-education-and-training"},
}
def main():
print(">>>> ICAI BOS Downloader by https://github.com/subinps <<<<")
print("\n")
for i in COURSES:
print(i, ": ", list(COURSES[i].keys())[0], "\n")
while True:
course = input("Enter the number corresponding to your course: ")
try:
course = int(course)
except Exception:
print("!Invalid, Enter A Number between 1-5\n")
continue
else:
if course in COURSES:
break
else:
print("!Invalid, Enter A Number between 1-5\n")
url = list(COURSES[course].values())[0]
name = list(COURSES[course].keys())[0]
res = requests.get(_URL + f"/post/{url}")
soup = BeautifulSoup(res.text, "html.parser")
subjects = soup.find("ul", {"style": "list-style-type: disc;"}).find_all("li") # type: ignore
makeDir(name)
handleChild(subjects, name)
if __name__ == "__main__":
main()