Merge pull request 'JSON API route' (#7) from json_api into master

Reviewed-on: https://git.wownero.com/dsc/ircradio/pulls/7
This commit is contained in:
dsc 2023-08-31 01:04:28 +00:00
commit 9a23d7e71f
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

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