def show_song(song, uri_length=None, brief=False): """以一行文字的方式显示一首歌的信息 :param uri_length: 控制 song uri 的长度 :param brief: 是否只显示简要信息 """ artists_name = song.artists_name_display title = _fit_text(song.title_display, 18, filling=False) album_name = song.album_name_display song_uri = get_url(song) if uri_length is not None: song_uri = _fit_text(song_uri, uri_length) if brief: artists_name = _fit_text(artists_name, 20, filling=False) s = '{song}\t# {title} - {artists_name}'.format( song=song_uri, title=title, artists_name=artists_name) return s # XXX: 这个操作可能会产生网络请求 album_uri = get_url(song.album) artists_uri = ','.join(get_url(artist) for artist in song.artists) msgs = ( 'provider: {}'.format(song.source), ' uri: {}'.format(song_uri), ' title: {}'.format(song.title), 'duration: {}'.format(song.duration), ' url: {}'.format(song.url), ' artists: {}\t# {}'.format(artists_uri, artists_name), ' album: {}\t# {}'.format(album_uri, album_name), ) return '\n'.join(msgs)
def show_album(album, brief=False): if brief: return '{uri}\t# {name} - {artists_name}'.format( uri=get_url(album), name=album.name, artists_name=album.artists_name) msgs = [ 'provider: {}'.format(album.source), 'identifier: {}'.format(album.identifier), 'name: {}'.format(album.name), ] if album.artists is not None: artists = album.artists artists_id = ','.join([str(artist.identifier) for artist in artists]) artists_name = ','.join([artist.name for artist in artists]) msgs_artists = [ 'artists: {}\t#{}'.format(artists_id, artists_name) ] msgs += msgs_artists msgs_songs_header = ['songs::'] msgs_songs = ['\t' + each for each in show_songs(album.songs).split('\n')] msgs += msgs_songs_header msgs += msgs_songs return '\n'.join(msgs)
def fetchMore(self, _=QModelIndex()): expect_len = 10 albums = list(itertools.islice(self.albums_g, expect_len)) acture_len = len(albums) colors = [ random.choice(list(COLORS.values())) for _ in range(0, acture_len) ] if acture_len < expect_len: self._maybe_has_more = False begin = len(self.albums) self.beginInsertRows(QModelIndex(), begin, begin + acture_len - 1) self.albums.extend(albums) self.colors.extend(colors) self.endInsertRows() # FIXME: since album.cover may trigger web request, # this may block the UI for album in albums: cover = album.cover if cover: # check if cover url is valid # FIXME: check if cover is a media object if not isinstance(cover, str): cover = cover.url self.fetch_image(cover, self._fetch_image_callback(album), uid=get_url(album) + '/cover')
def cb(future): if not future.done(): return content = future.result() img = QImage() img.loadFromData(content) pixmap = QPixmap(img) uri = get_url(album) self.pixmaps[uri] = pixmap
def show_artist(artist, brief=False): if brief: return '{uri}\t# {name}'.format(uri=get_url(artist), name=artist.name) msgs = [ 'provider: {}'.format(artist.source), 'identifier: {}'.format(artist.identifier), 'name: {}'.format(artist.name), ] if artist.songs: songs_header = ['songs::'] songs = ['\t' + each for each in show_songs(artist.songs).split('\n')] msgs += songs_header msgs += songs return '\n'.join(msgs)
def remove(self, song): if song in self.models: url = get_url(song) with open(self.fpath, 'r+', encoding='utf-8') as f: lines = [] for line in f: if line.startswith(url): continue lines.append(line) f.seek(0) f.write(''.join(lines)) f.truncate() # 确保最后写入一个换行符,让文件更加美观 if not lines[-1].endswith('\n'): f.write('\n') self.models.remove(song) return True
def data(self, index, role): offset = index.row() if not index.isValid() or offset >= len(self.albums): return None album = self.albums[offset] if role == Qt.DecorationRole: uri = get_url(album) pixmap = self.pixmaps.get(uri) if pixmap is not None: return pixmap color_str = self.colors[offset] color = QColor(color_str) color.setAlphaF(0.8) return color elif role == Qt.DisplayRole: return album.name elif role == Qt.UserRole: return album return None
def _to_uri(self, model): return get_url(model)
def show_songs(songs): uri_length = max((len(get_url(song)) for song in songs)) if songs else None return '\n'.join( [show_song(song, uri_length=uri_length, brief=True) for song in songs])
def remove(self, song_uri): # FIXME: a little bit tricky for song in self.playlist.list(): if get_url(song) == song_uri: self.playlist.remove(song) break