Files @ e13b62bfdaff
Branch filter:

Location: light9/light9/ascoltami/main.py

drewp@bigasterisk.com
fix some warnings
#!bin/python
import logging
import optparse
import sys
from typing import cast

import gi
from light9.run_local import log
from rdflib import URIRef
from twisted.internet import reactor
from twisted.internet.interfaces import IReactorCore

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.player import Player
from light9.ascoltami.playlist import NoSuchSong, Playlist
from light9.ascoltami.webapp import makeWebApp, songLocation, songUri

reactor = cast(IReactorCore, reactor)


class App(object):

    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 = songUri(graph, URIRef(song))

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

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


if __name__ == "__main__":
    Gst.init(None)

    parser = optparse.OptionParser()
    parser.add_option('--show', help='show URI, like http://light9.bigasterisk.com/show/dance2008', default=showconfig.showUri())
    parser.add_option("-v", "--verbose", action="store_true", help="logging.DEBUG")
    parser.add_option("--twistedlog", action="store_true", help="twisted logging")
    (options, args) = parser.parse_args()

    log.setLevel(logging.DEBUG if options.verbose else logging.INFO)

    if not options.show:
        raise ValueError("missing --show http://...")

    graph = showconfig.getGraph()
    app = App(graph, URIRef(options.show))
    if options.twistedlog:
        from twisted.python import log as twlog
        twlog.startLogging(sys.stderr)
    reactor.listenTCP(networking.musicPlayer.port, makeWebApp(app))
    log.info("listening on %s" % networking.musicPlayer.port)
    reactor.run()