Skip to content

Commit

Permalink
library: add API library.song_get_web_url (#870)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven authored Aug 26, 2024
1 parent 98f0215 commit 4c743a6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions feeluown/excs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down
23 changes: 12 additions & 11 deletions feeluown/gui/pages/song_explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -278,22 +277,24 @@ 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
# if alt key is pressed.
#
# 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)
Expand Down
15 changes: 15 additions & 0 deletions feeluown/library/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down

0 comments on commit 4c743a6

Please sign in to comment.