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

Fix SoundcloudPlaylistExtractor: tracks are in correct order #939

Merged
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package org.schabi.newpipe.extractor.services.soundcloud.extractors;

import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;

import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.StreamingService;
Expand All @@ -20,11 +16,15 @@
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import javax.annotation.Nonnull;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
private static final int STREAMS_PER_REQUESTED_PAGE = 15;
Expand Down Expand Up @@ -171,9 +171,26 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException

try {
final JsonArray tracks = JsonParser.array().from(response);
// Response may not contain tracks in the same order as currentIds.
// The streams are displayed in the order which is used in currentIds on SoundCloud.
final HashMap<Integer, JsonObject> idToTrack = new HashMap<>();
for (final Object track : tracks) {
if (track instanceof JsonObject) {
collector.commit(new SoundcloudStreamInfoItemExtractor((JsonObject) track));
final JsonObject o = (JsonObject) track;
idToTrack.put(o.getInt("id"), o);
}
}
for (final String strId : currentIds) {
final int id = Integer.parseInt(strId);
try {
collector.commit(new SoundcloudStreamInfoItemExtractor(
Objects.requireNonNull(
idToTrack.get(id),
"no track with id " + id + " in response"
)
));
} catch (final NullPointerException e) {
throw new ParsingException("Could not parse json response", e);
}
}
} catch (final JsonParserException e) {
Expand Down