def get_zone(zone_name):
    import re
    devices = discover()
    cache_name = "{}_ip.txt".format(zone_name)
    if devices:
        for device in devices:
            if device.player_name == zone_name:
                # Save the well known IP of the device for future cache
                # When discover() fails need to fallback to this solution
                #try:
                f = open(cache_name, "w+")
                f.write(device.ip_address)
                f.close()
                return device
    if not devices or len(devices) == 0:
        print("WARNING: No devices found through discover.")
    # Device not found, try be known IP
    try:
        if os.path.isfile(cache_name) and os.access(cache_name, os.R_OK):
            f = open(cache_name, "r")
            line = f.readline()
            ip_address = re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',
                                   line).group()
            f.close()
            print("Using cached IP:{} for zone:{}".format(
                ip_address, zone_name))
            device = SoCo(ip_address)
            return device
        else:
            print("No cached record for zone")
    except:
        print("Exception reading cached record")
        return None
def main():
    # Settings
    args = parse_args()
    print(" Will use the following settings:\n"
          " Zone: {args.zone}\n"
          " IP of this machine: {args.ip}\n"
          " Use port: {args.port}".format(args=args))

    # Get the zone
    zone = get_zone(args.zone)
    # Check if a zone by the given name was found
    if zone is None:
        print("No Sonos player named '{}'. Player names are {}"\
              .format(args.zone, discover()))
        sys.exit(1)

    # Check whether the zone is a coordinator (stand alone zone or master of a group)
    if not zone.is_coordinator:
        print("The zone '{}' is not a group master, and therefore cannot "
              "play music. Please use '{}' in stead"\
              .format(args.zone, zone.group.coordinator.player_name))
        sys.exit(2)

    try:
        load_music_files()
        http_server = get_server(args.port, 0, None)
        http_server.root_path = "http://{}:{}".format(args.ip, args.port)
        http_server.zone = zone
        http_server.serve_forever()
    except KeyboardInterrupt:
        print("Exiting")
        http_server.server_close()
        http_server.socket.close()
Exemple #3
0
def main():
    # Settings
    args = parse_args()
    print(" Will use the following settings:\n"
          " Zone: {args.zone}\n"
          " IP of this machine: {args.ip}\n"
          " Use port: {args.port}".format(args=args))

    # Get the zone
    zone = by_name(args.zone)

    # Check if a zone by the given name was found
    if zone is None:
        zone_names = [zone_.player_name for zone_ in discover()]
        print("No Sonos player named '{}'. Player names are {}"\
              .format(args.zone, zone_names))
        sys.exit(1)

    # Check whether the zone is a coordinator (stand alone zone or
    # master of a group)
    if not zone.is_coordinator:
        print("The zone '{}' is not a group master, and therefore cannot "
              "play music. Please use '{}' in stead"\
              .format(args.zone, zone.group.coordinator.player_name))
        sys.exit(2)

    # Setup and start the http server
    server = HttpServer(args.port)
    server.start()

    # When the http server is setup you can really add your files in
    # any way that is desired. The source code for
    # add_random_file_from_present_folder is just an example, but it may be
    # helpful in figuring out how to format the urls
    try:
        add_random_file_from_present_folder(args.ip, args.port, zone)
        # Remember the http server runs in its own daemonized thread, so it is
        # necessary to keep the main thread alive. So sleep for 3 years.
        time.sleep(10**8)
    except KeyboardInterrupt:
        server.stop()
Exemple #4
0
def main():
    # Settings
    args = parse_args()
    print(" Will use the following settings:\n"
          " Zone: {args.zone}\n"
          " IP of this machine: {args.ip}\n"
          " Use port: {args.port}".format(args=args))

    # Get the zone
    zone = by_name(args.zone)

    # Check if a zone by the given name was found
    if zone is None:
        zone_names = [zone_.player_name for zone_ in discover()]
        print("No Sonos player named '{}'. Player names are {}"\
              .format(args.zone, zone_names))
        sys.exit(1)

    # Check whether the zone is a coordinator (stand alone zone or
    # master of a group)
    if not zone.is_coordinator:
        print("The zone '{}' is not a group master, and therefore cannot "
              "play music. Please use '{}' in stead"\
              .format(args.zone, zone.group.coordinator.player_name))
        sys.exit(2)

    # Setup and start the http server
    server = HttpServer(args.port)
    server.start()

    # When the http server is setup you can really add your files in
    # any way that is desired. The source code for
    # add_random_file_from_present_folder is just an example, but it may be
    # helpful in figuring out how to format the urls
    try:
        add_random_file_from_present_folder(args.ip, args.port, zone)
        # Remember the http server runs in its own daemonized thread, so it is
        # necessary to keep the main thread alive. So sleep for 3 years.
        time.sleep(10**8)
    except KeyboardInterrupt:
        server.stop()
Exemple #5
0
def main():
    # Settings
    global _FINISH
    port = 61823
    args = parse_args()
    docroot = args.docroot
    here = os.getcwd()

    # Get the zone
    zone = by_name(args.zone)

    # Check if a zone by the given name was found
    if zone is None:
        zone_names = [zone_.player_name for zone_ in discover()]
        print("No Sonos player named '{}'. Player names are {}".format(args.zone,
                                                                       zone_names))
        sys.exit(1)

    # Check whether the zone is a coordinator (stand alone zone or
    # master of a group)
    if not zone.is_coordinator:
        msg = "The zone '{}' is not a group master, and therefore cannot ".format(args.zone)
        msg += "play music. Please use '{}' instead".format(zone.group.coordinator.player_name)
        print msg
        sys.exit(2)
    if args.party:
        zone.partymode()

    # Setup and start the http server
    server = HttpServer(docroot, port)
    server.start()

    zone.clear_queue()
    try:
        play_tracks(port, args, here, zone, docroot)
    except KeyboardInterrupt:
        server.stop()
    zone.clear_queue()
    print '\n'
def get_coordinator_device():
    return [device for device in discover() if device.is_coordinator][0]