JSON API route

This commit is contained in:
dsc 2022-03-18 10:25:02 +02:00
parent 21160e17c5
commit 337831ea1d
3 changed files with 22 additions and 0 deletions

View file

@ -23,6 +23,7 @@ class Ban(pw.Model):
class Meta: class Meta:
database = db database = db
class Song(pw.Model): class Song(pw.Model):
id = pw.AutoField() id = pw.AutoField()
date_added = pw.DateTimeField(default=datetime.now) date_added = pw.DateTimeField(default=datetime.now)
@ -34,6 +35,17 @@ class Song(pw.Model):
karma = pw.IntegerField(default=5, index=True) karma = pw.IntegerField(default=5, index=True)
banned = pw.BooleanField(default=False) 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 @staticmethod
def delete_song(utube_id: str) -> bool: def delete_song(utube_id: str) -> bool:
from ircradio.factory import app from ircradio.factory import app

View file

@ -131,3 +131,12 @@ async def np():
last_song = val last_song = val
await asyncio.sleep(5) 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)

View file

@ -50,3 +50,4 @@ liquidsoap_iface = icecast2_mount.replace(".", "(dot)")
liquidsoap_max_song_duration = 60 * 11 # seconds liquidsoap_max_song_duration = 60 * 11 # seconds
re_youtube = r"[a-zA-Z0-9_-]{11}$" re_youtube = r"[a-zA-Z0-9_-]{11}$"
json_songs_route = ""