All the things

This commit is contained in:
2022-05-26 14:20:13 +02:00
parent ec7cb4ca57
commit e0445f621e
6 changed files with 336 additions and 6 deletions

View File

@@ -7,6 +7,8 @@ from time import sleep
from radiotrack import RadioTrack
from notifications import SongNotification
from top2000 import load_top2000
def get_current_song():
result = requests.get(
@@ -25,9 +27,11 @@ def putchar(char):
if __name__ == "__main__":
top2000 = load_top2000("2021.csv")
un = SongNotification()
previous = None
try:
while True:
try:
@@ -37,9 +41,14 @@ if __name__ == "__main__":
continue
if previous != track:
print(f"\nNew track: {track}")
print(f"\nNew track: {track}", end="")
track.save_image()
un.notify_song(track)
t2000 = top2000.get(track.id)
if t2000:
print(t2000.position)
else:
print()
un.notify_song(track, t2000)
else:
putchar(".")

View File

@@ -7,6 +7,7 @@ from UserNotifications import (
)
from Foundation import NSURL
from radiotrack import RadioTrack
from top2000 import Top200Song
class UserNotification:
@@ -52,10 +53,9 @@ class SongNotification(UserNotification):
def __init__(self):
super().__init__()
def notify_song(self, song: RadioTrack):
def notify_song(self, song: RadioTrack, top2000: Top200Song = None):
url = NSURL.URLWithString_(f"file://{song.cover_path}")
err = None
atachement = (
UNNotificationAttachment.attachmentWithIdentifier_URL_options_error_(
@@ -70,8 +70,13 @@ class SongNotification(UserNotification):
print(err)
atachement = None
if top2000:
title = f"{top2000.position}: {song.title}"
else:
title = song.title
self.create_notification(
song.title,
title,
song.artist,
attachments=[atachement],
)

View File

@@ -2,9 +2,16 @@ from typing import Optional
import requests
import tempfile
from uuid import UUID
class RadioTrack:
def __init__(self, data: dict) -> None:
try:
self.id = UUID(data["id"])
except ValueError:
self.id = None
self.artist = data["artist"]
self.title = data["name"]
self.cover = data["cover_url"]

46
nporadio2/top2000.py Normal file
View File

@@ -0,0 +1,46 @@
from csv import DictReader
from uuid import UUID
class Top200Song:
def __init__(self, songdict) -> None:
try:
self.id = UUID(songdict["id"])
except ValueError:
self.id = None
self.artist = songdict["artist"]
self.title = songdict["title"]
self.position = int(songdict["position"])
def __str__(self) -> str:
return f"{self.position:4d}: {self.artist} - {self.title}"
def __repr__(self) -> str:
return str(self)
def load_top2000(filename="2021.csv") -> dict[UUID, Top200Song]:
songs = {}
with open(filename) as f:
reader = DictReader(f)
for line in reader:
song = Top200Song(line)
if song.id:
songs[song.id] = song
return songs
if __name__ == "__main__":
from csv import DictReader
songs = []
with open("2021.csv") as f:
reader = DictReader(f)
for line in reader:
print(line)
songs.append(Top200Song(line))
for song in songs:
print(song)