2020-02-20 22:46:33 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2020-02-22 13:06:56 +01:00
|
|
|
"""Runs playlistcast media server"""
|
2020-02-20 22:46:33 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2020-03-01 19:32:20 +01:00
|
|
|
import asyncio
|
2020-02-21 02:19:41 +01:00
|
|
|
import logging
|
2020-02-20 22:46:33 +01:00
|
|
|
import graphene
|
|
|
|
import tornado.web
|
|
|
|
import tornado.ioloop
|
2020-03-01 19:32:20 +01:00
|
|
|
from playlistcast.protocol import chromecast, ssdp
|
2020-02-20 22:46:33 +01:00
|
|
|
from playlistcast.api import Subscription, Query, Mutation
|
2020-02-27 20:50:16 +01:00
|
|
|
from playlistcast.api import browse
|
2020-02-20 22:46:33 +01:00
|
|
|
|
|
|
|
# https://stackoverflow.com/a/18812776
|
|
|
|
# Add vendor directory to module search path
|
2020-02-22 13:06:56 +01:00
|
|
|
PARENT_DIR = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
VENDOR_DIR = os.path.join(PARENT_DIR, 'vendor/tornadoql')
|
2020-02-20 22:46:33 +01:00
|
|
|
|
2020-02-22 13:06:56 +01:00
|
|
|
sys.path.append(VENDOR_DIR)
|
|
|
|
from tornadoql.tornadoql import GraphQLSubscriptionHandler, GraphQLHandler, GraphiQLHandler # pylint: disable=E0401,C0413
|
2020-02-20 22:46:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
STATIC_PATH = os.path.abspath('frontend/build')
|
2020-03-01 19:32:20 +01:00
|
|
|
LOG = logging.getLogger('playlistcast')
|
2020-02-20 22:46:33 +01:00
|
|
|
|
|
|
|
class IndexHandler(tornado.web.RequestHandler):
|
2020-02-22 13:06:56 +01:00
|
|
|
"""Serve index.html"""
|
|
|
|
# pylint: disable=W0223
|
2020-02-20 22:46:33 +01:00
|
|
|
def get(self):
|
2020-02-22 13:06:56 +01:00
|
|
|
"""Get request"""
|
2020-02-20 22:46:33 +01:00
|
|
|
path = os.path.join(STATIC_PATH, 'index.html')
|
2020-02-22 13:06:56 +01:00
|
|
|
with open(path, 'rb') as file:
|
|
|
|
self.finish(file.read())
|
|
|
|
|
2020-02-20 22:46:33 +01:00
|
|
|
|
2020-03-01 19:32:20 +01:00
|
|
|
def startup():
|
|
|
|
"""Run some scheduled tasks on start"""
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.create_task(ssdp.find_upnp_services())
|
|
|
|
loop.create_task(chromecast.list_devices())
|
|
|
|
LOG.debug('startup tasks complete')
|
|
|
|
|
2020-02-20 22:46:33 +01:00
|
|
|
if __name__ == '__main__':
|
2020-02-21 02:19:41 +01:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2020-02-20 22:46:33 +01:00
|
|
|
# configuration
|
2020-02-22 13:06:56 +01:00
|
|
|
DEBUG = False
|
|
|
|
PORT = 9666
|
|
|
|
HOST = '0.0.0.0'
|
2020-02-20 22:46:33 +01:00
|
|
|
|
|
|
|
# server
|
2020-02-22 13:06:56 +01:00
|
|
|
SCHEMA = graphene.Schema(query=Query, mutation=Mutation, subscription=Subscription)
|
|
|
|
ENDPOINTS = [
|
2020-02-20 22:46:33 +01:00
|
|
|
(r'/subscriptions', GraphQLSubscriptionHandler, dict(opts=dict(sockets=[],
|
|
|
|
subscriptions={}),
|
2020-02-22 13:06:56 +01:00
|
|
|
schema=SCHEMA)),
|
|
|
|
(r'/graphql', GraphQLHandler, dict(schema=SCHEMA)),
|
2020-02-20 22:46:33 +01:00
|
|
|
(r'/graphiql', GraphiQLHandler),
|
|
|
|
(r'/static/(.*)', tornado.web.StaticFileHandler, {'path': STATIC_PATH}),
|
|
|
|
(r'/', IndexHandler),
|
2020-02-27 20:50:16 +01:00
|
|
|
(r'/resource/(.*)', browse.BrowseResourceHandler),
|
2020-02-20 22:46:33 +01:00
|
|
|
]
|
2020-02-22 13:06:56 +01:00
|
|
|
APP = tornado.web.Application(ENDPOINTS)
|
|
|
|
APP.listen(PORT, HOST)
|
2020-02-20 22:46:33 +01:00
|
|
|
|
2020-03-01 19:32:20 +01:00
|
|
|
startup()
|
2020-02-21 02:19:41 +01:00
|
|
|
|
2020-03-01 21:40:46 +01:00
|
|
|
import playlistcast.scheduler # pylint: disable=W0611
|
|
|
|
|
2020-02-20 22:46:33 +01:00
|
|
|
tornado.ioloop.IOLoop.current().start()
|