Files
radio2/nporadio2/__main__.py
2022-05-26 14:20:13 +02:00

60 lines
1.3 KiB
Python

import sys
import requests
from time import sleep
from radiotrack import RadioTrack
from notifications import SongNotification
from top2000 import load_top2000
def get_current_song():
result = requests.get(
"https://www.nporadio2.nl/api/miniplayer/info?channel=npo-radio-2"
)
data = result.json()
radio_tracks = data["data"]["radio_track_plays"]["data"][0]["radio_tracks"]
track = RadioTrack(radio_tracks)
return track
def putchar(char):
print(char, end="")
sys.stdout.flush()
if __name__ == "__main__":
top2000 = load_top2000("2021.csv")
un = SongNotification()
previous = None
try:
while True:
try:
track = get_current_song()
except requests.exceptions.SSLError:
putchar(",")
continue
if previous != track:
print(f"\nNew track: {track}", end="")
track.save_image()
t2000 = top2000.get(track.id)
if t2000:
print(t2000.position)
else:
print()
un.notify_song(track, t2000)
else:
putchar(".")
previous = track
sleep(10)
except KeyboardInterrupt:
print("\nBye!")