From 9a0dbc2feba3e858676644b86121509a002c56d6 Mon Sep 17 00:00:00 2001 From: TiynGER Date: Wed, 11 Nov 2020 16:47:47 +0100 Subject: [PATCH] bugfixes: events without data-folder error If an event didn't have a folder for files it was erroring. This is fixed now by adding try-except blocks. --- README.md | 3 ++- src/crawler.py | 8 ++++---- src/studip.py | 28 ++++++++++++++++++++-------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8455e7f..57bcd1f 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,8 @@ the last time running it. ### Errors and workarounds -- [ ] if filename is too long it currently gets skipped +- [ ] If filename is too long it currently gets skipped +- [ ] Filenames get sometimes displayed wrong (probably utf8 problems) ## Installation diff --git a/src/crawler.py b/src/crawler.py index c61a266..cc522e1 100644 --- a/src/crawler.py +++ b/src/crawler.py @@ -40,7 +40,7 @@ class Crawler: self.create_dir(subdir_path) os.chdir(subdir_path) self.download_folder_rec(subdir, subdir_path) - log.info('Finished crawling folder ' + folder) + log.info('finished crawling folder ' + folder) def download_course(self, course, base_dir): """Download all documents in course. @@ -55,7 +55,7 @@ class Crawler: os.chdir(base_dir) root = self.studip.get_top_folder(course) self.download_folder_rec(root, base_dir) - log.info('Finished crawling course ' + course) + log.info('finished crawling course ' + course) def download_curr_courses(self, base_dir): """Download all documents of all current courses. @@ -74,7 +74,7 @@ class Crawler: course_name = curr_courses[course].replace('/', '-') path = os.path.join(base_dir, course_name) self.download_course(course, path) - log.info('Finished crawling all current courses') + log.info('finished crawling all current courses') def create_dir(self, dir): """Creates a dir if it doesnt exist already. @@ -83,5 +83,5 @@ class Crawler: dir(string): directory path to create """ if not os.path.exists(dir): - log.info('creating folder' + dir) + log.info('creating folder ' + dir) os.mkdir(dir) diff --git a/src/studip.py b/src/studip.py index 5317030..396739d 100755 --- a/src/studip.py +++ b/src/studip.py @@ -121,11 +121,17 @@ class Studip: list(string): ids of the documents """ rsp = self.auth_req('/api.php/folder/' + folder) - docs = rsp.json()['file_refs'] res_docs = [] + try: + docs = rsp.json()['file_refs'] + except ValueError: + return res_docs for doc in docs: - doc_id = doc['id'] - res_docs.append(doc_id) + try: + doc_id = doc['id'] + res_docs.append(doc_id) + except KeyError: + return res_docs return(res_docs) def download(self, doc): @@ -161,11 +167,17 @@ class Studip: Returns: list(string): ids of the subdirectories """ - rsp = self.auth_req('/api.php/folder/' + folder) - subdirs = rsp.json()['subfolders'] res_subdirs = {} + rsp = self.auth_req('/api.php/folder/' + folder) + try: + subdirs = rsp.json()['subfolders'] + except ValueError: + return res_docs for subdir in subdirs: - sub_id = subdir['id'] - sub_name = subdir['name'] - res_subdirs[sub_id] = sub_name + try: + sub_id = subdir['id'] + sub_name = subdir['name'] + res_subdirs[sub_id] = sub_name + except KeyError: + return res_subdirs return res_subdirs