def parse_week_day(self, text): try: for (k, n) in self.WEEKDAYS.items(): if k in text: return n except KeyError: wrong_format(MIIGAIK_SCHEDULE_URL, "can not parse weekday")
def __init__(self): front_page_data = self.request_get(MIIGAIK_SCHEDULE_URL) dom = BeautifulSoup(front_page_data) forms = dom.findAll("form") if len(forms) > 1: wrong_format(MIIGAIK_SCHEDULE_URL, "more than one form") form = forms[0] def pull_out_list(name): return [ i for i in parse_select_item(form.find("select", attrs={u"name": name})) if i["text"].strip() and i["value"] ] self._faculties = pull_out_list("fak") self._years = pull_out_list("kurs") self._groups = pull_out_list("grup")
def parse_table(self, group_id, table): utemp = dict((day, DaySchedule()) for day in xrange(1, 8)) ltemp = dict((day, DaySchedule()) for day in xrange(1, 8)) for row in table.findAll("tr"): cells = row.findAll("td") if len(cells) == self.ROWCOUNT and any(_un(t.text).strip() for t in cells): try: lesson = self.row_to_lesson(group_id, cells) except ValueError as e: wrong_format(MIIGAIK_SCHEDULE_URL, "can not parse lesson because of %s" % e) if lesson.week_type == UPPER_WEEK: utemp[lesson.week_day].set_lesson(lesson.number, lesson) else: ltemp[lesson.week_day].set_lesson(lesson.number, lesson) def conv(lst): p = lst.items() p.sort(lambda t1, t2: cmp(t1[0], t2[0])) return [i for i in p if len(i[1]) > 0] return GroupDataContainer(group_id, conv(utemp), conv(ltemp))
def find_form(dom): forms = dom.findAll("form") if len(forms) > 1: wrong_format(MIIGAIK_SCHEDULE_URL, "more than one form") return forms[0]
def parse_week_type(self, text): try: return self.WEEK_MAP[text] except KeyError: wrong_format(MIIGAIK_SCHEDULE_URL, "can not parse week type")
def choose_table(self, data): for table in data.findAll("table"): if self.table_is_valid(table): return table raise wrong_format(MIIGAIK_SCHEDULE_URL, "can not find valid table")