|
| 1 | +package org.schabi.newpipe.extractor.services.soundcloud; |
| 2 | + |
| 3 | +import com.grack.nanojson.JsonArray; |
| 4 | +import com.grack.nanojson.JsonObject; |
| 5 | +import com.grack.nanojson.JsonParser; |
| 6 | +import com.grack.nanojson.JsonParserException; |
| 7 | +import org.schabi.newpipe.extractor.*; |
| 8 | +import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
| 9 | +import org.schabi.newpipe.extractor.exceptions.ParsingException; |
| 10 | +import org.schabi.newpipe.extractor.search.InfoItemsSearchCollector; |
| 11 | +import org.schabi.newpipe.extractor.search.SearchEngine; |
| 12 | +import org.schabi.newpipe.extractor.search.SearchExtractor; |
| 13 | +import org.schabi.newpipe.extractor.search.SearchQueryUrlHandler; |
| 14 | +import org.schabi.newpipe.extractor.utils.Parser; |
| 15 | + |
| 16 | +import javax.annotation.Nonnull; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.UnsupportedEncodingException; |
| 19 | +import java.net.MalformedURLException; |
| 20 | +import java.net.URL; |
| 21 | + |
| 22 | +import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudSearchQueryUrlHandler.ITEMS_PER_PAGE; |
| 23 | + |
| 24 | +public class SoundcloudSearchExtractor extends SearchExtractor { |
| 25 | + |
| 26 | + private JsonArray searchCollection; |
| 27 | + |
| 28 | + public SoundcloudSearchExtractor(StreamingService service, |
| 29 | + SearchQueryUrlHandler urlIdHandler, |
| 30 | + String contentCountry) { |
| 31 | + super(service, urlIdHandler, contentCountry); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public String getSearchSuggestion() throws ParsingException { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + @Nonnull |
| 40 | + @Override |
| 41 | + public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException { |
| 42 | + return new InfoItemsPage<>(collectItems(searchCollection), getNextPageUrl()); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String getNextPageUrl() throws IOException, ExtractionException { |
| 47 | + return getNextPageUrlFromCurrentUrl(getUrl()); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public InfoItemsPage<InfoItem> getPage(String pageUrl) throws IOException, ExtractionException { |
| 52 | + final Downloader dl = getDownloader(); |
| 53 | + try { |
| 54 | + searchCollection = JsonParser.object().from(dl.download(pageUrl)).getArray("collection"); |
| 55 | + } catch (JsonParserException e) { |
| 56 | + throw new ParsingException("Could not parse json response", e); |
| 57 | + } |
| 58 | + |
| 59 | + return new InfoItemsPage<>(collectItems(searchCollection), getNextPageUrlFromCurrentUrl(pageUrl)); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException { |
| 64 | + final Downloader dl = getDownloader(); |
| 65 | + final String url = getUrl(); |
| 66 | + try { |
| 67 | + searchCollection = JsonParser.object().from(dl.download(url)).getArray("collection"); |
| 68 | + } catch (JsonParserException e) { |
| 69 | + throw new ParsingException("Could not parse json response", e); |
| 70 | + } |
| 71 | + |
| 72 | + if (searchCollection.size() == 0) { |
| 73 | + throw new SearchEngine.NothingFoundException("Nothing found"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private InfoItemsCollector<InfoItem, InfoItemExtractor> collectItems(JsonArray searchCollection) { |
| 78 | + final InfoItemsSearchCollector collector = getInfoItemSearchCollector(); |
| 79 | + |
| 80 | + for (Object result : searchCollection) { |
| 81 | + if (!(result instanceof JsonObject)) continue; |
| 82 | + //noinspection ConstantConditions |
| 83 | + JsonObject searchResult = (JsonObject) result; |
| 84 | + String kind = searchResult.getString("kind", ""); |
| 85 | + switch (kind) { |
| 86 | + case "user": |
| 87 | + collector.commit(new SoundcloudChannelInfoItemExtractor(searchResult)); |
| 88 | + break; |
| 89 | + case "track": |
| 90 | + collector.commit(new SoundcloudStreamInfoItemExtractor(searchResult)); |
| 91 | + break; |
| 92 | + case "playlist": |
| 93 | + collector.commit(new SoundcloudPlaylistInfoItemExtractor(searchResult)); |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return collector; |
| 99 | + } |
| 100 | + |
| 101 | + private String getNextPageUrlFromCurrentUrl(String currentUrl) |
| 102 | + throws MalformedURLException, UnsupportedEncodingException { |
| 103 | + final int pageOffset = Integer.parseInt( |
| 104 | + Parser.compatParseMap( |
| 105 | + new URL(currentUrl) |
| 106 | + .getQuery()) |
| 107 | + .get("offset")); |
| 108 | + |
| 109 | + return currentUrl.replace("&offset=" + |
| 110 | + Integer.toString(pageOffset), |
| 111 | + "&offset=" + Integer.toString(pageOffset + ITEMS_PER_PAGE)); |
| 112 | + } |
| 113 | +} |
0 commit comments