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:
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

View File

@ -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)

View File

@ -50,3 +50,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 = ""