Use new subsonic api getPlaylist() call.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
Yahor Berdnikau 2017-08-27 22:36:37 +02:00
parent b8b53dc81d
commit de0b57f9b8
2 changed files with 44 additions and 117 deletions

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

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;
}
}