Skip to content

Commit a822e91

Browse files
authored
Merge pull request #939 from TurtleArmyMc/fix/SoundcloudPlaylistExtractor_track_order
Fix SoundcloudPlaylistExtractor: tracks are in correct order
2 parents 2d50369 + 02810a7 commit a822e91

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package org.schabi.newpipe.extractor.services.soundcloud.extractors;
22

3-
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
4-
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
5-
63
import com.grack.nanojson.JsonArray;
74
import com.grack.nanojson.JsonObject;
85
import com.grack.nanojson.JsonParser;
96
import com.grack.nanojson.JsonParserException;
10-
117
import org.schabi.newpipe.extractor.NewPipe;
128
import org.schabi.newpipe.extractor.Page;
139
import org.schabi.newpipe.extractor.StreamingService;
@@ -20,11 +16,15 @@
2016
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
2117
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
2218

19+
import javax.annotation.Nonnull;
2320
import java.io.IOException;
2421
import java.util.ArrayList;
22+
import java.util.HashMap;
2523
import java.util.List;
24+
import java.util.Objects;
2625

27-
import javax.annotation.Nonnull;
26+
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
27+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
2828

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

172172
try {
173173
final JsonArray tracks = JsonParser.array().from(response);
174+
// Response may not contain tracks in the same order as currentIds.
175+
// The streams are displayed in the order which is used in currentIds on SoundCloud.
176+
final HashMap<Integer, JsonObject> idToTrack = new HashMap<>();
174177
for (final Object track : tracks) {
175178
if (track instanceof JsonObject) {
176-
collector.commit(new SoundcloudStreamInfoItemExtractor((JsonObject) track));
179+
final JsonObject o = (JsonObject) track;
180+
idToTrack.put(o.getInt("id"), o);
181+
}
182+
}
183+
for (final String strId : currentIds) {
184+
final int id = Integer.parseInt(strId);
185+
try {
186+
collector.commit(new SoundcloudStreamInfoItemExtractor(
187+
Objects.requireNonNull(
188+
idToTrack.get(id),
189+
"no track with id " + id + " in response"
190+
)
191+
));
192+
} catch (final NullPointerException e) {
193+
throw new ParsingException("Could not parse json response", e);
177194
}
178195
}
179196
} catch (final JsonParserException e) {

0 commit comments

Comments
 (0)