def __init__(self, opts, station): """Initilize the interface opts: a dictionary of command line options station: station requested as an argument""" cleanDebug() self.histfile = None self.config = Config() self.sirius = Sirius() self.player = Player(opts) self.options = opts atexit.register(self.onExit) if opts.list: self.list() sys.exit(0) if opts.setup: self.setup() sys.exit(0) self.notification = toBool(self.config.settings.notifications) if station != None: self.play(station) else: self.repl()
class Interface(object): """CLI Interface handler""" def __init__(self, opts, station): """Initilize the interface opts: a dictionary of command line options station: station requested as an argument""" cleanDebug() self.histfile = None self.config = Config() self.sirius = Sirius() self.player = Player(opts) self.options = opts atexit.register(self.onExit) if opts.list: self.list() sys.exit(0) if opts.setup: self.setup() sys.exit(0) self.notification = toBool(self.config.settings.notifications) if station != None: self.play(station) else: self.repl() def userPrompt(self): """Prompt user for station""" try: userinput = raw_input("\npyxis: ").strip() except (EOFError, KeyboardInterrupt): sys.exit(100) return userinput def onExit(self): """Allows cleaning up of dangling resources on exit""" try: readline.write_history_file(self.histfile) except: pass try: self.player.close() except: pass def play(self, stream): """Plays the given stream stream: the station name""" try: log('Play %s' % stream) self.sirius.setStreamByLongName(stream) except: print "Invalid station name. Type 'list' to see available station names" return else: if self.options.record: print "Recording %s. Please use Ctrl+C to stop." % stream else: print "Now playing %s. Please use Ctrl+C to stop." % stream while True: #playing loop if not self.player.playing(): url = self.sirius.getAsxURL() self.player.play(url, stream) playing = self.sirius.nowPlaying() if playing['new'] : if not self.options.quiet: print time.strftime('%H:%M' ) + ' - ' + playing['longName'] + ": " + playing['playing'] if self.notification: import pynotify if pynotify.init("Pyxis"): icon = os.path.dirname(__file__) + '/data/dog_white_outline.svg' n = pynotify.Notification("Sirius", playing['longName'] + ": " + playing['playing'], icon) n.show() try: time.sleep(30) except KeyboardInterrupt: break def repl(self): """Read Eval Print Loop - Interactive mode Allows the user to list, select and change channels without ever leaving the program""" self.histfile = os.path.join(self.config.confpath,"history") completer = Completer([x['longName'] for x in self.sirius.getStreams()]) readline.parse_and_bind("tab: complete") readline.set_completer(completer.complete) # Remove space as a valid word delimiter, since stream # names have spaces in them. readline.set_completer_delims(readline.get_completer_delims().strip(' ')) try: readline.read_history_file(self.histfile) except IOError: pass print "\nWelcome to Pyxis." if self.options.record: print "Recording" print "Enter the name of the station you want to record, type 'list' to see available stations or 'exit' to close the program." else: print "Enter the name of the station you want to listen to, type 'list' to see available stations or 'exit' to close the program." while True: userinput = self.userPrompt() if userinput.lower() == 'list': self.list() continue if userinput.lower() == 'exit': sys.exit(0) self.player.close() self.play(userinput) def list(self): """List all available stations""" station_cat = None nowplaying = self.sirius.getNowPlaying() for x in self.sirius.getStreams(): if station_cat != x['categoryKey']: station_cat = x['categoryKey'] print '\n\n' + '\033[1m' + '[' + station_cat.replace('cat','').capitalize() + ']\n' + '\033[0;0m' channel = x['longName'] if channel in nowplaying: artist = nowplaying[channel]['artist'] song = nowplaying[channel]['song'] print ' * ' + channel.title() + ' (' + song + ', ' + artist + ')' else: print ' * ' + channel.title() print '' def setup(self): """Configuration""" self.config.create()
class Interface(object): """CLI Interface handler""" def __init__(self, opts, station): """Initilize the interface opts: a dictionary of command line options station: station requested as an argument""" cleanDebug() self.histfile = None self.config = Config() self.sirius = Sirius() self.player = Player(opts) self.options = opts atexit.register(self.onExit) if opts.list: self.list() sys.exit(0) if opts.setup: self.setup() sys.exit(0) self.notification = toBool(self.config.settings.notifications) if station != None: self.play(station) else: self.repl() def userPrompt(self): """Prompt user for station""" try: userinput = raw_input("\npyxis: ").strip() except (EOFError, KeyboardInterrupt): sys.exit(100) return userinput def onExit(self): """Allows cleaning up of dangling resources on exit""" try: readline.write_history_file(self.histfile) except: pass try: self.player.close() except: pass def play(self, stream): """Plays the given stream stream: the station name""" try: log('Play %s' % stream) self.sirius.setStreamByLongName(stream) except InvalidStream: print "Invalid station name. Type 'list' to see available station names" return else: if self.options.record: print "Recording %s. Please use Ctrl+C to stop." % stream else: print "Now playing %s. Please use Ctrl+C to stop." % stream while True: #playing loop if not self.player.playing(): url = self.sirius.getAsxURL() self.player.play(url, stream) playing = self.sirius.nowPlaying() if playing['new']: if not self.options.quiet: print time.strftime('%H:%M') + ' - ' + playing[ 'longName'] + ": " + playing['playing'] if self.notification: import pynotify if pynotify.init("Pyxis"): icon = os.path.dirname( __file__) + '/data/dog_white_outline.svg' n = pynotify.Notification( "Sirius", playing['longName'] + ": " + playing['playing'], icon) n.show() try: time.sleep(30) except KeyboardInterrupt: break def repl(self): """Read Eval Print Loop - Interactive mode Allows the user to list, select and change channels without ever leaving the program""" self.histfile = os.path.join(self.config.confpath, "history") completer = Completer( [x['longName'] for x in self.sirius.getStreams()]) readline.parse_and_bind("tab: complete") readline.set_completer(completer.complete) # Remove space as a valid word delimiter, since stream # names have spaces in them. readline.set_completer_delims( readline.get_completer_delims().strip(' ')) try: readline.read_history_file(self.histfile) except IOError: pass print "\nWelcome to Pyxis." if self.options.record: print "Recording" print "Enter the name of the station you want to record, type 'list' to see available stations or 'exit' to close the program." else: print "Enter the name of the station you want to listen to, type 'list' to see available stations or 'exit' to close the program." while True: userinput = self.userPrompt() if userinput.lower() == 'list': self.list() continue if userinput.lower() == 'exit': sys.exit(0) self.player.close() self.play(userinput) def list(self): """List all available stations""" station_cat = None nowplaying = self.sirius.getNowPlaying() for x in self.sirius.getStreams(): if station_cat != x['categoryKey']: station_cat = x['categoryKey'] print '\n\n' + '\033[1m' + '[' + station_cat.replace( 'cat', '').capitalize() + ']\n' + '\033[0;0m' channel = x['longName'] if channel in nowplaying: artist = nowplaying[channel]['artist'] song = nowplaying[channel]['song'] print ' * ' + channel.title( ) + ' (' + song + ', ' + artist + ')' else: print ' * ' + channel.title() print '' def setup(self): """Configuration""" self.config.create()