-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfetch.py
68 lines (57 loc) · 1.46 KB
/
fetch.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
import requests, json, os
from bs4 import BeautifulSoup
cookie = os.environ['JSESSIONID'] # 'F8D06B8C66185BE893CC9838BE21E9E1.worker2'
url = 'https://erp.iitkgp.ac.in/Acad/timetable_track.jsp?action=second&dept={}'
headers = {
'Cookie' : 'JSESSIONID={}'.format(cookie)
}
form = {
'for_session': '2019-2020',
'for_semester': 'AUTUMN',
'dept': '{}'
}
dataFileName = 'courses.json'
depFileName = 'deps.txt'
def getData(dep):
global form
form['dept'] = dep
response = requests.post(url.format(dep), headers=headers, data=form)
courses = []
soup = BeautifulSoup(response.text, 'html.parser')
try:
parentTable = soup.find('table', {'id': 'disptab'})
rows = parentTable.find_all('tr')
except:
return courses
for row in rows:
if 'bgcolor' in row.attrs:
continue
cells = row.find_all('td')
try:
course = {}
course['Name'] = f'{cells[0].text}: {cells[1].text}'
data = {}
data['Faculty'] = cells[2].text
data['LTP'] = cells[3].text
data['Credits'] = cells[4].text
data['Slot'] = cells[5].text
try:
data['Room'] = cells[6].text
except:
pass
course['Data'] = data
print( course['Name'] )
courses.append(course)
except:
print('XXXXXXXXXX')
return courses
if __name__ == '__main__':
deps = []
with open(depFileName, 'r') as depFile:
for dep in depFile:
deps.append(dep[:2])
courses = []
for dep in deps:
courses.extend( getData(dep) )
with open(dataFileName, 'w') as dataFile:
json.dump(courses, dataFile)