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

38 lines
982 B
Python

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"]
self.cover_path: Optional[str] = None
def save_image(self):
r = requests.get(self.cover)
if r.status_code == 200:
with tempfile.NamedTemporaryFile("wb", suffix=".jpg", delete=False) as f:
f.write(r.content)
self.cover_path = f.name
else:
print(r.status_code)
def __str__(self) -> str:
return f"{self.artist} - {self.title}"
def __eq__(self, other: object) -> bool:
if isinstance(other, RadioTrack):
return self.title == other.title and self.artist == other.artist
return False