Merge pull request #37 from ultrasonic/add-get-playlist

Add getPlaylist call to subsonic api.
This commit is contained in:
Yahor Berdnikau 2017-08-27 22:40:55 +02:00 committed by GitHub
commit 961fccde4e
9 changed files with 224 additions and 118 deletions

View File

@ -0,0 +1,57 @@
package org.moire.ultrasonic.api.subsonic
import org.amshove.kluent.`should equal to`
import org.amshove.kluent.`should equal`
import org.junit.Test
import org.moire.ultrasonic.api.subsonic.models.MusicDirectoryChild
/**
* Integration test for [SubsonicAPIClient] for getPlaylist call.
*/
class SubsonicApiGetPlaylistTest : SubsonicAPIClientTest() {
@Test
fun `Should parse error response`() {
checkErrorCallParsed(mockWebServerRule) {
client.api.getPlaylist(10).execute()
}
}
@Test
fun `Should parse ok response`() {
mockWebServerRule.enqueueResponse("get_playlist_ok.json")
val response = client.api.getPlaylist(4).execute()
assertResponseSuccessful(response)
with(response.body().playlist) {
id `should equal to` 0
name `should equal to` "Aug 27, 2017 11:17 AM"
owner `should equal to` "admin"
public `should equal to` false
songCount `should equal to` 16
duration `should equal to` 3573
created `should equal` parseDate("2017-08-27T11:17:26.216Z")
changed `should equal` parseDate("2017-08-27T11:17:26.218Z")
coverArt `should equal to` "pl-0"
entriesList.size `should equal to` 2
entriesList[1] `should equal` MusicDirectoryChild(id = 4215, parent = 4186,
isDir = false, title = "Going to Hell", album = "Going to Hell",
artist = "The Pretty Reckless", track = 2, year = 2014,
genre = "Hard Rock", coverArt = "4186", size = 11089627,
contentType = "audio/mpeg", suffix = "mp3", duration = 277, bitRate = 320,
path = "The Pretty Reckless/Going to Hell/02 Going to Hell.mp3",
isVideo = false, playCount = 0, discNumber = 1,
created = parseDate("2016-10-23T21:30:41.000Z"),
albumId = 388, artistId = 238, type = "music")
}
}
@Test
fun `Should pass id as request param`() {
val playlistId = 453L
mockWebServerRule.assertRequestParam(responseResourceName = "get_playlist_ok.json",
apiRequest = {
client.api.getPlaylist(playlistId).execute()
}, expectedParam = "id=$playlistId")
}
}

View File

@ -0,0 +1,66 @@
{
"subsonic-response" : {
"status" : "ok",
"version" : "1.15.0",
"playlist" : {
"id" : "0",
"name" : "Aug 27, 2017 11:17 AM",
"owner" : "admin",
"public" : false,
"songCount" : 16,
"duration" : 3573,
"created" : "2017-08-27T11:17:26.216Z",
"changed" : "2017-08-27T11:17:26.218Z",
"coverArt" : "pl-0",
"entry" : [ {
"id" : "4209",
"parent" : "4186",
"isDir" : false,
"title" : "Follow Me Down",
"album" : "Going to Hell",
"artist" : "The Pretty Reckless",
"track" : 1,
"year" : 2014,
"genre" : "Hard Rock",
"coverArt" : "4186",
"size" : 11229681,
"contentType" : "audio/mpeg",
"suffix" : "mp3",
"duration" : 280,
"bitRate" : 320,
"path" : "The Pretty Reckless/Going to Hell/01 Follow Me Down.mp3",
"isVideo" : false,
"playCount" : 1,
"discNumber" : 1,
"created" : "2016-10-23T21:30:43.000Z",
"albumId" : "388",
"artistId" : "238",
"type" : "music"
}, {
"id" : "4215",
"parent" : "4186",
"isDir" : false,
"title" : "Going to Hell",
"album" : "Going to Hell",
"artist" : "The Pretty Reckless",
"track" : 2,
"year" : 2014,
"genre" : "Hard Rock",
"coverArt" : "4186",
"size" : 11089627,
"contentType" : "audio/mpeg",
"suffix" : "mp3",
"duration" : 277,
"bitRate" : 320,
"path" : "The Pretty Reckless/Going to Hell/02 Going to Hell.mp3",
"isVideo" : false,
"playCount" : 0,
"discNumber" : 1,
"created" : "2016-10-23T21:30:41.000Z",
"albumId" : "388",
"artistId" : "238",
"type" : "music"
} ]
}
}
}

View File

