51 lines
1.0 KiB
Python
51 lines
1.0 KiB
Python
import sys
|
|
import requests
|
|
|
|
from time import sleep
|
|
|
|
|
|
from radiotrack import RadioTrack
|
|
from notifications import SongNotification
|
|
|
|
|
|
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__":
|
|
|
|
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}")
|
|
track.save_image()
|
|
un.notify_song(track)
|
|
else:
|
|
putchar(".")
|
|
|
|
previous = track
|
|
|
|
sleep(10)
|
|
except KeyboardInterrupt:
|
|
print("\nBye!")
|