Commit 61b1bb8f authored by Kiste's avatar Kiste
Browse files

Add setuptools

parent 33ac2269
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from setuptools import setup
setup(
name = 'studsauger',
version = '1.0',
description = 'Stud.IP download tool',
url = 'https://git.finf.uni-hannover.de/kiste/studsauger',
author = 'Christian Buschau',
author_email = 'christian.buschau@stud.uni-hannover.de',
packages = ['studsauger'],
install_requires = [
'keyring',
'requests',
'PyYAML'
],
entry_points = {
'console_scripts': [
'studsauger = studsauger.studsauger'
]
}
)
#!/usr/bin/env python3
import os
import shutil
import argparse
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
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('-c', '--config', help='YAML-Konfigurationsdatei, wird ohne Angabe in $XDG_CONFIG_HOME/studsauger/config.yaml oder $HOME/.config/studsauger/config.yaml gesucht')
args = parser.parse_args()
configpath = None
if args.config:
if os.path.isfile(args.config):
configpath = args.config
else:
print('Die Konfigurationsdatei "' + args.config + '" existiert nicht!')
exit(1)
elif os.path.isfile(os.environ['XDG_CONFIG_HOME'] + '/studsauger/config.yaml'):
configpath = os.environ['XDG_CONFIG_HOME'] + '/studsauger/config.yaml'
elif os.path.isfile(os.environ['HOME'] + '/.config/studsauger/config.yaml'):
configpath = os.environ['HOME'] + '/.config/studsauger/config.yaml'
if configpath:
with open(configpath, 'r') as stream:
try:
config = yaml.load(stream)
except yaml.YAMLError as exc:
print(exc)
else:
config = {}
if args.path:
path = args.path
elif 'path' in config:
path = config['path']
else:
print('Kein Pfad angegeben!')
exit(1)
if args.user:
user = args.user
elif 'user' in config:
user = config['user']
else:
print('Kein Benutzer angegeben!')
exit(1)
keyringpw = keyring.get_password('studsauger', user)
if keyringpw:
password = keyringpw
if args.password or 'password' in config:
print('Passwort für Benutzer "' + user + '" ist im Keyring und darf nicht angegeben werden!')
exit(1)
elif args.password:
password = args.password
elif 'password' in config:
password = config['password']
else:
print('Kein Passwort angegeben!')
exit(1)
if args.blacklist:
blacklist = args.blacklist
elif 'blacklist' in config:
blacklist = config['blacklist']
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.')
# -*- codding: utf-8 -*-
from .studsauger import main
main()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import shutil
import argparse
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
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('-c', '--config', help='YAML-Konfigurationsdatei, wird ohne Angabe in $XDG_CONFIG_HOME/studsauger/config.yaml oder $HOME/.config/studsauger/config.yaml gesucht')
args = parser.parse_args()
configpath = None
if args.config:
if os.path.isfile(args.config):
configpath = args.config
else:
print('Die Konfigurationsdatei "' + args.config + '" existiert nicht!')
exit(1)
elif os.path.isfile(os.environ['XDG_CONFIG_HOME'] + '/studsauger/config.yaml'):
configpath = os.environ['XDG_CONFIG_HOME'] + '/studsauger/config.yaml'
elif os.path.isfile(os.environ['HOME'] + '/.config/studsauger/config.yaml'):
configpath = os.environ['HOME'] + '/.config/studsauger/config.yaml'
if configpath:
with open(configpath, 'r') as stream:
try:
config = yaml.load(stream)
except yaml.YAMLError as exc:
print(exc)
else:
config = {}
if args.path:
path = args.path
elif 'path' in config:
path = config['path']
else:
print('Kein Pfad angegeben!')
exit(1)
if args.user:
user = args.user
elif 'user' in config:
user = config['user']
else:
print('Kein Benutzer angegeben!')
exit(1)
keyringpw = keyring.get_password('studsauger', user)
if keyringpw:
password = keyringpw
if args.password or 'password' in config:
print('Passwort für Benutzer "' + user + '" ist im Keyring und darf nicht angegeben werden!')
exit(1)
elif args.password:
password = args.password
elif 'password' in config:
password = config['password']
else:
print('Kein Passwort angegeben!')
exit(1)
if args.blacklist:
blacklist = args.blacklist
elif 'blacklist' in config:
blacklist = config['blacklist']
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.')
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