Example #1
0
 def _goto(self, model=None, uri=None):
     """真正的跳转逻辑"""
     if model is None:
         try:
             model = resolve(uri)
         except ResolveFailed:
             model = None
     else:
         uri = reverse(model)
     if not uri.startswith('fuo://'):
         uri = 'fuo://' + uri
     with self._app.create_action('-> {}'.format(uri)) as action:
         if model is not None:
             self._render_model(model)
         else:
             try:
                 self.router.dispatch(uri, {'app': self._app})
             except NotFound:
                 action.failed('not found.'.format(uri))
                 return
     self._last_uri = self.current_uri
     if model is not None:
         self.current_uri = reverse(model)
     else:
         self.current_uri = uri
Example #2
0
    def play(self, s):
        if s.startswith('fuo://'):
            song = resolve(s)
            if song is not None:
                self.player.play_song(song)
            return
        elif s.startswith('http'):
            return self.player.play(s, video=False)

        # 取每个提供方的第一个搜索结果
        source_song_map = defaultdict()
        for result in self.library.search(s):
            for song in result.songs:
                if song.source in source_song_map:
                    break
                source_song_map[song.source] = song
        songs = list(source_song_map.values())

        if songs:
            songs = sorted(songs,
                           key=lambda song: score(s, repr_song(song)),
                           reverse=True)
            msg = 'select:\t{}\n'.format(show_song(songs[0], brief=True))
            self.player.play_song(songs[0])
            lines = []
            for song in songs[1:]:
                lines.append('\t' + show_song(song, brief=True))
            if lines:
                msg += 'options::\n' + '\n'.join(lines)
            return msg
        else:
            return 'No song has been found.'
Example #3
0
 def load(self):
     """解析文件,初始化自己"""
     self.models = []
     filepath = Path(self.fpath)
     name = filepath.stem
     stat_result = filepath.stat()
     self.updated_at = stat_result.st_mtime
     self.name = name
     if name == DEFAULT_COLL_SONGS:
         self.type = CollectionType.sys_song
     elif name == DEFAULT_COLL_ALBUMS:
         self.type = CollectionType.sys_album
     else:
         self.type = CollectionType.mixed
     with filepath.open(encoding='utf-8') as f:
         for line in f:
             try:
                 model = resolve(line)
             except ResolverNotFound:
                 logger.warning('resolver not found for line:%s', line)
                 model = None
             except ResolveFailed:
                 logger.warning('invalid line: %s', line)
                 model = None
             if model is not None:
                 if model.exists is ModelExistence.no:
                     self._has_nonexistent_models = True
                 self.models.append(model)
Example #4
0
 def add(self, furi_list):
     playlist = self.playlist
     for furi in furi_list:
         furi = furi.strip()
         obj = resolve(furi)
         if obj is not None:
             obj_type = type(obj).meta.model_type
             if obj_type == ModelType.song:
                 playlist.add(obj)
             elif obj_type == ModelType.playlist:
                 for song in obj.songs:
                     playlist.add(song)
Example #5
0
    def load(self):
        """解析文件,初始化自己"""
        self.models = []
        filepath = Path(self.fpath)
        name = filepath.stem
        stat_result = filepath.stat()
        self.updated_at = datetime.fromtimestamp(stat_result.st_mtime)
        self.name = name
        if name == DEFAULT_COLL_SONGS:
            self.type = CollectionType.sys_song
        elif name == DEFAULT_COLL_ALBUMS:
            self.type = CollectionType.sys_album
        else:
            self.type = CollectionType.mixed

        # parse file content
        with filepath.open(encoding='utf-8') as f:
            first = f.readline()
            lines = []
            if first == TOML_DELIMLF:
                is_valid = True
                for line in f:
                    if line == TOML_DELIMLF:
                        break
                    else:
                        lines.append(line)
                else:
                    logger.warning('the metadata is invalid, will ignore it')
                    is_valid = False
                if is_valid is True:
                    toml_str = ''.join(lines)
                    metadata = tomlkit.parse(toml_str)
                    self._loads_metadata(metadata)
                    lines = []
            else:
                lines.append(first)

            for line in itertools.chain(lines, f):
                if not line.strip():  # ignore empty lines
                    continue
                try:
                    model = resolve(line)
                except ResolverNotFound:
                    logger.warning('resolver not found for line:%s', line)
                    model = None
                except ResolveFailed:
                    logger.warning('invalid line: %s', line)
                    model = None
                if model is not None:
                    if model.exists is ModelExistence.no:
                        self._has_nonexistent_models = True
                    self.models.append(model)
Example #6
0
 def add(self, furi_list):
     playlist = self.playlist
     for furi in furi_list:
         furi = furi.strip()
         obj = resolve(furi)
         if obj is not None:
             obj_type = type(obj).meta.model_type
             if obj_type == ModelType.song:
                 playlist.add(obj)
             elif obj_type == ModelType.playlist:
                 songs = to_readall_reader(obj, "songs").readall()
                 for song in songs:
                     playlist.add(song)
Example #7
0
 def load(self):
     """解析文件,初始化自己"""
     filepath = self.fpath
     filename = filepath.rsplit('/')[-1]
     name, _ = filename.split('.')
     stat_result = os.stat(filepath)
     self.updated_at = stat_result.st_mtime
     self.name = name
     with open(filepath, encoding='utf-8') as f:
         for line in f:
             try:
                 model = resolve(line)
             except ResolverNotFound:
                 logger.warn('resolver not found for line:%s', line)
                 model = None
             except ResolveFailed:
                 logger.warn('invalid line: %s', line)
                 model = None
             if model is not None:
                 self.models.append(model)