Commit 5b6bc192 authored by Kiste's avatar Kiste
Browse files

Add color, split out stuff

parent 61b1bb8f
......@@ -8,7 +8,7 @@ Rest.IP unterstützt zur Authentifizierung `3-legged OAuth`, `Session` per Cooki
$ studsauger --password "$(pass user@studip)"
oder direkt als Parameter, **dann landet es aber in der Shell-History**. Es kann auch in der `config.yaml` hinterlegt werden, **dann liegt es allerdings im Klartext im Dateisystem**. Es empfieht sich die Verwendung eines [Keyrings](https://pypi.python.org/pypi/keyring). Der Key kann mit
oder direkt als Parameter, **dann landet es aber in der Shell-History**. Es kann auch in der `config.yaml` hinterlegt werden, **dann liegt es allerdings im Klartext im Dateisystem**. Es empfiehlt sich die Verwendung eines [Keyrings](https://pypi.python.org/pypi/keyring). Der Key kann mit
$ keyring set studsauger $user
......
......@@ -12,13 +12,14 @@ setup(
author_email = 'christian.buschau@stud.uni-hannover.de',
packages = ['studsauger'],
install_requires = [
'colorama',
'keyring',
'requests',
'PyYAML'
],
entry_points = {
'console_scripts': [
'studsauger = studsauger.studsauger'
'studsauger = studsauger.studsauger:main'
]
}
)
# -*- coding: utf-8 -*-
import os
import shutil
import requests
from colorama import Fore
def mkdir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
return
BASEURL = 'https://studip.uni-hannover.de/plugins.php/restipplugin/api'
def logik(path, user, password, blacklist):
mkdir(path)
s = requests.Session()
s.auth = (user, password)
jsemester = s.get(BASEURL + '/courses/semester')
semester_id = jsemester.json()['semesters'][-1]['semester_id']
semester_title = jsemester.json()['semesters'][-1]['title']
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)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import shutil
import argparse
import os
import keyring
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
from logik import logik
def main():
parser = argparse.ArgumentParser()
......@@ -79,39 +71,7 @@ def main():
else:
blacklist = {}
mkdir(config['path'])
s = requests.Session()
s.auth = (user, password)
jsemester = s.get(BASEURL + '/courses/semester')
semester_id = jsemester.json()['semesters'][-1]['semester_id']
semester_title = jsemester.json()['semesters'][-1]['title']
print('Lade Semester: ' + 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 config['blacklist']):
continue
print('Lade Kurs: ' + course_title)
mkdir(config['path'] + '/' + course_title)
jfolder = s.get(BASEURL + '/documents/' + course_id + '/folder')
for folder in jfolder.json()['folders']:
folder_id = folder['folder_id']
jfiles = s.get(BASEURL + '/documents/' + course_id + '/folder/' + folder_id)
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 = s.get(BASEURL + '/documents/' + document_id + '/download', 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.')
logik(path, user, password, blacklist)
if __name__ == '__main__':
main()
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