-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from muesli-hd/api_new_lectures
Contribute new API usage example 🔥
- Loading branch information
Showing
4 changed files
with
102 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# api_tools/sample_requests.py | ||
# | ||
# This file is part of MUESLI. | ||
# | ||
# Copyright (C) 2021, Christian Heusel <christian (at) heusel.eu> | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import threading | ||
import logging | ||
import requests | ||
import coloredlogs | ||
|
||
from header import authenticate, STATIC_HEADERS, MUESLI_URL | ||
|
||
# This could be replaced by cmd input vars | ||
WAIT_TIME_SECONDS = 60 * 60 # 1h | ||
USERNAME = "[email protected]" | ||
SEMESTER = "2021 SS" | ||
|
||
|
||
def get(endpoint, header): | ||
endpoint = MUESLI_URL + endpoint | ||
request = requests.get(endpoint, headers=header) | ||
return request.json() | ||
|
||
|
||
def get_lectures(): | ||
headers = authenticate(username=USERNAME, save=True) | ||
endpoint = "/api/v1/lectures" | ||
return get(endpoint, headers) | ||
|
||
|
||
def extract_lecture_titles(l): | ||
return [r["name"] for r in l if r["term"] == SEMESTER] | ||
|
||
|
||
def run_event_loop(): | ||
ticker = threading.Event() | ||
old = set(extract_lecture_titles(get_lectures())) | ||
|
||
while not ticker.wait(WAIT_TIME_SECONDS): | ||
lectures = get_lectures() | ||
current = set(extract_lecture_titles(lectures)) | ||
if current.difference(old): | ||
logging.info("New lecture found: %s", current.difference(old)) | ||
else: | ||
logging.info("No new lecture found!") | ||
old = current | ||
|
||
|
||
def main(): | ||
coloredlogs.install(level=logging.DEBUG, fmt='%(asctime)s → %(message)s') | ||
logging.info("Will check every %s seconds for new lectures on \"%s\"!", | ||
WAIT_TIME_SECONDS, MUESLI_URL) | ||
run_event_loop() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
requests | ||
coloredlogs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters