Permalink
Browse files

some code updates

  • Loading branch information...
1 parent 7b94b28 commit 75f75bbfaae4032a7b4635c7f67f5906471cad5c @mochja committed May 12, 2012
Showing with 39 additions and 42 deletions.
  1. +39 −42 edu.py
View
81 edu.py
@@ -42,8 +42,8 @@ def parse_date(s):
# page = "spse-po.edupage.org/substitution/subst_students2012-05-11.htm"
class Substitution():
- def __init__(self,hour,date=None):
- self.hour = hour
+ def __init__(self,classes):
+ self.hour = 0
self.teacher = None
self.lesson = None
self.nlesson = None
@@ -52,7 +52,8 @@ def __init__(self,hour,date=None):
self.room = None
self.chroom = None
self.notice = None
- self.date = date
+ self.date = None
+ self.classes = classes
def parseLesson(self,name):
if "->" in name:
name = [ a.strip() for a in name.split('->') ]
@@ -81,15 +82,6 @@ def parseRoom(self,name):
else:
self.room = name
-class Classroom():
- def __init__(self,name):
- self.name = name
- self.list = []
- def addSubst(self,hour,date=None):
- self.list.append( Substitution(hour,date) )
- def getLastSubst(self):
- return self.list[-1]
-
class Page():
def __init__(self,table,teachers,date):
self.table = table
@@ -104,10 +96,13 @@ def __init__(self):
def fetch(self):
return
def get(self):
- self.last_update = datetime.datetime.now()
+ _log.debug("PageConn: get http")
self.web_conn.request("GET", "/substitution/")
a = self.web_conn.getresponse().read()
- return self.parse(a)
+ if not a is None:
+ _log.debug('PageConn: recv data..')
+ self.last_update = datetime.datetime.now()
+ return self.parse(a)
def parse_teachers(self,s):
s = remove_html_tags(s)
s = re.sub(r'\([^)]*\)', '', s)
@@ -146,30 +141,32 @@ def parse(self, content):
if s == 2:
n += line[i] + "\n"
n = "<html><body><table>" + remove_html_tags(n) + "</table></body></html>"
+ _log.debug("PageConn: parsing done")
return Page(n, teachers, date)
pageWorker = PageConn()
class SubstitutionData():
def __init__(self):
- self.classrooms = []
+ self.substitutions = []
self.datetime = datetime.datetime.now()
- self.teachers = []
-
+ self.teachers = []
class SubstitutionManager():
- def __init__(self):
+ def __init__(self,pw):
self.data = SubstitutionData()
self.datetime = None
- def get(self,page):
+ self.pageWorker = pw
+ def get(self):
+ page = self.pageWorker.get()
if not isinstance(page, Page):
_log.error('somethings wrong')
return
tree = ElementTree()
tree.parse(StringIO(page.table))
- classrooms = self.data.classrooms
+ substitutions = self.data.substitutions
self.data.teachers.append({ page.date.isoformat(): page.teachers })
- self.datetime = datetime.datetime.now()
+ self.data.datetime = datetime.datetime.now()
current = None
l = 0
next = 0
@@ -182,54 +179,54 @@ def get(self,page):
if b == 0 and next == 0:
next = int(tds[b].attrib['rowspan'])
l = a
- current = Classroom(tds[b].text.split(', '))
- classrooms.append( current )
+ current = Substitution(tds[b].text.split(', '))
+ substitutions.append( current )
continue
if b == 1 and l == a or b == 0 and l != a:
- current.addSubst(int(tds[b].text), page.date)
+ current.hour = int(tds[b].text)
+ current.date = page.date
continue
if b == 2 and l == a or b == 1 and l != a:
- current.getLastSubst().teacher = tds[b].text
+ current.teacher = tds[b].text
if b == 3 and l == a or b == 2 and l != a:
- current.getLastSubst().parseLesson(tds[b].text) # here can be a change ->
+ current.parseLesson(tds[b].text) # here can be a change ->
if b == 4 and l == a or b == 3 and l != a:
- current.getLastSubst().parseStatus(tds[b].text)
+ current.parseStatus(tds[b].text)
if b == 5 and l == a or b == 4 and l != a:
- current.getLastSubst().parseRoom(tds[b].text)
+ current.parseRoom(tds[b].text)
if b == 6 and l == a or b == 5 and l != a:
- current.getLastSubst().notice = tds[b].text
+ current.notice = tds[b].text
+ return self.data
-
-manager = SubstitutionManager()
-manager.get( pageWorker.get() )
+manager = SubstitutionManager(pageWorker)
class ClassroomEncoder(json.JSONEncoder):
def default(self,obj):
- if isinstance (obj, Classroom):
- return { 'classes': obj.name, 'substitutions': obj.list }
if isinstance (obj, Substitution):
return {
- 'date': obj.date.isoformat(),
+ 'date': obj.date.isoformat(), 'classes': obj.classes,
'hour': obj.hour, 'teacher': obj.teacher, 'lesson': obj.lesson,
'status': format(obj.status, "03d"), 'room': obj.room, 'chroom': obj.chroom,
'notice': obj.notice, 'nlesson': obj.nlesson, 'steacher': obj.steacher
}
- if isinstance (obj, SubstitutionManager):
+ if isinstance (obj, SubstitutionData):
return {
- 'created': obj.datetime.isoformat(), 'classrooms': obj.data.classrooms, 'version': '0.0.1b',
- 'author': 'janmochnak@gmail.com', 'teachers': obj.data.teachers
+ 'created': obj.datetime.isoformat(), 'substitutions': obj.substitutions, 'version': '0.0.2',
+ 'author': 'janmochnak@gmail.com', 'teachers': obj.teachers
}
return json.JSONEncoder.default(obj)
-def json_encode(obj):
- return json.dumps(obj, cls=ClassroomEncoder, indent=4, sort_keys=True, ensure_ascii=False)
+def json_encode(obj,f):
+ return json.dump(obj,f, cls=ClassroomEncoder, indent=2, sort_keys=True, ensure_ascii=False)
+
+data = manager.get()
class RequestHandler(SimpleHTTPRequestHandler):
def log_message(self, format, *args):
_log.debug('[HTTP] '+format, *args)
def do_GET(self):
- f = StringIO()
- f.write( json_encode(manager) )
+ f = StringIO()
+ json_encode( data , f )
self.send_response(200)
self.send_header("Content-type", "application/json; charset=utf-8")
#self.send_header("Content-Length", str(f.tell()))

0 comments on commit 75f75bb

Please sign in to comment.