Commit f227922b authored by Kiste's avatar Kiste
Browse files

Migrate to new API

parent 41e0ae64
# Stud.FEED
[Stud.FEED](https://git.finf.uni-hannover.de/kiste/studfeed) erstellt einen ATOM-Feed aus allen Veranstaltungen des aktuellen Semesters aus [Stud.IP](http://www.studip.de/) mittels
der [REST-API](https://docs.studip.de/develop/Entwickler/RESTAPI).
Damit das Skript auch hinter einem Webserver liegen kann, liegt eine FastCGI-Bridge bei.
---
Copyright © 2017 Christian Buschau <christian.buschau@stud.uni-hannover.de>
Copyright © 2017-2018 Christian Buschau <christian.buschau@stud.uni-hannover.de>
This work is free. 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 the COPYING file for more details.
......@@ -10,7 +10,7 @@ CONFIGPATH = '/path/to/config.json'
def application(environ, start_response):
try:
feed = generate_feed(config['user'], config['password']).atom_str()
feed = generate_feed(config['user'], config['password'], false).atom_str()
except:
start_response('500 Internal Server Error', [('Content-Type', 'text/plain')])
return []
......
......@@ -7,11 +7,13 @@ published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
"""
import argparse
from datetime import datetime
import time
import requests
from feedgen.feed import FeedGenerator
BASEURL = 'https://studip.uni-hannover.de/plugins.php/restipplugin/api'
BASEURL = 'https://studip.uni-hannover.de/api.php'
def main():
......@@ -19,12 +21,13 @@ def main():
parser.add_argument('-u', '--user', help='Stud.IP Benutzername', required=True)
parser.add_argument('-p', '--password', help='Stud.IP Passwort', required=True)
parser.add_argument('-o', '--output', help='Datei, in die der ATOM-Feed geschrieben wird', required=True)
parser.add_argument('-v', '--verbose', help='Sag was du tust', action='store_true')
args = parser.parse_args()
generate_feed(args.user, args.password).atom_file(args.output)
generate_feed(args.user, args.password, args.verbose).atom_file(args.output)
def generate_feed(user, password):
def generate_feed(user, password, verbose):
session = requests.Session()
session.auth = (user, password)
fg = FeedGenerator()
......@@ -34,27 +37,41 @@ def generate_feed(user, password):
fg.logo('https://studip.uni-hannover.de/assets/images/favicon.png')
fg.language('de')
json_semester = session.get(BASEURL + '/courses/semester').json()
# get current user
json_user = session.get(BASEURL + '/user').json()
# get current semester
json_semesters = session.get(BASEURL + '/semesters', params='limit=1000').json()
latest = None
for semester in json_semester['semesters']:
if not latest:
latest = semester
else:
if semester['begin'] > latest['begin']:
latest = semester
json_courses = session.get(BASEURL + '/courses/semester/' + latest['semester_id']).json()
for course in json_courses['courses']:
json_news = session.get(BASEURL + '/news/range/' + course['course_id']).json()
for news in json_news['news']:
json_user = session.get(BASEURL + '/user/' + news['user_id']).json()
author = json_user['user']
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)
# look in all courses
json_courses = session.get(BASEURL + '/user/' + json_user['user_id'] + '/courses', params='limit=1000').json()
for key, course in json_courses['collection'].items():
if course['start_semester'] != latest:
continue
if verbose:
print(course['title'])
json_news = session.get(BASEURL + '/course/' + course['course_id'] + '/news').json()
if not json_news['collection']:
continue
for key, news in json_news['collection'].items():
if verbose:
print(' ' + news['topic'])
json_author = session.get(BASEURL + '/user/' + news['user_id']).json()
fe = fg.add_entry()
fe.author(name=str.join(' ', filter(None, (author['title_pre'], author['forename'], author['lastname']))),
email=author['email'])
fe.content(news['body'], type='html')
fe.author(name=json_author['name']['formatted'], email=json_author['email'])
fe.content(news['body_html'], type='html')
fe.id(news['news_id'])
fe.published(news['iso_mkdate'])
fe.published(datetime.fromtimestamp(int(news['mkdate'])).isoformat() + '+00:00')
fe.title(str.join('', ('[', course['title'], '] ', news['topic'])))
fe.updated(news['iso_chdate'])
fe.updated(datetime.fromtimestamp(int(news['chdate'])).isoformat() + '+00:00')
return fg
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