Exemplo n.º 1
0
def curriculum_weeks(request):
    def parse(soup_response) -> typing.List[dict]:
        soup = BeautifulSoup(soup_response.content, "lxml")

        select_elm = soup.find("select", attrs={"id": "hafta"})
        option_elms = select_elm.find_all("option")

        data = []

        for elm in option_elms:
            obj = {"id": None, "name": None}
            obj["id"] = elm["value"]
            obj["name"] = elm.text.strip()
            data.append(obj)

        return data

    term_id = request.GET.get("termId", None)

    if term_id is None:
        return response.Response(
            {"detail": "termId parameter must be provided."},
            status=status.HTTP_400_BAD_REQUEST,
        )

    payload = get_payload(request)
    payload.pop("exp", None)
    payload["emailHost"] = "ogr.deu.edu.tr"
    payload["ogretim_donemi_id"] = str(term_id)
    soup_response = requests.post(CURRICULUM_URL, data=payload)

    return response.Response(parse(soup_response))
Exemplo n.º 2
0
def info(request):
    def parse_tr(elm):
        return elm.find_all("td")[1]

    def parse_whole(elm):
        td_elm = parse_tr(elm)
        return td_elm.text.strip()

    def parse_class(elm) -> int:
        td_elm = parse_tr(elm)
        return int(td_elm.text.strip()[0])

    def parse_system(elm) -> bool:
        td_elm = parse_tr(elm)
        return td_elm.text.strip() == "BAĞIL"

    def parse_term(soup) -> int:
        select_elm = soup.find("select", attrs={"id": "ogretim_donemi_id"})
        option_elms = select_elm.find_all("option")
        return len(option_elms) - 1

    def parse(soup_response):
        soup = BeautifulSoup(soup_response.content, "lxml")

        form_elm = soup.find("form", attrs={"name": "form_donem"})
        inspection_elm = form_elm.find("tbody")

        if inspection_elm is None:
            inspection_elm = form_elm.find("table")

        tr_elms = inspection_elm.find_all("tr")

        name_surname_elm = tr_elms[0]
        student_id_elm = tr_elms[1]
        official_class_elm = tr_elms[2]
        faculty_elm = tr_elms[3]
        department_elm = tr_elms[4]
        system_elm = tr_elms[5]

        data = {
            "name": parse_whole(name_surname_elm),
            "studentId": parse_whole(student_id_elm),
            "officialClass": parse_class(official_class_elm),
            "term": parse_term(soup),
            "faculty": parse_whole(faculty_elm),
            "department": parse_whole(department_elm),
            "isRelative": parse_system(system_elm),
        }

        return data

    payload = get_payload(request)
    payload.pop("exp", None)
    payload["emailHost"] = "ogr.deu.edu.tr"

    soup_response = requests.post(INFO_URL, data=payload)

    return response.Response(parse(soup_response))
Exemplo n.º 3
0
    def authenticate(self, request):
        payload = get_payload(request)

        username = payload["username"]
        password = payload["password"]

        soup_response = requests.post(
            "http://debis.deu.edu.tr/debis.php",
            data={
                "username": username,
                "password": password,
                "emailHost": "ogr.deu.edu.tr",
            },
        )

        if "hatal" in soup_response.text:
            raise exceptions.AuthenticationFailed("User is invalid.")

        return (None, None)
Exemplo n.º 4
0
def curriculum_terms(request):
    def parse(soup_response) -> typing.List[dict]:
        soup = BeautifulSoup(soup_response.content, "lxml")

        select_elm = soup.find("select", attrs={"id": "ogretim_donemi_id"})
        option_elms = select_elm.find_all("option")

        data = []

        for elm in option_elms:
            obj = {"id": None, "name": None}
            obj["id"] = elm["value"]
            obj["name"] = elm.text.strip()
            data.append(obj)

        return data

    payload = get_payload(request)
    payload.pop("exp", None)
    payload["emailHost"] = "ogr.deu.edu.tr"
    soup_response = requests.post(CURRICULUM_URL, data=payload)

    return response.Response(parse(soup_response))
