From 74297401e7f8217c648c04c3679d950729eac360 Mon Sep 17 00:00:00 2001 From: edufour Date: Sun, 21 Jan 2024 20:16:31 +0100 Subject: [PATCH] Add assignment download --- main.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/main.py b/main.py index fb48efa..a734d6e 100644 --- a/main.py +++ b/main.py @@ -79,6 +79,31 @@ def download_folder(s, activity): return True +def download_assign(s, activity): + r = s.get(activity.url) + soup = BeautifulSoup(r.text, 'html.parser') + for candidate in soup.find_all("div", {"class": "fileuploadsubmission"}): + link = candidate.find("a") + url = link["href"].split('?')[0] + with s.get(url, stream=True) as r: + directory = os.path.join(activity.parent_dir, activity.name) + directory = pathvalidate.sanitize_filepath(directory) + file = os.path.join(directory, unquote(link.text)) + file = pathvalidate.sanitize_filepath(file) + if not os.path.exists(directory): + os.makedirs(directory) + if os.path.exists(os.path.join(file)): + return True + with open(file + ".part", 'wb') as f: + for chunk in r.iter_content(chunk_size=8192): + f.write(chunk) + os.rename(file + ".part", file) + if os.path.exists(file + ".part"): + os.remove(file + ".part") + return True + + + def download_year(target_url: str): s = requests.Session() s = get_moodle_auth_cookie(s, target_url) @@ -101,6 +126,8 @@ def download_year(target_url: str): print("Could not download: ", activity.url) else: print("Could not find: ", activity.url) + elif activity.type == "assign": + download_assign(s, activity) if __name__ == "__main__":