-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathupdate_course_data.py
executable file
·140 lines (121 loc) · 4.86 KB
/
update_course_data.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
#!/usr/bin/python
'''
Copyright (C) 2014 Muhd Amirul Ashraf bin Mohd Fauzi <[email protected]>
This file is part of Automatic IIUM Schedule Formatter.
Automatic IIUM Schedule Formatter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Automatic IIUM Schedule Formatter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Automatic IIUM Schedule Formatter. If not, see <http://www.gnu.org/licenses/>.
'''
import bootstrap
import schedulescraper
import logging
import logging.config
import admincontroller
import staticsettings
import sqlalchemy.orm.exc
import os
from flask import g
from models import SubjectData,SectionData,SectionScheduleData
logging.config.dictConfig({
'version':1,
'formatters':{
'simple':{
'format':'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
}
},
'handlers':{
'console':{
'class':'logging.StreamHandler',
'level':'INFO',
'formatter':'simple',
'stream': 'ext://sys.stdout'
},
'file':{
'class':'logging.handlers.RotatingFileHandler',
'filename': os.path.join( os.path.dirname(__file__),'course_data_updater.log' ),
'level':'INFO',
'formatter':'simple',
}
},
'root':{
'level':'INFO',
'handlers':['console','file']
}
})
sessions=staticsettings.SESSIONS_STILL_UPDATE
with bootstrap.app.app_context():
for session in sessions:
logging.info("=== STARTING UPDATE ===")
def callback(sem,stype,kuly,data):
if(stype=='<'):
stype='UG'
if(stype=='>='):
stype='PG'
code=data['code']
obj=SubjectData.get_subject_data(code,session,sem)
if(obj!=None):
if(obj.coursetype==stype and
obj.title==data['title'] and
obj.credit==float(data['credit']) and
obj.kuliyyah==kuly and
obj.session==session and
obj.semester==int(sem)):
pass
else:
obj.coursetype=stype
obj.title=data['title']
obj.credit=float(data['credit'])
obj.kuliyyah=kuly
obj.session=session
obj.semester=sem
obj.put()
logging.info("Update subject %s"%obj.code)
else:
obj=SubjectData()
obj.code=code
obj.coursetype=stype
obj.title=data['title']
obj.credit=float(data['credit'])
obj.kuliyyah=kuly
obj.session=session
obj.semester=sem
obj.put()
logging.info("Insert subject %s"%obj.code)
section=data['section']
try:
sdata=SectionData.query.filter(SectionData.subject==obj).filter(SectionData.sectionno==section).one()
sdata.lecturer=data['lecturer']
sdata.schedules = [] # Remove it
for sched in data['schedules']:
newsched = SectionScheduleData()
newsched.venue = sched['venue']
newsched.day = sched['day']
newsched.time = sched['time']
newsched.lecturer = sched['lecturer']
sdata.schedules.append(newsched)
sdata.put()
logging.info("Update subject %s section %s"%(data['code'],data['section']))
except sqlalchemy.orm.exc.NoResultFound, e:
sdata=SectionData()
sdata.subject=obj
sdata.sectionno=section
sdata.lecturer=data['lecturer']
sdata.schedules = [] # Remove it
for sched in data['schedules']:
newsched = SectionScheduleData()
newsched.venue = sched['venue']
newsched.day = sched['day']
newsched.time = sched['time']
newsched.lecturer = sched['lecturer']
sdata.schedules.append(newsched)
sdata.put()
logging.info("Add subject %s section %s"%(data['code'],data['section']))
obj=schedulescraper.fetch_schedule_data_callback(session,callback)
logging.info("Done update")