Compare commits

...

3 Commits

Author SHA1 Message Date
2b2dbddb0b change readme 2024-01-21 20:19:19 +01:00
74297401e7 Add assignment download 2024-01-21 20:16:31 +01:00
117aa0fdb8 clean up requirement.txt 2024-01-21 12:10:00 +01:00
3 changed files with 31 additions and 2 deletions

View File

@@ -12,7 +12,10 @@ This script was written in python 3.10.4 and the following packages:
| requests | 2.31.0 |
## Features
This script should be able to download all file directly linked to in moodle ressources and folders. It is not able to download files from secondary sources.
Currently downloads:
- ressources
- folders
- assignments
## Usage
The script is pretty simple to use. Just provide a moodle link and authentication information like this:
@@ -40,4 +43,3 @@ python main.py -u USER -p PASSWORD -d /moodle -m https://moodlearchive.epfl.ch/2
## Planned
- [ ] Download submission files
- [ ] Create url files for secondary urls
-

27
main.py
View File

@@ -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__":

Binary file not shown.