164 lines
5.9 KiB
Java
164 lines
5.9 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.JsonObject;
|
|
import com.grack.nanojson.JsonParser;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import org.schabi.newpipe.extractor.MediaFormat;
|
|
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.LinkHandler;
|
|
import org.schabi.newpipe.extractor.stream.AudioStream;
|
|
import org.schabi.newpipe.extractor.stream.StreamExtractor;
|
|
import org.schabi.newpipe.extractor.stream.StreamType;
|
|
import org.schabi.newpipe.extractor.stream.VideoStream;
|
|
|
|
// Parse
|
|
import org.jsoup.Jsoup;
|
|
import org.jsoup.nodes.Document;
|
|
import org.schabi.newpipe.extractor.services.xh.extractors.items.XhVideo;
|
|
|
|
|
|
|
|
public class XhStreamExtractor extends StreamExtractor {
|
|
|
|
// baseurl
|
|
String baseUrl;
|
|
|
|
// Object
|
|
XhVideo video;
|
|
|
|
public XhStreamExtractor(StreamingService service, LinkHandler linkHandler) throws ParsingException {
|
|
super(service, linkHandler);
|
|
|
|
// get baseurl using predefined method getBaseUrl()
|
|
// perform regex to replace the url host
|
|
this.baseUrl = getBaseUrl();
|
|
|
|
// parse video info
|
|
try {
|
|
Document html = Jsoup.connect(this.baseUrl).get();
|
|
|
|
// Parse Content
|
|
String content = html.selectFirst("#initials-script").html();
|
|
String info_cdn = content.substring(0, content.length() - 1).replace("window.initials=", "");
|
|
|
|
// Use NanoJSON to convert to VideoItem
|
|
JsonObject json = JsonParser.object().from(info_cdn);
|
|
final JsonObject videoModel = json.getObject("videoModel");
|
|
|
|
// Video Info
|
|
final int id = videoModel.getInt("id");
|
|
final String title = videoModel.getString("title");
|
|
final String description = videoModel.getString("description");
|
|
final String thumbURL = videoModel.getString("thumbURL");
|
|
final int duration = videoModel.getInt("created");
|
|
final String pageURL = videoModel.getString("pageURL");
|
|
final int created = videoModel.getInt("created");
|
|
// Author
|
|
final String authorName = videoModel.getObject("author").getString("name");
|
|
final int authorID = videoModel.getObject("author").getInt("id");
|
|
final String authorThumb = videoModel.getObject("author").getString("thumbURL");
|
|
final String authorPageURL = videoModel.getObject("author").getString("pageURL");
|
|
|
|
|
|
HashMap<String, String> streams = new HashMap<>();
|
|
|
|
// iterate urls
|
|
final JsonObject mp4 = videoModel.getObject("sources").getObject("mp4");
|
|
if (!mp4.isEmpty()) {
|
|
final Object keys[] = mp4.keySet().toArray();
|
|
for (int i=0; i < keys.length; i++) {
|
|
final String key = keys[i].toString();
|
|
streams.put(key, mp4.getString("key"));
|
|
}
|
|
}
|
|
|
|
// set the video object
|
|
this.video = new XhVideo(id, title, description, duration, created, pageURL, thumbURL, authorID, authorName, authorThumb, authorPageURL, streams);
|
|
} catch (Exception e) {
|
|
throw new ParsingException(e.getMessage(), e);
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public String getThumbnailUrl() throws ParsingException {
|
|
return this.video.getThumbURL();
|
|
}
|
|
|
|
@Override
|
|
public String getUploaderUrl() throws ParsingException {
|
|
return "";
|
|
}
|
|
|
|
@Override
|
|
public String getUploaderName() throws ParsingException {
|
|
return this.video.getAuthorName();
|
|
}
|
|
|
|
@Override
|
|
public List<AudioStream> getAudioStreams() throws IOException, ExtractionException {
|
|
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
|
return Collections.emptyList();
|
|
}
|
|
|
|
@Override
|
|
public List<VideoStream> getVideoStreams() throws IOException, ExtractionException {
|
|
// video was not parsed / no video streams
|
|
if (this.video.getSources().isEmpty()) {
|
|
return Collections.emptyList();
|
|
}
|
|
|
|
// new list
|
|
List<VideoStream> listStreams = new ArrayList<>();
|
|
// iterate hashmap for key values
|
|
for (Map.Entry<String, String> source: this.video.getSources().entrySet()) {
|
|
// add source to list
|
|
listStreams.add(
|
|
new VideoStream.Builder()
|
|
.setId(String.valueOf(this.video.getId()))
|
|
.setContent(source.getValue(), true)
|
|
.setIsVideoOnly(false)
|
|
.setMediaFormat(source.getValue().endsWith(".webm")? MediaFormat.WEBM:MediaFormat.MPEG_4)
|
|
.setResolution(source.getKey())
|
|
.build()
|
|
);
|
|
}
|
|
// return sources
|
|
return listStreams;
|
|
}
|
|
|
|
@Override
|
|
public List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionException {
|
|
return Collections.emptyList();
|
|
}
|
|
|
|
@Override
|
|
public StreamType getStreamType() throws ParsingException {
|
|
return StreamType.VIDEO_STREAM;
|
|
}
|
|
|
|
@Override
|
|
public void onFetchPage(Downloader downloader) throws IOException, ExtractionException {
|
|
|
|
}
|
|
|
|
@Override
|
|
public String getName() throws ParsingException {
|
|
return video.getTitle();
|
|
}
|
|
|
|
}
|