Exemplo n.º 5
0
def curriculum(request):
    def parse_session(elm) -> dict:
        """Parses "from" and "to" hours."""
        vals = elm.text.strip().split(" ")  # "17:00 17:45"
        return {
            "fromHour": vals[0] if len(vals) > 0 else None,
            "toHour": vals[1] if len(vals) > 1 else None,
        }

    def parse_td(elm, day: int, session: dict) -> dict:
        obj = {
            "day": day,
            "department": None,
            "code": None,
            "name": None,
            "branch": None,
            "is_theoric": False,
            "lecturer": None,
            "location": None,
        }
        obj.update(session)

        text = elm.get_text("\n").strip()  # type: str
        if not text:  # empty
            return None
        lines = text.splitlines()

        obj["department"] = lines[0]

        code = re.findall("[A-ZÇĞİÖŞÜ]{3} [0-9]{4}", text)
        obj["code"] = code[0] if len(code) else None

        # ref: https://regex101.com/r/sC54Yv/1
        name = re.findall("[A-ZÇĞİÖŞÜ]{3} [0-9]{4} ?- ?(.+)\n", text)
        obj["name"] = name[0] if len(name) else None

        branch = re.findall("(.+) Şubesi", text)
        obj["branch"] = branch[0] if len(branch) else None

        obj["is_theoric"] = True if "Teorik" in text else False
        obj["lecturer"] = lines[-3]

        location = re.findall("Derslik:\n(.+)", text)
        obj["location"] = location[0].strip() if len(location) else None

        return obj

    def parse_tr(elm) -> typing.List[dict]:
        td_elms = elm.find_all("td")

        session_elm = td_elms[0]
        session = parse_session(session_elm)

        objs = []
        for index, td_elm in enumerate(td_elms[1:]):
            obj = parse_td(td_elm, index + 1, session)
            if obj is not None:
                objs.append(obj)

        return objs

    def parse(soup_response):
        soup = BeautifulSoup(soup_response.content, "lxml")

        table_elm = soup.find("table", attrs={"bordercolor": "#FF9900"})
        tbody_elm = table_elm.find("tbody")
        inspection_elm = table_elm if tbody_elm is None else tbody_elm
        tr_elms = inspection_elm.find_all("tr")

        data = []
        for tr_elm in tr_elms[1:]:  # without table header
            objs = data.extend(parse_tr(tr_elm))

        return data

    term_id, week_id = (
        request.GET.get("termId", None),
        request.GET.get("weekId", None),
    )

    if None in (term_id, week_id):
        return response.Response(
            {"detail": "termId and weekId must be provided as parameter."},
            status=status.HTTP_400_BAD_REQUEST,
        )

    payload = get_payload(request)
    payload.pop("exp", None)
    payload["emailHost"] = "ogr.deu.edu.tr"
    payload["ogretim_donemi_id"] = request.GET["termId"]
    payload["hafta"] = request.GET["weekId"]
    soup_response = requests.post(CURRICULUM_URL, data=payload)

    return response.Response(parse(soup_response))
Exemplo n.º 6
0
def results(request):
    def parse_table_section(table_elm, is_left=True):
        sections = table_elm.find_all("tr")[2]
        td_elms = sections.find_all("td", recursive=False)

        if is_left:
            return td_elms[0].find("table")

        return td_elms[1].find("table")

    def parse_klass(table_elm) -> dict:
        def parse_code_name(
                table_elm) -> typing.Tuple[typing.Union[str, None]]:
            header_tr_elm = table_elm.find_all("tr")[0]
            content = header_tr_elm.text.strip()
            parsed_content = content.split(" - ")
            return (
                parsed_content[0] if len(parsed_content) > 0 else None,
                parsed_content[1] if len(parsed_content) > 1 else None,
            )

        def parse_has_completed(table_elm) -> typing.Union[bool, None]:
            footer_tr_elm = table_elm.find_all("tr")[-1]
            if footer_tr_elm is None:
                return None

            content = footer_tr_elm.text.strip()
            return content != "ALIYOR"

        def parse_left_section(left_section,
                               index: int,
                               wrapper=str) -> typing.Union[str, int, bool]:
            elm = left_section.find_all("tr")[index].find_all("td")[2]
            try:
                return wrapper(elm.text.strip())
            except Exception:
                return None

        code_name = parse_code_name(table_elm)
        left_section = parse_table_section(table_elm)

        data = {
            "code":
            code_name[0],
            "name":
            code_name[1],
            "faculty":
            parse_left_section(left_section, 0),
            "department":
            parse_left_section(left_section, 1),
            "branch":
            parse_left_section(left_section, 2, lambda s: str(s[0])),
            "credit":
            parse_left_section(left_section, 3, int),
            "isMandatory":
            parse_left_section(left_section, 4, lambda s: s == "Zorunlu"),
            "repetitionCount":
            parse_left_section(left_section, 5, int),
            "lecturer":
            parse_left_section(left_section, 6, lambda s: s.split(" (")[0]),
            "mandatoryParticipancy":
            parse_left_section(left_section, 7,
                               lambda s: "DEVAM MECBURİ" in s),
            "hasCompleted":
            parse_has_completed(table_elm),
        }

        return data

    def parse_results(table_elm) -> typing.List[dict]:
        def parse_right_section(right_section, index) -> dict:
            elm = right_section.find_all("tr")[index].find_all("td")
            name_elm = elm[0]
            announcement_elm = elm[1]
            median1_elm = elm[2]
            median2_elm = elm[3]
            grade_elm = elm[4]

            data = {
                "name":
                name_elm.text.strip(),
                "announcement":
                announcement_elm.text.strip()
                if announcement_elm.text.strip() != "İLAN EDİLMEMİŞ" else None,
                "median1":
                int(median1_elm.text.strip())
                if median1_elm.text.strip() else None,
                "median2":
                int(median2_elm.text.strip())
                if median2_elm.text.strip() else None,
                "grade":
                grade_elm.text.strip() if grade_elm.text.strip() else None,
            }

            return data

        results = []
        right_section = parse_table_section(table_elm, False)
        result_length = len(right_section.find_all("tr")) - 1

        for index in range(result_length):
            result = parse_right_section(right_section, index + 1)
            results.append(result)

        return results

    def parse(soup_response) -> dict:
        soup = BeautifulSoup(soup_response.content, "lxml")
        table_elm = soup.find("table", attrs={"bgcolor": "99CCFF"})

        data = {
            "class": parse_klass(table_elm),
            "results": parse_results(table_elm),
        }

        return data

    term_id = request.GET.get("termId", None)
    klass_id = request.GET.get("classId", None)

    if None in (term_id, klass_id):
        return response.Response(
            {"detail": "termId and classId parameters must be provided."},
            status=status.HTTP_400_BAD_REQUEST,
        )

    payload = get_payload(request)
    payload.pop("exp", None)
    payload["emailHost"] = "ogr.deu.edu.tr"
    soup_response = requests.post(RESULTS_URL, data=payload)

    return response.Response(parse(soup_response))