def setup(self, stdscr): fm_log(logger, 'init baidufm fm cli') self.stdscr = stdscr # init color curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK) curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_BLACK) curses.init_pair(5, curses.COLOR_WHITE, curses.COLOR_BLACK) curses.init_pair(6, curses.COLOR_BLACK, curses.COLOR_MAGENTA) curses.init_pair(7, curses.COLOR_BLACK, curses.COLOR_GREEN) curses.init_pair(8, curses.COLOR_MAGENTA, curses.COLOR_BLACK) curses.init_pair(9, curses.COLOR_GREEN, curses.COLOR_BLACK) curses.init_pair(10, curses.COLOR_RED, curses.COLOR_BLACK) curses.start_color() for i in range(0, curses.COLORS): if i < 10: continue curses.init_pair(i + 1, curses.COLOR_BLACK, i) self.player = choose_player()(self.footer, self.event) self.stdscr.nodelay(0) self.setup_and_draw_screen() self.run()
def init_head(self): title = "BaiduFM" try: user_counts = self.api.get_fm_user_counts() user_name = ' / ' + (user_counts['user_name'] or "Visitor") total_listen = "Listen: %s" % user_counts['counts']['total_listen'] like_songs = "Like: %s" % user_counts['counts']['like_songs'] dislike_songs = "Dislike: %s" % user_counts['counts']['dislike_songs'] if user_counts['user_name'] and self.login_channel: self.login_channel.extend(self.channels) self.channels, self.login_channel = self.login_channel, None except Exception as e: fm_log(logger, "INIT HEAD: %s", str(e.args)) return len_x = len(title) self.head_win.addstr(0, 0, title, curses.color_pair(1)) self.head_win.addstr(0, len_x, user_name, curses.color_pair(2)) len_x += len(user_name) + 2 self.head_win.addstr(0, len_x, total_listen, curses.color_pair(3)) len_x += len(total_listen) + 2 self.head_win.addstr(0, len_x, like_songs, curses.color_pair(4)) len_x += len(like_songs) + 2 self.head_win.addstr(0, len_x, dislike_songs, curses.color_pair(5)) self.head_win.noutrefresh()
def _send_command(self, command): """ send keystroke command to player """ if self.process is None: return try: fm_log(logger, "Command: %s", command.strip()) self.process.stdin.write(command.encode("utf-8")) except Exception as e: fm_log(logger, "Error when sending: %s, msg: %s", command, str(e.args))
def close(self): """ exit (and kill mplayer instance) """ # First close the subprocess self._stop() # Here is fallback solution and cleanup if self.process is not None: os.kill(self.process.pid, 9) fm_log(logger, "KILL process PID: %d", self.process.pid) self.process.wait() self.process = None
def play(self, stream_url): """ use a multimedia player to play a stream """ self.close() opts = self._build_start_opts(stream_url) or '' self.process = subprocess.Popen( opts, shell=False, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT ) fm_log(logger, "CREATE process PID: %d", self.process.pid) t = threading.Thread(target=self.update_status, args=()) t.setDaemon(True) t.start() fm_log(logger, "Player started")
def update_status(self): out_str = ('Title', 'Artist', 'Album', 'Year', 'Comment', 'Genre', 'Volume') try: self.event.clear() # set threading.Event() False out = self.process.stdout while True: subsystem_out = out.readline().decode("utf-8") if subsystem_out == '': break subsystem_out = subsystem_out.strip() fm_log(logger, "User input: %s", subsystem_out) for s in out_str: if s in subsystem_out and ":" in subsystem_out: show_msg = subsystem_out.split(":")[1] self.output_stream.write(show_msg) except Exception as e: fm_log(logger, "Error in update_status thread. Msg: %s", str(e.args)) self.event.set() # set threading.Event() True
def login(self): username = self.api.is_login() if username: if isinstance(username, unicode): username = username.encode('utf-8') self.footer.write('%s 已登录.' % username) return curses.echo() self.login_win.erase() self.login_win.addstr(0, 2, "Username: "******" " * 43, curses.A_UNDERLINE) username = self.login_win.getstr(1, 2) password = '' if len(username) > 0: self.login_win.addstr(3, 2, "Password: "******" " * 43, curses.A_UNDERLINE) password = self.login_win.getstr(4, 2) fm_log(logger, "USERNAME: %s, PWD: %s", username, '*****') if username and password: try: result = self.api.login(username, password, '') login_count = 0 if result: while True: login_count += 1 try: image_to_display(self.login_win, result, 8) except: pass self.login_win.addstr(6, 2, "Captcha(%s): " % result, curses.color_pair(1)) self.login_win.addstr(7, 2, " " * 43, curses.A_UNDERLINE) captcha = self.login_win.getstr(7, 2) result = self.api.login(username, password, captcha) if not result or login_count > 3: break if not result: if isinstance(username, unicode): username = username.encode('utf-8') self.footer.write('%s 登录成功.' % username) except Exception as e: fm_log(logger, "Login error. %s", str(e.args)) self.refresh_body()
def choose_player(): """ Probes the multimedia players which are available on the host system. """ fm_log(logger, "Probing available multimedia players...") implemented_players = Player.__subclasses__() players = [player.PLAYER_CMD for player in implemented_players] fm_log(logger, "Implemented players: %s", ", ".join(players)) for player in implemented_players: try: p = subprocess.Popen( [player.PLAYER_CMD, "--help"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=False ) p.terminate() fm_log(logger, "%s supported.", str(player)) return player except OSError: fm_log(logger, "%s not supported.", str(player))
def play(self): fm_log(logger, "now channel is: %s", self.channels[self.playing][0].encode('utf-8')) if not self.song_links: song_ids = self.api.get_next_play_list(self.channel_id, self.song_id) self.song_links = self.api.get_song_link(song_ids) try: link = self.song_links.pop() except: self.play_selection() try: fm_log(logger, "len(song_links): %d, link info: %s", len(self.song_links), link) song_link = link['linkinfo']['128']['songLink'] self.song_time = link['linkinfo']['128']['time'] self.song_id = link['songId'] self.song_name = link['songName'] self.artist_name = link['artistName'] if isinstance(self.song_name, unicode): self.song_name = self.song_name.encode('utf-8') if isinstance(self.artist_name, unicode): self.artist_name = self.artist_name.encode('utf-8') self.lrc_link = link['lrcLink'] except Exception as e: fm_log(logger, "play_selection not 128: %s", e.args) self.play() return try: if self.player.is_playing(): self.player.close() self.player.play(song_link) self.lrc_dict = self.api.get_lrc(self.lrc_link) self.is_collect = self.api.iscollect(self.song_id) if self.execute_time == 0: s_time = threading.Thread(target=self.song_time_thread, args=()) s_time.setDaemon(True) s_time.start() else: self.execute_time = 1 except OSError: self.footer.write('Are you sure MPlayer is installed?')