|
1 | 1 | package org.schabi.newpipe.extractor.services.soundcloud.extractors; |
2 | 2 |
|
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 | | - |
6 | 3 | import com.grack.nanojson.JsonArray; |
7 | 4 | import com.grack.nanojson.JsonObject; |
8 | 5 | import com.grack.nanojson.JsonParser; |
9 | 6 | import com.grack.nanojson.JsonParserException; |
10 | | - |
11 | 7 | import org.schabi.newpipe.extractor.NewPipe; |
12 | 8 | import org.schabi.newpipe.extractor.Page; |
13 | 9 | import org.schabi.newpipe.extractor.StreamingService; |
|
20 | 16 | import org.schabi.newpipe.extractor.stream.StreamInfoItem; |
21 | 17 | import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; |
22 | 18 |
|
| 19 | +import javax.annotation.Nonnull; |
23 | 20 | import java.io.IOException; |
24 | 21 | import java.util.ArrayList; |
| 22 | +import java.util.HashMap; |
25 | 23 | import java.util.List; |
| 24 | +import java.util.Objects; |
26 | 25 |
|
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; |
28 | 28 |
|
29 | 29 | public class SoundcloudPlaylistExtractor extends PlaylistExtractor { |
30 | 30 | private static final int STREAMS_PER_REQUESTED_PAGE = 15; |
@@ -171,9 +171,26 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException |
171 | 171 |
|
172 | 172 | try { |
173 | 173 | 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<>(); |
174 | 177 | for (final Object track : tracks) { |
175 | 178 | 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); |
177 | 194 | } |
178 | 195 | } |
179 | 196 | } catch (final JsonParserException e) { |
|
0 commit comments