Example #1
0
File: zeya.py Project: sekjal/zeya
def run_server(backend, bind_address, port, bitrate, basic_auth_file=None):
    # Read the library.
    print "Loading library..."

    library_contents = backend.get_library_contents()
    if not library_contents:
        print "Warning: no tracks were found. Check that you've specified " \
            + "the right backend/path."
    # Filter out songs that we won't be able to decode.
    filtered_library_contents = \
        [ s for s in library_contents \
              if decoders.has_decoder(backend.get_filename_from_key(s['key'])) ]
    if not filtered_library_contents and library_contents:
        print "Warning: no playable tracks were found. You may need to " \
            "install one or more decoders."

    try:
        playlists = backend.get_playlists()
    except NotImplementedError:
        playlists = None

    output = { 'library': filtered_library_contents,
               'playlists': playlists }

    library_repr = json.dumps(output, ensure_ascii=False)
    basedir = os.path.abspath(os.path.dirname(os.path.realpath(sys.argv[0])))

    auth_data = None
    if basic_auth_file is not None:
        auth_data = {}
        for line in basic_auth_file:
            s_user, s_pass = split_user_pass(line.rstrip())
            auth_data[s_user] = s_pass
    zeya_handler = ZeyaHandler(backend,
                               library_repr,
                               os.path.join(basedir, 'resources'),
                               bitrate,
                               auth_type=NO_AUTH if basic_auth_file is None else BASIC_AUTH,
                               auth_data=auth_data,
                               )
    try:
        server = IPV6ThreadedHTTPServer((bind_address, port), zeya_handler)
    except socket.error:
        # One possible failure mode (among many others...) is that IPv6 is
        # disabled, which manifests as a socket.error. If this happens, attempt
        # to bind only on IPv4.
        server = ThreadedHTTPServer((bind_address, port), zeya_handler)

    if bind_address != '':
        print "Binding to address %s" % bind_address

    print "Listening on port %d" % (port,)
    # Start up a web server.
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass
    finally:
        server.server_close()
Example #2
0
File: zeya.py Project: lifeeth/Zeya
def run_server(backend, port, bitrate, basic_auth_file=None):
    # Read the library.
    print "Loading library..."
    library_contents = backend.get_library_contents()
    # Filter out songs that we won't be able to decode.
    library_contents = \
        [ s for s in library_contents \
              if decoders.has_decoder(backend.get_filename_from_key(s['key'])) ]
    if not library_contents:
        print "WARNING: no tracks were found. Check that you've specified " \
            + "the right backend/path."
    library_repr = json.dumps(library_contents, ensure_ascii=False)
    basedir = os.path.abspath(os.path.dirname(os.path.realpath(sys.argv[0])))

    auth_data = None
    if basic_auth_file is not None:
        auth_data = {}
        for line in basic_auth_file:
            s_user, s_pass = split_user_pass(line.rstrip())
            auth_data[s_user] = s_pass
    server = ThreadedHTTPServer(
        ('', port),
        ZeyaHandler(backend,
                    library_repr,
                    os.path.join(basedir, 'resources'),
                    bitrate,
                    auth_type=NO_AUTH if basic_auth_file is None else BASIC_AUTH,
                    auth_data=auth_data,
                   ))
    print "Listening on port %d" % (port,)
    # Start up a web server.
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass
    finally:
        server.server_close()
Example #3
0
 def test_has_decoder(self):
     """
     Test decoders.has_decoder.
     """
     self.assertTrue(decoders.has_decoder("/path/to/something.mp3"))
     self.assertFalse(decoders.has_decoder("/path/to/something.m4a"))