Commit 22fca2ae authored by Kiste's avatar Kiste
Browse files

Cleanup

parent de806101
# -*- codding: utf-8 -*-
from .studsauger import main
main()
# -*- coding: utf-8 -*-
"""
This program is free software. It comes without any warranty, to the extent
permitted by applicable law. You can redistribute it and/or modify it under the
terms of the Do What The Fuck You Want To Public License, Version 2, as
published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
"""
import json
import os
import shutil
import time
import requests
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, allsemesters):
mkdir(path)
newtimestamp = str(int(time.time()))
if not os.path.isfile(path + '/.timestamp'):
with open(path + '/.timestamp', 'w+') as timestampfile:
timestampfile.write(newtimestamp)
timestamp = ''
else:
with open(path + '/.timestamp', 'r+') as timestampfile:
timestamp = '/' + timestampfile.read()
timestampfile.seek(0)
timestampfile.write(newtimestamp)
timestampfile.truncate()
session = requests.Session()
session.auth = (user, password)
jsemester = session.get(BASEURL + '/courses/semester')
if allsemesters:
for semester in jsemester.json()['semesters']:
holsemester(path + '/' + semester['title'].replace('/', ''), session, blacklist, timestamp, semester)
else:
aktuell = None
for semester in jsemester.json()['semesters']:
if not aktuell:
aktuell = semester
else:
if semester['begin'] > aktuell['begin']:
aktuell = semester
holsemester(path, session, blacklist, timestamp, aktuell)
def holsemester(path, session, blacklist, timestamp, cursemester):
jcourses = session.get(BASEURL + '/courses/semester/' + cursemester['semester_id'])
for course in jcourses.json()['courses']:
if course['title'] in blacklist:
continue
mkdir(path + '/' + course['title'])
jnew = session.get(BASEURL + '/documents/' + course['course_id'] + '/new' + timestamp)
try:
for document in jnew.json()['documents']:
holdatei(path, session, course, document)
except json.decoder.JSONDecodeError:
print('Die Veranstaltung "' + course['title'] + '" hat keine Dokumente.')
def holdatei(path, session, course, document):
target = path + '/' + course['title'].replace('/', '') + '/' + document['filename']
download = session.get(BASEURL + '/documents/' + document['document_id'] + '/download', stream=True)
if download.status_code == 200:
print(path + '/' + course['title'].replace('/', '') + '/' + document['filename'])
with open(target, 'wb') as file:
download.raw.decode_content = True
shutil.copyfileobj(download.raw, file)
......@@ -9,6 +9,10 @@ published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
import argparse
import json
import os
import shutil
import time
import requests
try:
import keyring
......@@ -17,16 +21,20 @@ except ImportError:
else:
usekeyring = True
from .logik import logik
BASEURL = 'https://studip.uni-hannover.de/plugins.php/restipplugin/api'
def main():
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')
parser.add_argument('-b', '--blacklist',
help='Blacklist für Veranstaltungen, mehrmals angeben für mehrere Veranstaltungen',
action='append')
parser.add_argument('-a', '--allsemesters', help='Alle Semester statt nur dem aktuellen', action="store_true")
parser.add_argument('-c', '--config', help='JSON-Konfigurationsdatei, wird ohne Angabe in $XDG_CONFIG_HOME/studsauger/config.json oder $HOME/.config/studsauger/config.json gesucht')
parser.add_argument('-c', '--config',
help='JSON-Konfigurationsdatei, wird ohne Angabe in $XDG_CONFIG_HOME/studsauger/config.json oder $HOME/.config/studsauger/config.json gesucht')
args = parser.parse_args()
configpath = None
......@@ -91,4 +99,65 @@ def main():
else:
allsemesters = False
logik(path, user, password, blacklist, allsemesters)
mkdir(path)
newtimestamp = str(int(time.time()))
if not os.path.isfile(path + '/.timestamp'):
with open(path + '/.timestamp', 'w+') as timestampfile:
timestampfile.write(newtimestamp)
timestamp = ''
else:
with open(path + '/.timestamp', 'r+') as timestampfile:
timestamp = '/' + timestampfile.read()
timestampfile.seek(0)
timestampfile.write(newtimestamp)
timestampfile.truncate()
session = requests.Session()
session.auth = (user, password)
json_semester = session.get(BASEURL + '/courses/semester').json()
if allsemesters:
for semester in json_semester['semesters']:
get_semester(path + '/' + semester['title'].replace('/', ''), session, blacklist, timestamp, semester)
else:
latest = None
for semester in json_semester['semesters']:
if not latest:
latest = semester
else:
if semester['begin'] > latest['begin']:
latest = semester
get_semester(path, session, blacklist, timestamp, latest)
def mkdir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
return
def get_semester(path, session, blacklist, timestamp, cursemester):
json_courses = session.get(BASEURL + '/courses/semester/' + cursemester['semester_id']).json()
for course in json_courses['courses']:
if course['title'] in blacklist:
continue
mkdir(path + '/' + course['title'])
json_new = session.get(BASEURL + '/documents/' + course['course_id'] + '/new' + timestamp).json()
try:
for document in json_new['documents']:
get_file(path, session, course, document)
except json.decoder.JSONDecodeError:
print('Die Veranstaltung "' + course['title'] + '" hat keine Dokumente.')
def get_file(path, session, course, document):
target = path + '/' + course['title'].replace('/', '') + '/' + document['filename']
download = session.get(BASEURL + '/documents/' + document['document_id'] + '/download', stream=True)
if download.status_code == 200:
print(path + '/' + course['title'].replace('/', '') + '/' + document['filename'])
with open(target, 'wb') as file:
download.raw.decode_content = True
shutil.copyfileobj(download.raw, file)
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