Commit 3e32c66d authored by Kiste's avatar Kiste
Browse files

Only download new files

parent 0a6081fa
......@@ -2,6 +2,7 @@
import os
import shutil
import time
import requests
def mkdir(directory):
......@@ -12,29 +13,48 @@ def mkdir(directory):
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'):
neu = True
with open(path + '/.timestamp', 'w+') as timestampfile:
timestampfile.write(newtimestamp)
timestamp = newtimestamp
else:
neu = False
with open(path + '/.timestamp', 'r+') as timestampfile:
timestamp = timestampfile.read()
timestampfile.seek(0)
timestampfile.write(newtimestamp)
timestampfile.truncate()
s = requests.Session()
s.auth = (user, password)
jsemester = s.get(BASEURL + '/courses/semester')
if allsemesters:
for semester in jsemester.json()['semesters']:
holsemester(path + '/' + semester['title'].replace('/', ''), s, blacklist, semester)
holsemester(path + '/' + semester['title'].replace('/', ''), s, blacklist, neu, timestamp, semester)
else:
holsemester(path, s, blacklist, jsemester.json()['semesters'][-1])
holsemester(path, s, blacklist, neu, timestamp, jsemester.json()['semesters'][-1])
def holsemester(path, s, blacklist, cursemester):
mkdir(path)
def holsemester(path, s, blacklist, neu, timestamp, cursemester):
jcourses = s.get(BASEURL + '/courses/semester/' + cursemester['semester_id'])
for course in jcourses.json()['courses']:
if course['title'] in blacklist:
continue
mkdir(path + '/' + course['title'])
jfolder = s.get(BASEURL + '/documents/' + course['course_id'] + '/folder')
for folder in jfolder.json()['folders']:
holordner(path, s, course, folder)
if neu:
jfolder = s.get(BASEURL + '/documents/' + course['course_id'] + '/folder')
for folder in jfolder.json()['folders']:
holordner(path, s, course, folder)
else:
jnew = s.get(BASEURL + '/documents/' + course['course_id'] + '/new/' + timestamp)
for document in jnew.json()['documents']:
holdatei(path, s, course, document)
def holordner(path, s, course, folder):
......@@ -52,6 +72,7 @@ def holdatei(path, s, course, document):
download = s.get(BASEURL + '/documents/' + document['document_id'] + '/download', stream=True)
if download.status_code == 200:
with open(document, 'wb') as file:
print(path + '/' + course['title'] + '/' + 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