@ -5,11 +5,12 @@ import org.moire.ultrasonic.api.subsonic.response.GetArtistResponse
import org.moire.ultrasonic.api.subsonic.response.GetArtistsResponse
import org.moire.ultrasonic.api.subsonic.response.GetIndexesResponse
import org.moire.ultrasonic.api.subsonic.response.GetMusicDirectoryResponse
import org.moire.ultrasonic.api.subsonic.response.GetPlaylistResponse
import org.moire.ultrasonic.api.subsonic.response.LicenseResponse
import org.moire.ultrasonic.api.subsonic.response.MusicFoldersResponse
import org.moire.ultrasonic.api.subsonic.response.SearchTwoResponse
import org.moire.ultrasonic.api.subsonic.response.SearchResponse
import org.moire.ultrasonic.api.subsonic.response.SearchThreeResponse
import org.moire.ultrasonic.api.subsonic.response.SearchTwoResponse
import org.moire.ultrasonic.api.subsonic.response.SubsonicResponse
import retrofit2.Call
import retrofit2.http.GET
@ -83,4 +84,7 @@ interface SubsonicAPIDefinition {
@Query("albumOffset") albumOffset: Int? = null,
@Query("songCount") songCount: Int? = null,
@Query("musicFolderId") musicFolderId: Long? = null): Call<SearchThreeResponse>
@GET("getPlaylist.view")
fun getPlaylist(@Query("id") id: Long): Call<GetPlaylistResponse>
}

View File

@ -0,0 +1,17 @@
package org.moire.ultrasonic.api.subsonic.models
import com.fasterxml.jackson.annotation.JsonProperty
import java.util.Calendar
data class Playlist(
val id: Long = -1,
val name: String = "",
val owner: String = "",
val public: Boolean = false,
val songCount: Int = 0,
val duration: Long = 0,
val created: Calendar? = null,
val changed: Calendar? = null,
val coverArt: String = "",
@JsonProperty("entry") val entriesList: List<MusicDirectoryChild> = emptyList()
)

View File

@ -0,0 +1,11 @@
package org.moire.ultrasonic.api.subsonic.response
import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions
import org.moire.ultrasonic.api.subsonic.SubsonicError
import org.moire.ultrasonic.api.subsonic.models.Playlist
class GetPlaylistResponse(
status: Status,
version: SubsonicAPIVersions,
error: SubsonicError?,
val playlist: Playlist = Playlist()) : SubsonicResponse(status, version, error)

View File

