-
Notifications
You must be signed in to change notification settings - Fork 8
/
gyft2.py
156 lines (132 loc) · 4.59 KB
/
gyft2.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from bs4 import BeautifulSoup as bs
import json
import generate_ics
from sys import exit
import os.path
from Query_yn import query_yes_no
# Input the file
if os.path.isfile('view_stud_time_table.html'):
with open('view_stud_time_table.html', 'r') as myfile:
r = myfile.read()
elif os.path.isfile('Time Table View.html'):
with open('Time Table View.html', 'r') as myfile:
r = myfile.read()
else:
# print("HTML file not found,please enter the path to name of your timetable \'.html\' file(with full path to it):")
# file = input()
dir_path = os.path.dirname(os.path.realpath(__file__))
r = None
for root, dirs, files in os.walk(dir_path):
# print(dirs)
for dir in dirs:
if os.path.isfile(os.path.join(dir , 'view_stud_time_table.html')):
with open(os.path.join(dir , 'view_stud_time_table.html'), 'r') as myfile:
r = myfile.read()
break
elif os.path.isfile(os.path.join(dir , 'Time Table View.html')):
with open(os.path.join(dir , 'Time Table View.html'), 'r') as myfile:
r = myfile.read()
break
if r is not None:
break
if r is None:
print("HTML file not found, while downloading make sure you select the folder of gyft2 to save the \".html\" file")
exit()
# print(r)
soup = bs(r, 'html.parser')
rows_head = soup.findAll('table')[2]
rows = rows_head.findAll('tr')
times = []
# Delete the rows that doesn't have tableheader, basically without a weekday
del_rows = []
for i in range(1, len(rows)):
HeaderRows = rows[i].findAll("td", {"class": "tableheader"})
# print(HeaderRows)
if len(HeaderRows) is 0:
del_rows.append(i)
# print(del_rows)
for index_del in sorted(del_rows, reverse=True):
del rows[index_del]
# for row in rows:
# print(row)
# for row in rows:
# print(len(row))
# #### For timings
for a in rows[0].findAll('td'):
if ('AM' in a.text or 'PM' in a.text):
times.append(a.text)
# ### For timings end
days = {}
# ### For day
days[1] = "Monday"
days[2] = "Tuesday"
days[3] = "Wednesday"
days[4] = "Thursday"
days[5] = "Friday"
days[6] = "Saturday"
# ### For day end
timetable_dict = {}
for i in range(1, len(rows)):
timetable_dict[days[i]] = {}
tds = rows[i].findAll('td')
time = 0
for a in range(1, len(tds)):
if not tds[a].find('b'):
continue
txt = tds[a].find('b').text.strip()
if (len(txt) >= 7):
timetable_dict[days[i]][times[time]] = list(
(
tds[a].find('b').text[:7],
tds[a].find('b').text[7:],
int(tds[a].attrs['colspan'])
)
)
time = time + int(tds[a].attrs['colspan'])
def merge_slots(in_dict):
for a in in_dict:
in_dict[a] = sorted(in_dict[a])
for i in range(len(in_dict[a]) - 1, 0, -1):
if (in_dict[a][i][0] == in_dict[a][i - 1][0] + in_dict[a][i - 1][1]):
in_dict[a][i - 1][1] = in_dict[a][i][1] + in_dict[a][i - 1][1]
in_dict[a].remove(in_dict[a][i])
in_dict[a] = in_dict[a][0]
return (in_dict)
for day in timetable_dict.keys():
subject_timings = {}
for time in timetable_dict[day]:
flattened_time = int(time[:time.find(':')])
if (flattened_time < 6):
flattened_time += 12
if (not timetable_dict[day][time][0] in subject_timings.keys()):
subject_timings[timetable_dict[day][time][0]] = []
subject_timings[timetable_dict[day][time][0]].append(
[flattened_time, timetable_dict[day][time][2]]
)
subject_timings = merge_slots(subject_timings)
for time in list(timetable_dict[day].keys()):
flattened_time = int(time[:time.find(':')])
if (flattened_time < 6):
flattened_time += 12
if (
not flattened_time == subject_timings[
timetable_dict[day][time][0]
][0]
):
del (timetable_dict[day][time])
else:
timetable_dict[day][time][2] = subject_timings[
timetable_dict[day][time][0]
][1]
with open('data.txt', 'w+') as outfile:
json.dump(timetable_dict, outfile, indent=4, ensure_ascii=False)
print(
'''
Timetable saved to data.txt file. Be sure to edit this file,
to have desired names of subjects rather than subject codes.
'''
)
if query_yes_no("Do you want to generate ICS file too?"):
generate_ics.main()
else:
print("You can Generate ICS later using generate_ics.py")