Commit 2c4ac50c authored by Kiste's avatar Kiste
Browse files

Initial commit

parents
# Stud.SAUGER
Der [Stud.SAUGER](https://git.finf.uni-hannover.de/kiste/studsauger) lädt alle Dateien der eigenen Veranstaltungen aus [Stud.IP](http://www.studip.de/) mittels der [Rest.IP](https://studip.github.io/studip-rest.ip/)-API herunter.
path:
user:
#password:
blacklist:
-
-
#!/usr/bin/env python3
import os
import shutil
import argparse
import requests
import yaml
BASEURL = 'https://studip.uni-hannover.de/plugins.php/restipplugin/api'
def mkdir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
return
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--path', help='Pfad für lokalen Dateibaum')
parser.add_argument('-u', '--user', help='Stud.IP Benutzername')
parser.add_argument('-pw', '--password', help='Stud.IP Passwort')
parser.add_argument('-b', '--blacklist', help='Blacklist für Veranstaltungen, mehrmals angeben für mehrere Veranstaltungen', action='append')
args = parser.parse_args()
with open('config.yaml', 'r') as stream:
try:
config = yaml.load(stream)
except yaml.YAMLError as exc:
print(exc)
if args.path:
path = args.path
elif 'path' in config:
path = config['path']
else:
print('Kein Pfad angegeben!')
exit(1)
if args.user:
user = args.user
elif 'user' in config:
user = config['user']
else:
print('Kein Benutzer angegeben!')
exit(1)
if args.password:
password = args.password
elif 'password' in config:
password = config['password']
else:
print('Kein Passwort angegeben!')
exit(1)
if args.blacklist:
blacklist = args.blacklist
elif 'blacklist' in config:
blacklist = config['blacklist']
else:
blacklist = {}
mkdir(config['path'])
jsemester = requests.get(BASEURL + '/courses/semester', auth=(config['user'], config['password']))
semester_id = jsemester.json()['semesters'][-1]['semester_id']
semester_title = jsemester.json()['semesters'][-1]['title']
print('Lade Semester: ' + semester_title)
jcourses = requests.get(BASEURL + '/courses/semester/' + semester_id, auth=(config['user'], config['password']))
for course in jcourses.json()['courses']:
course_id = course['course_id']
course_title = course['title']
if any(course_title in s for s in config['blacklist']):
continue
print('Lade Kurs: ' + course_title)
mkdir(config['path'] + '/' + course_title)
jfolder = requests.get(BASEURL + '/documents/' + course_id + '/folder', auth=(config['user'], config['password']))
for folder in jfolder.json()['folders']:
folder_id = folder['folder_id']
jfiles = requests.get(BASEURL + '/documents/' + course_id + '/folder/' + folder_id, auth=(config['user'], config['password']))
for file in jfiles.json()['documents']:
document_id = file['document_id']
filename = file['filename']
f = config['path'] + '/' + course_title + '/' + filename
if not os.path.isfile(f):
print('Lade ' + filename)
fdl = requests.get(BASEURL + '/documents/' + document_id + '/download', auth=(config['user'], config['password']), stream=True)
if fdl.status_code == 200:
with open(f, 'wb') as file:
print('Writing ' + f)
fdl.raw.decode_content = True
shutil.copyfileobj(fdl.raw, file)
else:
print('Nichts zu tun.')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment