Commit 992a9e1a authored by Kiste's avatar Kiste
Browse files

Migrate to new API, allsemesters broken

parent dcabcd71
# TODO
- Keine Doku schreiben
- Alle Semester runterladen ist broken
......@@ -9,6 +9,7 @@ published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
import argparse
import json
import os
import re
import shutil
import time
......@@ -21,7 +22,7 @@ except ImportError:
else:
usekeyring = True
BASEURL = 'https://studip.uni-hannover.de/plugins.php/restipplugin/api'
BASEURL = 'https://studip.uni-hannover.de/api.php'
def main():
......@@ -29,9 +30,8 @@ def main():
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('-i', '--ignore', help='Blacklist für Dateinamen als regex')
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')
......@@ -92,6 +92,12 @@ def main():
blacklist = config['blacklist']
else:
blacklist = {}
if args.ignore:
ignore = args.ignore
elif 'ignore' in config:
ignore = config['ignore']
else:
ignore = None
if args.allsemesters:
allsemesters = True
elif 'allsemesters' in config:
......@@ -101,63 +107,61 @@ def main():
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()
json_user = session.get(BASEURL + '/user').json()
json_semesters = session.get(BASEURL + '/semesters', params='limit=1000').json()
json_courses = session.get(BASEURL + '/user/' + json_user['user_id'] + '/courses', params='limit=1000').json()
if not allsemesters:
latest=None
unixnow = int(time.time())
for key, semester in json_semesters['collection'].items():
if unixnow >= semester['begin'] and unixnow <= semester['end']:
latest = key
break
if not latest:
print('Aktuelles Semester nicht gefunden')
exit(1)
if allsemesters:
for semester in json_semester['semesters']:
get_semester(path + '/' + semester['title'].replace('/', ''), session, blacklist, timestamp, semester)
if os.path.isfile(path + '/.database.json'):
with open(path + '/.database.json', 'r') as databasefile:
database = json.load(databasefile)
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)
database = {}
for key, course in json_courses['collection'].items():
if not allsemesters and course['start_semester'] != latest:
continue
if course['title'] in blacklist:
continue
print('=== ' + course['title'] + ' ===')
mkdir(path + '/' + course['title'].replace('/', '_'))
json_files = session.get(BASEURL + '/course/' + course['course_id'] + '/files', params='limit=1000').json()
for key, file in json_files['collection'].items():
if not file['documents']:
continue
for key, document in file['documents'].items():
if re.match(ignore, document['filename']):
continue
date = ''
if document['file_id'] in database:
if database[document['file_id']] == document['chdate']:
continue
else:
date = document['chdate']
database[document['file_id']] = document['chdate']
print(document['filename'] + date)
target = path + '/' + course['title'].replace('/', '_') + '/' + document['filename'] + date
download = session.get(BASEURL + '/file/' + document['file_id'] + '/content', stream=True)
with open(target, 'wb') as downloadfile:
download.raw.decode_content = True
shutil.copyfileobj(download.raw, downloadfile)
with open(path + '/.database.json', 'w') as databasefile:
json.dump(database, databasefile, indent=4)
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'].replace('/', ''))
try:
json_new = session.get(BASEURL + '/documents/' + course['course_id'] + '/new' + timestamp).json()
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