Skip to content

Commit

Permalink
refactor/ocp_modernize (#5)
Browse files Browse the repository at this point in the history
* refactor/ocp_modernize

return the new OCP MediaEntry/Playlist objects instead of needing to know a json structure

* fix/allow_ocp_objects

MediaEntry/Playlist/PluginStream objects should be allowed in ocp decorators

* utils 0.0.38 compat
  • Loading branch information
JarbasAl authored Jun 21, 2024
1 parent 9e3c75e commit 64453a2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 81 deletions.
159 changes: 80 additions & 79 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from os.path import join, dirname
from typing import Iterable

from ovos_utils import classproperty
from ovos_workshop.backwards_compat import MediaType, PlaybackType, Playlist, PluginStream
from ovos_utils.parse import fuzzy_match
from ovos_utils.process_utils import RuntimeRequirements
from ovos_utils.ocp import MediaType, PlaybackType
from ovos_workshop.decorators import ocp_search, ocp_featured_media
from ovos_workshop.decorators import ocp_search
from ovos_workshop.skills.common_play import OVOSCommonPlaybackSkill
from py_bandcamp import BandCamp

Expand All @@ -29,15 +30,13 @@ def runtime_requirements(self):

# common play
@ocp_search()
def search_bandcamp_artist(self, phrase,
media_type=MediaType.GENERIC):
def search_bandcamp_artist(self, phrase, media_type=MediaType.GENERIC) -> Iterable[Playlist]:
base_score = 0
if self.voc_match(phrase, "bandcamp"):
base_score = 30
if media_type == MediaType.MUSIC:
base_score += 15
phrase = self.remove_voc(phrase, "bandcamp")

try:
# artist top tracks
for match in BandCamp.search_artists(phrase):
Expand All @@ -47,41 +46,36 @@ def search_bandcamp_artist(self, phrase,

# all albums
for idx, album in enumerate(match.albums):
pl = [{
"match_confidence": min(100, score) - idx2,
"media_type": MediaType.MUSIC,
"uri": "bandcamp//" + track.url,
"playback": PlaybackType.AUDIO,
"image": track.image or album.image or match.image,
"bg_image": album.image or match.image,
"skill_icon": self.skill_icon,
"duration": track.duration * 1000,
"title": track.title,
"artist": artist_name,
"skill_id": self.skill_id,
"album": album.title
} for idx2, track in enumerate(album.tracks)]

pl = Playlist(match_confidence=score - idx,
media_type=MediaType.MUSIC,
playback=PlaybackType.AUDIO,
image=album.image or match.image,
skill_icon=self.skill_icon,
skill_id=self.skill_id,
title=album.title + f" ({artist_name}|Full Album)",
artist=artist_name
)
for idx2, track in enumerate(album.tracks):
pl.append(PluginStream(
match_confidence=min(100, score) - idx2,
extractor_id="bandcamp",
stream=track.url,
title=track.title,
image=track.image or album.image or match.image,
artist=artist_name,
length=track.duration * 1000,
skill_id=self.skill_id,
skill_icon=self.skill_icon,
media_type=MediaType.MUSIC,
playback=PlaybackType.AUDIO
))
if pl:
yield {
"match_confidence": score - idx,
"media_type": MediaType.AUDIO,
"playback": PlaybackType.AUDIO,
"playlist": pl, # return full playlist result
"image": album.image or match.image,
"bg_image": album.image or match.image,
"skill_icon": self.skill_icon,
"album": album.title,
"duration": sum(t["duration"] for t in pl),
"title": album.title + f" ({artist_name}|Full Album)",
"skill_id": self.skill_id
}
yield pl
except Exception as e:
pass

# @ocp_search()
def search_bandcamp_tracks(self, phrase,
media_type=MediaType.GENERIC):
def search_bandcamp_tracks(self, phrase, media_type=MediaType.GENERIC) -> Iterable[PluginStream]:
base_score = 0
if self.voc_match(phrase, "bandcamp"):
base_score = 15
Expand All @@ -94,26 +88,25 @@ def search_bandcamp_tracks(self, phrase,
artist_name = match.artist.name
track_score = fuzzy_match(match.title, phrase) * 100
score = base_score + track_score
yield {
"match_confidence": min(100, score),
"media_type": MediaType.MUSIC,
"uri": "bandcamp//" + match.url,
"playback": PlaybackType.AUDIO,
"image": match.image,
"bg_image": match.image,
"duration": match.duration * 1000,
"skill_icon": self.skill_icon,
"title": match.title + f" ({artist_name})",
"artist": artist_name,
"skill_id": self.skill_id
}
yield PluginStream(
match_confidence=min(100, score),
extractor_id="bandcamp",
stream=match.url,
title=match.title + f" ({artist_name})",
image=match.image,
artist=artist_name,
length=match.duration * 1000,
skill_id=self.skill_id,
skill_icon=self.skill_icon,
media_type=MediaType.MUSIC,
playback=PlaybackType.AUDIO
)
except:
pass

# @ocp_search() # deactivated due to many bad matches, users rarely ask
# for album name anyways... maybe add dedicated intent for albums??
def search_bandcamp_album(self, phrase,
media_type=MediaType.GENERIC):
def search_bandcamp_album(self, phrase, media_type=MediaType.GENERIC) -> Iterable[Playlist]:
base_score = 0
if self.voc_match(phrase, "bandcamp"):
base_score = 10
Expand All @@ -128,37 +121,45 @@ def search_bandcamp_album(self, phrase,
artist_score = fuzzy_match(artist_name, phrase) * 100
score = artist_score * 0.3 + album_score * 0.7

pl = [{
"match_confidence": min(100, score) - idx,
"media_type": MediaType.MUSIC,
"uri": "bandcamp//" + track.url,
"playback": PlaybackType.AUDIO,
"image": track.image or album.image,
"bg_image": album.image,
"skill_icon": self.skill_icon,
"title": track.title,
"duration": track.duration * 1000,
"skill_id": self.skill_id,
"album": album.title
} for idx, track in enumerate(album.tracks)]

pl = Playlist(
title=album.title + f" (Full Album)",
artist=artist_name,
image=album.image,
match_confidence=score,
skill_id=self.skill_id,
skill_icon=self.skill_icon,
media_type=MediaType.MUSIC,
playback=PlaybackType.AUDIO
)
for idx, track in enumerate(album.tracks):
pl.append(PluginStream(
match_confidence=min(100, score) - idx,
extractor_id="bandcamp",
stream=track.url,
title=track.title,
image=track.image or album.image,
artist=artist_name,
length=track.duration * 1000,
skill_id=self.skill_id,
skill_icon=self.skill_icon,
media_type=MediaType.MUSIC,
playback=PlaybackType.AUDIO
))
if pl:
yield {
"match_confidence": score,
"media_type": MediaType.AUDIO,
"playback": PlaybackType.AUDIO,
"playlist": pl, # return full playlist result
"image": album.image,
"bg_image": album.image,
"skill_icon": self.skill_icon,
"album": album.title,
"duration": sum(t["duration"] for t in pl),
"title": album.title + f" (Full Album)",
"skill_id": self.skill_id
}
yield pl
except:
pass


def create_skill():
return BandCampSkill()

if __name__ == "__main__":
from ovos_utils.messagebus import FakeBus
from ovos_utils.log import LOG

LOG.set_level("DEBUG")

s = BandCampSkill(bus=FakeBus(), skill_id="t.fake")
for r in s.search_bandcamp_artist("planet of the dead", MediaType.MUSIC):
print(r)
# Playlist(title='Pilgrims (Planet of the Dead|Full Album)', artist='Planet of the Dead', position=0, image='https://f4.bcbits.com/img/a0090508043_10.jpg', match_confidence=103.88888888888889, skill_id='t.fake', skill_icon='/home/miro/PycharmProjects/OCPSkills/skill-ovos-bandcamp/res/logo.png', playback=<PlaybackType.AUDIO: 2>, media_type=<MediaType.MUSIC: 2>)
# Playlist(title='Fear of a Dead Planet (Planet of the Dead|Full Album)', artist='Planet of the Dead', position=0, image='https://f4.bcbits.com/img/a3408335541_10.jpg', match_confidence=102.88888888888889, skill_id='t.fake', skill_icon='/home/miro/PycharmProjects/OCPSkills/skill-ovos-bandcamp/res/logo.png', playback=<PlaybackType.AUDIO: 2>, media_type=<MediaType.MUSIC: 2>)
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
py_bandcamp>=0.7.0
ovos-utils~=0.0, >=0.1.0a17
ovos_workshop~=0.0, >=0.0.11
ovos-utils >= 0.0.38
ovos-workshop>=0.0.16a39
ovos_plugin_common_play

0 comments on commit 64453a2

Please sign in to comment.