Add assignment download

This commit is contained in:
2024-01-21 20:16:31 +01:00
parent 117aa0fdb8
commit 74297401e7

27
main.py
View File

@@ -79,6 +79,31 @@ def download_folder(s, activity):
return True 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): def download_year(target_url: str):
s = requests.Session() s = requests.Session()
s = get_moodle_auth_cookie(s, target_url) s = get_moodle_auth_cookie(s, target_url)
@@ -101,6 +126,8 @@ def download_year(target_url: str):
print("Could not download: ", activity.url) print("Could not download: ", activity.url)
else: else:
print("Could not find: ", activity.url) print("Could not find: ", activity.url)
elif activity.type == "assign":
download_assign(s, activity)
if __name__ == "__main__": if __name__ == "__main__":