From 4c743a6ddca84946e0dff1cfa54574419953ccde Mon Sep 17 00:00:00 2001 From: Shaowen Yin Date: Mon, 26 Aug 2024 11:36:32 +0800 Subject: [PATCH] library: add API library.song_get_web_url (#870) --- feeluown/excs.py | 1 + feeluown/gui/pages/song_explore.py | 23 ++++++++++++----------- feeluown/library/library.py | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/feeluown/excs.py b/feeluown/excs.py index 5c9bee881f..778775691d 100644 --- a/feeluown/excs.py +++ b/feeluown/excs.py @@ -69,6 +69,7 @@ class ModelNotFound(ResourceNotFound): """Model is not found For example, a model identifier is invalid. + Maybe ResourceNotFound is enough and this exception should be removed. .. versionadded:: 3.7.7 """ diff --git a/feeluown/gui/pages/song_explore.py b/feeluown/gui/pages/song_explore.py index 190a76c4a7..56a6f5e9b7 100644 --- a/feeluown/gui/pages/song_explore.py +++ b/feeluown/gui/pages/song_explore.py @@ -10,8 +10,7 @@ from feeluown.excs import ResourceNotFound from feeluown.library import ( - SupportsSongHotComments, SupportsSongSimilar, SupportsSongWebUrl, - ModelFlags, + SupportsSongHotComments, SupportsSongSimilar, ModelFlags, ) from feeluown.player import Lyric from feeluown.library import reverse, resolve @@ -278,13 +277,11 @@ def _setup_ui(self): self._right_layout.addWidget(self.lyric_view) async def maybe_show_web_url_btn(self, provider, song): - if isinstance(provider, SupportsSongWebUrl): - async def copy_song_web_url(): - web_url = await aio.run_fn(provider.song_get_web_url, song) - QGuiApplication.clipboard().setText(web_url) - self._app.show_msg(f'已经复制:{web_url}') - - self.copy_web_url_btn.clicked.connect(lambda: aio.run_afn(copy_song_web_url)) + try: + web_url = self._app.library.song_get_web_url(song) + except ResourceNotFound: + self.copy_web_url_btn.hide() + else: # TODO: Open url in browser when alt key is pressed. Use # QDesktopServices.openUrl to open url in browser, and # you may use QGuiApplication::keyboardModifiers to check @@ -292,8 +289,12 @@ async def copy_song_web_url(): # # NOTE(cosven): Since switching from applications is inconvenience, # the default behaviour of button is url-copy instead of url-open. - else: - self.copy_web_url_btn.hide() + + async def copy_song_web_url(): + QGuiApplication.clipboard().setText(web_url) + self._app.show_msg(f'已经复制:{web_url}') + + self.copy_web_url_btn.clicked.connect(lambda: aio.run_afn(copy_song_web_url)) async def show_song_wiki(self, song, album): aio.run_afn(self.song_wiki_label.show_song, song, album) diff --git a/feeluown/library/library.py b/feeluown/library/library.py index e63e59d563..c1ee51ff83 100644 --- a/feeluown/library/library.py +++ b/feeluown/library/library.py @@ -298,6 +298,21 @@ def song_prepare_media(self, song: BriefSongModel, policy) -> Media: raise MediaNotFound('provider returns empty media') return media + def song_get_web_url(self, song: BriefSongModel) -> str: + """Get song web url + + :raises ResourceNotFound: provider/song/web_url is not found + :raises ProviderIOError: provider raises error during get_web_url + + .. versionadded:: 4.1.8 + """ + provider = self.get(song.source) + if provider is None: + raise ResourceNotFound(f'provider({song.source}) not found') + if isinstance(provider, SupportsSongWebUrl): + return provider.song_get_web_url(song) + raise ResourceNotFound(reason=ResourceNotFound.Reason.not_supported) + def song_prepare_mv_media(self, song: BriefSongModel, policy) -> Media: """