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"]["radioTrackPlays"]["data"][0]["radioTracks"] 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!")