@ -61,6 +61,7 @@ import org.moire.ultrasonic.api.subsonic.response.GetArtistResponse;
import org.moire.ultrasonic.api.subsonic.response.GetArtistsResponse;
import org.moire.ultrasonic.api.subsonic.response.GetIndexesResponse;
import org.moire.ultrasonic.api.subsonic.response.GetMusicDirectoryResponse;
import org.moire.ultrasonic.api.subsonic.response.GetPlaylistResponse;
import org.moire.ultrasonic.api.subsonic.response.LicenseResponse;
import org.moire.ultrasonic.api.subsonic.response.MusicFoldersResponse;
import org.moire.ultrasonic.api.subsonic.response.SearchResponse;
@ -91,7 +92,6 @@ import org.moire.ultrasonic.service.parser.GenreParser;
import org.moire.ultrasonic.service.parser.JukeboxStatusParser;
import org.moire.ultrasonic.service.parser.LyricsParser;
import org.moire.ultrasonic.service.parser.MusicDirectoryParser;
import org.moire.ultrasonic.service.parser.PlaylistParser;
import org.moire.ultrasonic.service.parser.PlaylistsParser;
import org.moire.ultrasonic.service.parser.PodcastEpisodeParser;
import org.moire.ultrasonic.service.parser.PodcastsChannelsParser;
@ -473,54 +473,53 @@ public class RESTMusicService implements MusicService
return APIConverter.toDomainEntity(response.body().getSearchResult());
}
@Override
public MusicDirectory getPlaylist(String id, String name, Context context, ProgressListener progressListener) throws Exception
{
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(params, SOCKET_READ_TIMEOUT_GET_PLAYLIST);
@Override
public MusicDirectory getPlaylist(String id,
String name,
Context context,
ProgressListener progressListener) throws Exception {
if (id == null) {
throw new IllegalArgumentException("id param is null!");
}
Reader reader = getReader(context, progressListener, "getPlaylist", params, "id", id);
try
{
MusicDirectory playlist = new PlaylistParser(context).parse(reader, progressListener);
updateProgressListener(progressListener, R.string.parser_reading);
Response<GetPlaylistResponse> response = subsonicAPIClient.getApi()
.getPlaylist(Long.valueOf(id)).execute();
checkResponseSuccessful(response);
File playlistFile = FileUtil.getPlaylistFile(Util.getServerName(context), name);
FileWriter fw = new FileWriter(playlistFile);
BufferedWriter bw = new BufferedWriter(fw);
try
{
fw.write("#EXTM3U\n");
for (MusicDirectory.Entry e : playlist.getChildren())
{
String filePath = FileUtil.getSongFile(context, e).getAbsolutePath();
if (!new File(filePath).exists())
{
String ext = FileUtil.getExtension(filePath);
String base = FileUtil.getBaseName(filePath);
filePath = base + ".complete." + ext;
}
fw.write(filePath + '\n');
}
}
catch (Exception e)
{
Log.w(TAG, "Failed to save playlist: " + name);
}
finally
{
bw.close();
fw.close();
}
MusicDirectory playlist = APIConverter
.toMusicDirectoryDomainEntity(response.body().getPlaylist());
savePlaylist(name, context, playlist);
return playlist;
}
return playlist;
}
finally
{
Util.close(reader);
}
}
private void savePlaylist(String name,
Context context,
MusicDirectory playlist) throws IOException {
File playlistFile = FileUtil.getPlaylistFile(Util.getServerName(context), name);
FileWriter fw = new FileWriter(playlistFile);
BufferedWriter bw = new BufferedWriter(fw);
try {
fw.write("#EXTM3U\n");
for (MusicDirectory.Entry e : playlist.getChildren()) {
String filePath = FileUtil.getSongFile(context, e).getAbsolutePath();
if (!new File(filePath).exists()) {
String ext = FileUtil.getExtension(filePath);
String base = FileUtil.getBaseName(filePath);
filePath = base + ".complete." + ext;
}
fw.write(filePath + '\n');
}
} catch (IOException e) {
Log.w(TAG, "Failed to save playlist: " + name);
throw e;
} finally {
bw.close();
fw.close();
}
}
@Override
@Override
public List<PodcastsChannel> getPodcastsChannels(boolean refresh, Context context, ProgressListener progressListener) throws Exception
{
Reader reader = getReader(context, progressListener, "getPodcasts", null,"includeEpisodes", "false");

View File

@ -1,72 +0,0 @@
/*
This file is part of Subsonic.
Subsonic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Subsonic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2009 (C) Sindre Mehus
*/
package org.moire.ultrasonic.service.parser;
import android.content.Context;
import org.moire.ultrasonic.R;
import org.moire.ultrasonic.domain.MusicDirectory;
import org.moire.ultrasonic.util.ProgressListener;
import org.xmlpull.v1.XmlPullParser;
import java.io.Reader;
/**
* @author Sindre Mehus
*/
public class PlaylistParser extends MusicDirectoryEntryParser
{
public PlaylistParser(Context context)
{
super(context);
}
public MusicDirectory parse(Reader reader, ProgressListener progressListener) throws Exception
{
updateProgress(progressListener, R.string.parser_reading);
init(reader);
MusicDirectory dir = new MusicDirectory();
int eventType;
do
{
eventType = nextParseEvent();
if (eventType == XmlPullParser.START_TAG)
{
String name = getElementName();
if ("entry".equals(name))
{
dir.addChild(parseEntry("", false, 0));
}
else if ("error".equals(name))
{
handleError();
}
}
} while (eventType != XmlPullParser.END_DOCUMENT);
validate();
updateProgress(progressListener, R.string.parser_reading_done);
return dir;
}
}

View File

@ -16,6 +16,7 @@ import org.moire.ultrasonic.api.subsonic.models.Artist as APIArtist
import org.moire.ultrasonic.api.subsonic.models.Indexes as APIIndexes
import org.moire.ultrasonic.api.subsonic.models.MusicDirectory as APIMusicDirectory
import org.moire.ultrasonic.api.subsonic.models.MusicFolder as APIMusicFolder
import org.moire.ultrasonic.api.subsonic.models.Playlist as APIPlaylist
import org.moire.ultrasonic.api.subsonic.models.SearchResult as APISearchResult
fun APIMusicFolder.toDomainEntity(): MusicFolder = MusicFolder(this.id.toString(), this.name)
@ -103,3 +104,8 @@ fun SearchThreeResult.toDomainEntity(): SearchResult = SearchResult(
this.artistList.map { it.toDomainEntity() },
this.albumList.map { it.toDomainEntity() },
this.songList.map { it.toDomainEntity() })
fun APIPlaylist.toMusicDirectoryDomainEntity(): MusicDirectory = MusicDirectory().apply {
name = this@toMusicDirectoryDomainEntity.name
addAll(this@toMusicDirectoryDomainEntity.entriesList.map { it.toDomainEntity() })
}

View File

@ -13,6 +13,7 @@ import org.moire.ultrasonic.api.subsonic.models.Indexes
import org.moire.ultrasonic.api.subsonic.models.MusicDirectory
import org.moire.ultrasonic.api.subsonic.models.MusicDirectoryChild
import org.moire.ultrasonic.api.subsonic.models.MusicFolder
import org.moire.ultrasonic.api.subsonic.models.Playlist
import org.moire.ultrasonic.api.subsonic.models.SearchResult
import org.moire.ultrasonic.api.subsonic.models.SearchThreeResult
import org.moire.ultrasonic.api.subsonic.models.SearchTwoResult
@ -257,6 +258,23 @@ class APIConverterTest {
}
}
@Test
fun `Should convert Playlist to MusicDirectory domain entity`() {
val entity = Playlist(name = "some-playlist-name", entriesList = listOf(
MusicDirectoryChild(id = 10L, parent = 1393),
MusicDirectoryChild(id = 11L, parent = 1393)
))
val convertedEntity = entity.toMusicDirectoryDomainEntity()
with(convertedEntity) {
name `should equal to` entity.name
children.size `should equal to` entity.entriesList.size
children[0] `should equal` entity.entriesList[0].toDomainEntity()
children[1] `should equal` entity.entriesList[1].toDomainEntity()
}
}
private fun createMusicFolder(id: Long = 0, name: String = ""): MusicFolder =
MusicFolder(id, name)