-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
373 lines (336 loc) · 11.6 KB
/
api.py
File metadata and controls
373 lines (336 loc) · 11.6 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
from bs4 import BeautifulSoup
import urllib.request
from bs4.element import Tag
dtuUrl = 'http://dtu.ac.in/'
dtuExamUrl = 'http://exam.dtu.ac.in'
dtuNoticesUrl = 'http://exam.dtu.ac.in/Notices-n-Circulars.htm'
def dtuMainWebpage(extended: bool = False) -> object:
'''
Fetches information from DTU Official Webpage
---
Argument
- `extended` : bool (defaults `false`)
If `True` returns the whole page information instead of just the main components.
Useful when creating alternate version of webpage so need the webpage to be scraped entirely.
---
Returns
JSON Object containing all the data from DTU Official Webpage
'''
page = urllib.request.urlopen(dtuUrl)
soup = BeautifulSoup(page.read(), 'html.parser')
title = soup.title.string
# Extracting important updates from marquee in upward direction
marqueeUp = soup.find(direction='up')
sub = []
impUpdates = []
data: Tag
for data in marqueeUp.find_all('a'):
if data.text != None and not '»' in data.text:
sub.append({
'name': data.text.replace('»', '').strip(),
'link': convert_link(data.get('href'))
})
else:
if sub != []:
impUpdates.append({
'name': text,
'sub_list': sub
})
sub = []
text = data.text.replace('»', '').strip()
if data.get('href') != None:
impUpdates.append({
'name': text,
'link': convert_link(data.get('href'))
})
try:
marqueeUp = soup.find(direction='left')
note = {
'name': marqueeUp.a.string.strip(),
'link': convert_link(marqueeUp.a.get('href'))
}
except:
note = {
'name': 'Nothing new here',
'link': 'http://dtu.ac.in'
}
# Extracting tabs of webpage containing information
latestTab = soup.find_all('div', class_='latest_tab')
latestNews = latest_tab_extractor(latestTab[0])
notices = latest_tab_extractor(latestTab[1])
jobs = latest_tab_extractor(latestTab[2])
tenders = latest_tab_extractor(latestTab[3])
firstYear = latest_tab_extractor(latestTab[4])
registeration = latest_tab_extractor(latestTab[5])
events = latest_tab_extractor(latestTab[6])
more_notices = fetch_notices()
notices.extend(more_notices)
# Extracting last updated
bottomSection = soup.find('div', id='bottom_Section')
lastUpdated = bottomSection.get_text().split(
'Last updated :')[1].split('\n')[0].strip()
if not extended:
return {
'title': title,
'last_updated': lastUpdated,
'note': note,
'important_updates': impUpdates,
'news': latestNews,
'events': events,
'notices': notices,
'first_year_notices': firstYear,
'registeration_schedule': registeration,
'tenders': tenders,
'jobs': jobs,
}
else:
# Extracting Menu bar which contains sub-menus and redirecting links
menu = []
linkBar = soup.find('div', id='smoothmenu1').ul
linkBarMenus = linkBar.find_all('li', recursive=False)
i: Tag
for i in linkBarMenus:
menu.append(link_bar_extractor(i))
# Extracting the slider displaying images from important moments
showcase = []
slider = soup.find('div', id='slider2')
showcaseDiv = slider.find_all('div', class_='contentdiv')
for i in showcaseDiv:
try:
showcase.append({
'name': i.get_text().replace('\n', '').strip(),
'image_link': convert_link(i.img.get('src'))
})
except:
None
# Extracting side menu (can be hardcoded as well as not changed with time)
sidemenu = []
wrapper = soup.find('div', id='wrapper_sec')
sideMenuDivs = wrapper.find_all('div', class_='college_gallery')
for i in sideMenuDivs:
sideMenuDivArray = side_menu_extractor(i)
sidemenu.extend(sideMenuDivArray)
# Extracting top menu bar (can be hardcoded as well as not changed with time)
topMenu = []
topMenuList = wrapper.find('ul')
for i in topMenuList.find_all('li'):
name = i.get_text().replace('\n', '').strip()
if name != '':
topMenu.append({
'name': name,
'link': convert_link(i.a.get('href'))
})
return {'title': title,
'last_updated': lastUpdated,
'top_menu': topMenu,
'menu': menu,
'showcase': showcase,
'side_menu': sidemenu,
'note': note,
'important_updates': impUpdates,
'news': latestNews,
'events': events,
'notices': notices,
'first_year_notices': firstYear,
'registeration_schedule': registeration,
'tenders': tenders,
'jobs': jobs, }
def convert_link(url: str, domain: str = dtuUrl) -> str:
'''
Converts relative URL String of DTU Pages to abosute URL string
---
Arguments
- `url` : String containing the URL
- `domain` : Parent page URL ( defaults to `http://dtu.ac.in/` )
---
Returns
Returns the absolute URL of the DTU Page
'''
if url != None:
if url[0] == '/' or url[0] == '.':
return url.replace(
'/', domain, 1) if url[0] == "/" else(url.replace('./', domain))
try:
url.index('http')
url.index('://')
return url
except:
return domain + '/' + url
else:
return None
def link_bar_extractor(html_component: Tag) -> list:
'''
Converts the HTML under link bar from DTU Webpage
into meaningful Array
'''
result = {}
submenu = []
try:
heading = html_component.find('a').get_text().strip()
i: Tag
for i in html_component.ul.find_all('li', recursive=False):
try:
link = i.a.get('href')
if link != None:
submenu.append({
'name': i.a.get_text().strip(), 'link': convert_link(link)
})
else:
subsublist = []
x: Tag
for x in i.find_all('li'):
subsublist.append({
'name': x.a.get_text().strip(), 'link': convert_link(x.a.get('href'))
})
submenu.append({
'name': i.a.get_text().strip(), 'sub_list': subsublist
})
except:
None
result = {
'name': heading,
'sub_list': submenu
}
except:
None
finally:
return result
def side_menu_extractor(html_component: Tag) -> list:
'''
Converts the HTML under the side menu
from DTU Webpage containing links to
various resources into meaningful Array
'''
result = []
items = html_component.find_all('li')
i: Tag
for i in items:
try:
result.append({'name': i.get_text().replace(
'\n', '').strip(), 'link': convert_link(i.find('a').get('href'))})
except:
None
return result
def latest_tab_extractor(html_component: Tag) -> list:
'''
Converts the HTML under latest_tab div
from DTU Webpage into meaningful Array
'''
result = []
try:
item: Tag
for item in html_component.find_all('li'):
try:
aTag = item.find_all('a')
if len(aTag) > 1:
sub = []
i: Tag
for i in aTag:
try:
sub.append({
'name': str(i.get_text()).replace('||', '').strip(),
'link': convert_link(i.get('href'))
})
except:
None
if item.a.get('href') == None:
if item.h6.string == None and aTag[0].link == None:
name = aTag[0].get_text()
sub.pop(0)
else:
name = item.h6.string
result.append({
'name': name.strip(),
'sub_list': sub
})
else:
for x in sub:
result.append(x)
else:
result.append({
'name': item.get_text().strip(),
'link': convert_link(item.a.get('href'))
})
if str(result[len(result)-1]).__contains__('view all'):
result.pop()
break
except:
None
except:
if(len(result) == 0):
result.append({
'name': 'End of results',
'link': 'http://dtu.ac.in/'
})
finally:
return result
def exam() -> list:
'''
Fetches information from DTU Official Result Page
---
Returns
List of JSON Objects containing information about Exam Results
'''
page = urllib.request.urlopen(dtuExamUrl + '/result.htm')
soup = BeautifulSoup(page.read(), 'html.parser')
table = soup.find('table', id='AutoNumber1')
tableRows = table.find_all('tr')
temp: Tag = tableRows[0]
try:
temp.get_text().index('discrepancy')
tableRows.pop(0)
except:
None
headingRow: Tag
headingRow = tableRows[0]
tableRows.pop(0)
headingData = headingRow.find_all('td')
headings = []
i: Tag
for i in headingData:
headings.append(i.get_text().replace(
'\n', '').replace('.', '').lower().strip()) # regex [.\\n]
data = []
for i in tableRows:
contentData = i.find_all('td')
contents = []
x: Tag
y: Tag
counter = 0
for x in contentData:
links = x.find_all('a')
if len(links) > 0:
linkAssests = []
for y in links:
linkAssests.append({"name": y.get_text().replace('\n', '').strip(),
"link": convert_link(y.get('href'), dtuExamUrl)})
contents.append({headings[counter]: linkAssests})
else:
contents.append(
{headings[counter]: x.get_text().replace('\n', '').strip()})
counter += 1
data.append(contents)
return data
def fetch_notices() -> list:
'''
Fetches notices from DTU Official Notices Page
---
Returns
List of JSON Objects containing information about Exam Results
'''
result = []
try:
page = urllib.request.urlopen(dtuNoticesUrl)
soup = BeautifulSoup(page.read(), 'html.parser')
table = soup.table.tbody.find_all('tr', recursive=True)[4]
i: Tag
for i in table.find_all('tr'):
try:
result.append({
'name': i.find('td').get_text().replace('\n', '').strip(),
'link': convert_link(i.find('a').get('href'), dtuExamUrl+'/')
})
except:
None
finally:
return result