Skip to content

Commit bf70d32

Browse files
committed
Fix SoundcloudPlaylistExtractor: tracks are in correct order
1 parent 5c710da commit bf70d32

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

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

Lines changed: 22 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,25 @@ 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+
final HashMap<Integer, JsonObject> idToTrack = new HashMap<>();
174176
for (final Object track : tracks) {
175177
if (track instanceof JsonObject) {
176-
collector.commit(new SoundcloudStreamInfoItemExtractor((JsonObject) track));
178+
final JsonObject o = (JsonObject) track;
179+
idToTrack.put(o.getInt("id"), o);
180+
}
181+
}
182+
for (final String strId : currentIds) {
183+
final int id = Integer.parseInt(strId);
184+
try {
185+
collector.commit(new SoundcloudStreamInfoItemExtractor(
186+
Objects.requireNonNull(
187+
idToTrack.get(id),
188+
"no track with id " + id + " in response"
189+
)
190+
));
191+
} catch (final NullPointerException e) {
192+
throw new ParsingException("Could not parse json response", e);
177193
}
178194
}
179195
} catch (final JsonParserException e) {

0 commit comments

Comments
 (0)