Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PlayList functionality is added (and some minor bugs that caused instance to stop playing) #36

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion asksonic/intents/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from asksonic.utils.subsonic import subsonic
from . import queue


@ask.intent('AMAZON.HelpIntent')
@ask.launch
def launch() -> question:
Expand Down Expand Up @@ -40,6 +39,18 @@ def play_artist(artist: str) -> Union[audio, statement]:
return statement(render_template('artist_not_found', artist=artist))


@ask.intent('AskSonicPlayPlayListIntent')
def play_playlist(playlist: str) -> Union[audio, statement]:
log(f'Play Playlist: {playlist}')
tracks = subsonic.get_playlist(playlist, tracks_count)
if tracks:
track = queue.reset(tracks)
return play_track_response(
track,
render_template('playing_artist', artist=track.artist)
)
return statement(render_template('playlist_not_found', playlist=playlist))

@ask.intent('AskSonicPlayAlbumIntent')
def play_album(album: str, artist: Optional[str]) -> Union[audio, statement]:
log(f'Play Album: {artist} - {album}')
Expand Down
18 changes: 14 additions & 4 deletions asksonic/intents/playback.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
@ask.on_playback_started()
def playback_started() -> tuple:
log('Playback Started')
if queue.current:
queue.current.scrobble(submission=False, timestamp=queue.start_time)
# this will exception out if LAST FM isn't setup on sonic api
try:
if queue.current:
queue.current.scrobble(submission=False, timestamp=queue.start_time)
except:
pass

return empty_response


Expand Down Expand Up @@ -40,8 +45,13 @@ def playback_nearly_finished() -> Union[audio, tuple]:
@ask.on_playback_finished()
def playback_finished() -> tuple:
log('Playback Finished')
if queue.current:
queue.current.scrobble(submission=True, timestamp=queue.start_time)
# This will exception out if last.fm is not setup
try:
if queue.current:
queue.current.scrobble(submission=True, timestamp=queue.start_time)
except:
pass

queue.next()
return empty_response

Expand Down
2 changes: 1 addition & 1 deletion asksonic/templates/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pausing: "Pausing"
resuming: "Resuming"
stopping: "Stopping"
okay: "Okay"

playlist_not_found: "Play list not found {{ playlist }}"
playing_library: "Playing music from your library"
playing_artist: "Playing music by {{ artist }}"
playing_album: "Playing the album {{ album }} {{ 'by {}'.format(artist) if artist }}"
Expand Down
39 changes: 31 additions & 8 deletions asksonic/utils/subsonic/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from urllib.request import Request
from random import shuffle
from .track import Track
from asksonic import logger


class Subsonic(Connection):
Expand Down Expand Up @@ -96,16 +97,38 @@ def artist_tracks(self, artist: str, count: int = 50) -> list[Track]:
return []
shuffle(albums)
tracks = []
# Fix bug when it can't find album to play
for album in albums:
songs = self.getAlbum(album['id'])
songs = songs['album']['song']
shuffle(songs)
tracks.extend([Track(**track) for track in songs])
if len(tracks) >= count:
tracks = tracks[:count]
break
try:
songs = self.getAlbum(album['id'])
songs = songs['album']['song']
shuffle(songs)
tracks.extend([Track(**track) for track in songs])
if len(tracks) >= count:
tracks = tracks[:count]
break
except:
continue
shuffle(tracks)
return tracks

def get_playlist(self, playlist: str, count: int = 50) -> list[Track]:
playlists = self.getPlaylists()
logger.debug(str(playlists))
id = -1
tracks = []
if not playlists:
return []
for id in playlists['playlists']['playlist']:
if playlist.lower() == id['name'].lower():
id = id['id']
break
playListEntry = self.getPlaylist(id)
for track in playListEntry['playlist']['entry']:
tracks.append(Track(**track))

return tracks


def album_tracks(self, album: str, artist: Optional[str]) -> list[Track]:
if artist:
Expand All @@ -117,4 +140,4 @@ def album_tracks(self, album: str, artist: Optional[str]) -> list[Track]:
tracks = self.getAlbum(found_album['id'])
tracks = tracks['album']['song']
tracks = [Track(**track) for track in tracks]
return tracks
return tracks
54 changes: 53 additions & 1 deletion interaction_models/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,24 @@
"play the album {album}",
"play the {artist} album {album}"
]
},
{
"name": "AskSonicPlayPlayListIntent",
"slots": [
{
"name": "playlist",
"type": "AMAZON.MusicAlbum",
"samples": [
"{playlist}"
]
}
],
"samples": [
"play the mix {playlist}",
"open playlist {playlist}",
"play playlist {playlist}",
"Play the playlist {playlist}"
]
}
],
"types": [
Expand Down Expand Up @@ -214,6 +232,22 @@
"prompts": {}
}
]
},
{
"name": "AskSonicPlayPlayListIntent",
"confirmationRequired": false,
"prompts": {},
"slots": [
{
"name": "playlist",
"type": "AMAZON.MusicAlbum",
"confirmationRequired": false,
"elicitationRequired": true,
"prompts": {
"elicitation": "Elicit.Slot.1552392366824.395353797063"
}
}
]
}
],
"delegationStrategy": "ALWAYS"
Expand All @@ -236,7 +270,25 @@
"value": "What album should I play?"
}
]
},
{
"id": "Confirm.Intent.589542519485",
"variations": [
{
"type": "PlainText",
"value": "What playlist do you want to play?"
}
]
},
{
"id": "Elicit.Slot.1552392366824.395353797063",
"variations": [
{
"type": "PlainText",
"value": "Which playlist do you want to play?"
}
]
}
]
}
}
}
Loading