logik.py 2.06 KB
Newer Older
Kiste's avatar
Kiste committed
1
2
3
4
5
6
7
# -*- coding: utf-8 -*-

import os
import shutil
import requests
from colorama import Fore

Kiste's avatar
Kiste committed
8
import json
Kiste's avatar
Kiste committed
9
10
11
12
13
14
15
def mkdir(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)
    return

BASEURL = 'https://studip.uni-hannover.de/plugins.php/restipplugin/api'

Kiste's avatar
Kiste committed
16
def logik(path, user, password, blacklist, allsemesters):
Kiste's avatar
Kiste committed
17
18
19
20
    mkdir(path)
    s = requests.Session()
    s.auth = (user, password)
    jsemester = s.get(BASEURL + '/courses/semester')
Kiste's avatar
Kiste committed
21
22
23
24
25
26
27
28
29
30
31

    if allsemesters:
        for semester in jsemester.json()['semesters']:
            fetchsemester(s, semester)
    else:
        fetchsemester(s, jsemester.json()['semesters'][-1])


def fetchsemester(s, cursemester):
    semester_id = cursemester['semester_id']
    semester_title = cursemester['title']
Kiste's avatar
Kiste committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    print(Fore.YELLOW + semester_title)
    jcourses = s.get(BASEURL + '/courses/semester/' + semester_id)
    for course in jcourses.json()['courses']:
        course_id = course['course_id']
        course_title = course['title']
        if any(course_title in s for s in blacklist):
            continue
        print(Fore.GREEN + course_title)
        mkdir(path + '/' + course_title)
        jfolder = s.get(BASEURL + '/documents/' + course_id + '/folder')
        for folder in jfolder.json()['folders']:
            folder_id = folder['folder_id']
            folder_name = folder['name']
            print(Fore.BLUE + folder_name)
            jfiles = s.get(BASEURL + '/documents/' + course_id + '/folder/' + folder_id)
            for file in jfiles.json()['documents']:
                document_id = file['document_id']
                filename = file['filename']
                print(Fore.RED + filename)
                f = path + '/' + course_title + '/' + filename
                if not os.path.isfile(f):
                    fdl = s.get(BASEURL + '/documents/' + document_id + '/download', stream=True)
                    if fdl.status_code == 200:
                        with open(f, 'wb') as file:
                            fdl.raw.decode_content = True
                            shutil.copyfileobj(fdl.raw, file)