diff --git a/ircradio/models.py b/ircradio/models.py index d26a609..ad5114c 100644 --- a/ircradio/models.py +++ b/ircradio/models.py @@ -23,6 +23,7 @@ class Ban(pw.Model): class Meta: database = db + class Song(pw.Model): id = pw.AutoField() date_added = pw.DateTimeField(default=datetime.now) @@ -34,6 +35,17 @@ class Song(pw.Model): karma = pw.IntegerField(default=5, index=True) banned = pw.BooleanField(default=False) + @property + def to_json(self): + return { + "title": self.title, + "utube_id": self.utube_id, + "added_by": self.added_by, + "duration": self.duration, + "karma": self.karma, + "banned": self.banned + } + @staticmethod def delete_song(utube_id: str) -> bool: from ircradio.factory import app diff --git a/ircradio/routes.py b/ircradio/routes.py index eadaa80..1caeb3a 100644 --- a/ircradio/routes.py +++ b/ircradio/routes.py @@ -131,3 +131,12 @@ async def np(): last_song = val await asyncio.sleep(5) + +if settings.json_songs_route: + @app.route(settings.json_songs_route) + async def songs_json(): + from ircradio.models import Song + data = [] + for song in Song.select().filter(Song.banned == False): + data.append(song.to_json) + return jsonify(data) \ No newline at end of file diff --git a/settings.py_example b/settings.py_example index aeb10e4..9755ee8 100644 --- a/settings.py_example +++ b/settings.py_example @@ -51,3 +51,4 @@ liquidsoap_iface = icecast2_mount.replace(".", "(dot)") liquidsoap_max_song_duration = 60 * 11 # seconds re_youtube = r"[a-zA-Z0-9_-]{11}$" +json_songs_route = "" \ No newline at end of file