Files @ 3d58c1c78f1f
Branch filter:

Location: light9/light9/ascoltami/main.py

drewp@bigasterisk.com
port ascoltami from cyclone to starlette
#!bin/python
import logging
import optparse
import sys
from typing import cast

import gi
from rdflib import URIRef
from starlette.applications import Starlette
from starlette.routing import Route
from starlette_exporter import PrometheusMiddleware, handle_metrics
from twisted.internet import reactor
from twisted.internet.interfaces import IReactorCore

from light9.run_local import log

gi.require_version('Gst', '1.0')
gi.require_version('Gtk', '3.0')

from gi.repository import Gst  # type: ignore

from light9 import networking, showconfig
from light9.ascoltami import webapp
from light9.ascoltami.player import Player
from light9.ascoltami.playlist import NoSuchSong, Playlist

reactor = cast(IReactorCore, reactor)


class Ascoltami:

    def __init__(self, graph, show):
        self.graph = graph
        self.player = Player(onEOS=self.onEOS, autoStopOffset=0)
        self.show = show
        self.playlist = Playlist.fromShow(graph, show)

    def onEOS(self, song):
        self.player.pause()
        self.player.seek(0)

        thisSongUri = webapp.songUri(self.graph, URIRef(song))

        try:
            nextSong = self.playlist.nextSong(thisSongUri)
        except NoSuchSong:  # we're at the end of the playlist
            return

        self.player.setSong(webapp.songLocation(self.graph, nextSong), play=False)


def main():
    Gst.init(None)

    graph = showconfig.getGraph()
    asco = Ascoltami(graph, showconfig.showUri())

    app = Starlette(
        debug=True,
        routes=[
            Route("/config", webapp.get_config),
            Route("/time", webapp.get_time, methods=["GET"]),
            Route("/time", webapp.post_time, methods=["POST"]),
            Route("/time/stream", webapp.timeStream),
            Route("/song", webapp.post_song, methods=["POST"]),
            Route("/songs", webapp.get_songs),
            Route("/seekPlayOrPause", webapp.post_seekPlayOrPause),
            Route("/output", webapp.post_output, methods=["POST"]),
            Route("/go", webapp.post_goButton, methods=["POST"]),
        ],
    )

    app.add_middleware(PrometheusMiddleware)
    app.add_route("/metrics", handle_metrics)

    app.state.graph = graph
    app.state.show = asco.show
    app.state.player = asco.player

    return app


app = main()