<commit_before>import os
import shutil
import requests
from studip_sync import parsers
import time


class SessionError(Exception):
    pass


class LoginError(SessionError):
    pass


class DownloadError(SessionError):
    pass


class URL(object):
    @staticmethod
    def login_page():
        return "https://studip.uni-goettingen.de"

    @staticmethod
    def files_main():
        return "https://studip.uni-goettingen.de/dispatch.php/course/files"

    @staticmethod
    def files_flat():
        return "https://studip.uni-goettingen.de/dispatch.php/course/files/flat"

    @staticmethod
    def bulk_download(folder_id):
        return "https://studip.uni-goettingen.de/dispatch.php/file/bulk/{}".format(folder_id)

    @staticmethod
    def studip_main():
        return "https://studip.uni-goettingen.de/dispatch.php/start"

    @staticmethod
    def courses():
        return "https://studip.uni-goettingen.de/dispatch.php/my_courses"


class Session(object):

    def __init__(self):
        super(Session, self).__init__()
        self.session = requests.Session()
        self.session.headers.update({"User-Agent": "WeWantFileSync"})

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.session.__exit__()

    def login(self, username, password):
        with self.session.get(URL.login_page()) as response:
            if not response.ok:
                raise LoginError("Cannot access Stud.IP login page")
            login_data = parsers.extract_login_data(response.text)

        login_params_auth = {
            "loginname": username,
            "password": password
        }

        login_params = {**login_params_auth, **login_data['params']}

        with self.session.post(login_data['action'], data=login_params) as response:
            if not response.ok:
                raise LoginError("Cannot post login data")
            elif "Fehler" in response.text:
                raise LoginError("Wrong credentials, cannot login")

        #Test if logged in
        with self.session.post(URL.studip_main()) as response:
            if not response.ok or not "Veranstaltungen" in response.text:
                raise LoginError("Cannot access Stud.IP main page")

    def get_courses(self):
        with self.session.get(URL.courses()) as response:
            if not response.ok:
                raise SessionError("Failed to get courses")

            return parsers.extract_courses(response.text)

    def check_course_new_files(self, course_id, last_sync):
        params = {"cid": course_id}

        with self.session.get(URL.files_flat(), params=params) as response:
            if not response.ok:
                raise DownloadError("Cannot access course files_flat page")
            last_edit = parsers.extract_files_flat_last_edit(response.text)
        
        if last_edit == 0:
            print("\tLast file edit couldn't be detected!")
        else:
            print("\tLast file edit: {}".format(time.strftime("%d.%m.%Y %H:%M", time.gmtime(last_edit))))
        return last_edit == 0 or last_edit > last_sync

    def download(self, course_id, workdir, sync_only=None):
        params = {"cid": course_id}
<commit_msg>Fix Login bug<commit_after>import os
import shutil
import requests
from studip_sync import parsers
import time


class SessionError(Exception):
    pass


class LoginError(SessionError):
    pass


class DownloadError(SessionError):
    pass


class URL(object):
    @staticmethod
    def login_page():
        return "https://studip.uni-goettingen.de"

    @staticmethod
    def files_main():
        return "https://studip.uni-goettingen.de/dispatch.php/course/files"

    @staticmethod
    def files_flat():
        return "https://studip.uni-goettingen.de/dispatch.php/course/files/flat"

    @staticmethod
    def bulk_download(folder_id):
        return "https://studip.uni-goettingen.de/dispatch.php/file/bulk/{}".format(folder_id)

    @staticmethod
    def studip_main():
        return "https://studip.uni-goettingen.de/dispatch.php/start"

    @staticmethod
    def courses():
        return "https://studip.uni-goettingen.de/dispatch.php/my_courses"


class Session(object):

    def __init__(self):
        super(Session, self).__init__()
        self.session = requests.Session()
        self.session.headers.update({"User-Agent": "WeWantFileSync"})

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.session.__exit__()

    def login(self, username, password):
        with self.session.get(URL.login_page()) as response:
            if not response.ok:
                raise LoginError("Cannot access Stud.IP login page")
            login_data = parsers.extract_login_data(response.text)

        login_params_auth = {
            "loginname": username,
            "password": password
        }

        login_params = {**login_params_auth, **login_data['params']}

        with self.session.post(login_data['action'], data=login_params) as response:
            if not response.ok:
                raise LoginError("Cannot post login data")
            elif "messagebox_error" in response.text:
                raise LoginError("Wrong credentials, cannot login")

        #Test if logged in
        with self.session.post(URL.studip_main()) as response:
            if not response.ok or not "Veranstaltungen" in response.text:
                raise LoginError("Cannot access Stud.IP main page")

    def get_courses(self):
        with self.session.get(URL.courses()) as response:
            if not response.ok:
                raise SessionError("Failed to get courses")

            return parsers.extract_courses(response.text)

    def check_course_new_files(self, course_id, last_sync):
        params = {"cid": course_id}

        with self.session.get(URL.files_flat(), params=params) as response:
            if not response.ok:
                raise DownloadError("Cannot access course files_flat page")
            last_edit = parsers.extract_files_flat_last_edit(response.text)
        
        if last_edit == 0:
            print("\tLast file edit couldn't be detected!")
        else:
            print("\tLast file edit: {}".format(time.strftime("%d.%m.%Y %H:%M", time.gmtime(last_edit))))
        return last_edit == 0 or last_edit > last_sync

    def download(self, course_id, workdir, sync_only=None):
        params = {"cid": course_id}

{'fraction_non_alphanumeric': 0.1096017289286817, 'fraction_numerical': 0.0006174745291756715, 'mean_word_length': 2.6014452473596443, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1422987', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1842, 'n_words': 430}