133 lines
4.7 KiB
Java
133 lines
4.7 KiB
Java
|
/*
|
||
|
* To change this license header, choose License Headers in Project Properties.
|
||
|
* To change this template file, choose Tools | Templates
|
||
|
* and open the template in the editor.
|
||
|
*/
|
||
|
package org.schabi.newpipe.extractor.services.xh.extractors;
|
||
|
|
||
|
import com.grack.nanojson.JsonArray;
|
||
|
import com.grack.nanojson.JsonObject;
|
||
|
import com.grack.nanojson.JsonParser;
|
||
|
import com.grack.nanojson.JsonParserException;
|
||
|
import edu.umd.cs.findbugs.annotations.NonNull;
|
||
|
import java.io.IOException;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Collections;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.List;
|
||
|
import java.util.logging.Level;
|
||
|
import java.util.logging.Logger;
|
||
|
import org.jsoup.Jsoup;
|
||
|
import org.jsoup.nodes.Document;
|
||
|
import org.schabi.newpipe.extractor.InfoItem;
|
||
|
import org.schabi.newpipe.extractor.MetaInfo;
|
||
|
import org.schabi.newpipe.extractor.MultiInfoItemsCollector;
|
||
|
import org.schabi.newpipe.extractor.Page;
|
||
|
import org.schabi.newpipe.extractor.StreamingService;
|
||
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
||
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
||
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
||
|
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler;
|
||
|
import org.schabi.newpipe.extractor.search.SearchExtractor;
|
||
|
import static org.schabi.newpipe.extractor.services.xh.XhService.HOST;
|
||
|
import org.schabi.newpipe.extractor.services.xh.extractors.items.XhVideo;
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @author Rakesh
|
||
|
*/
|
||
|
public class XhSearchExtractor extends SearchExtractor {
|
||
|
|
||
|
public XhSearchExtractor(StreamingService service, SearchQueryHandler linkHandler) {
|
||
|
super(service, linkHandler);
|
||
|
}
|
||
|
|
||
|
@NonNull
|
||
|
@Override
|
||
|
public String getSearchSuggestion() throws ParsingException {
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean isCorrectedSearch() throws ParsingException {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public List<MetaInfo> getMetaInfo() throws ParsingException {
|
||
|
return Collections.emptyList();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
|
||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public InfoItemsPage<InfoItem> getPage(Page page) throws IOException, ExtractionException {
|
||
|
// Collector
|
||
|
final MultiInfoItemsCollector collector = new MultiInfoItemsCollector(getServiceId());
|
||
|
|
||
|
// String
|
||
|
String url = page.getUrl();
|
||
|
|
||
|
// HTML
|
||
|
Document content = Jsoup.connect(url).get();
|
||
|
|
||
|
// JS
|
||
|
final String js = content.getElementById("initials-script").html();
|
||
|
|
||
|
// Parse
|
||
|
String info_cdn = js.substring(0, js.length() - 1).replace("window.initials=", "");
|
||
|
|
||
|
try {
|
||
|
// parse
|
||
|
final JsonObject info = JsonParser.object().from(info_cdn);
|
||
|
|
||
|
// list
|
||
|
final JsonArray list = info.getObject("SearchResult").getArray("models");
|
||
|
|
||
|
for (int i = 0; i < list.size(); i++) {
|
||
|
|
||
|
final JsonObject v = list.getObject(i);
|
||
|
collector.commit(new XhSearchStreamInfoExtractor(
|
||
|
new XhVideo(
|
||
|
v.getInt("id"),
|
||
|
v.getString("title"),
|
||
|
"",
|
||
|
v.getInt("duration"),
|
||
|
v.getInt("created"),
|
||
|
v.getString("pageURL"),
|
||
|
v.getString("thumbURL"),
|
||
|
0,
|
||
|
"",
|
||
|
"",
|
||
|
"",
|
||
|
new HashMap<String, String>()
|
||
|
)
|
||
|
));
|
||
|
}
|
||
|
|
||
|
int currentPage = Integer.parseInt(page.getUrl().substring(page.getUrl().length() - 2));
|
||
|
assert list.size() < 46;
|
||
|
|
||
|
String nextUrl = null;
|
||
|
if (currentPage < list.size()) {
|
||
|
nextUrl = page.getUrl().substring(0, page.getUrl().length() -2) + String.valueOf(currentPage + 1);
|
||
|
}
|
||
|
|
||
|
return new InfoItemsPage<>(collector, new Page(nextUrl));
|
||
|
} catch (JsonParserException ex) {
|
||
|
Logger.getLogger(XhSearchExtractor.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
}
|
||
|
return null;
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onFetchPage(Downloader downloader) throws IOException, ExtractionException {
|
||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||
|
}
|
||
|
|
||
|
}
|