|
| 1 | +package org.schabi.newpipe.extractor.services.media_ccc.extractors; |
| 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.Downloader; |
| 8 | +import org.schabi.newpipe.extractor.MediaFormat; |
| 9 | +import org.schabi.newpipe.extractor.StreamingService; |
| 10 | +import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
| 11 | +import org.schabi.newpipe.extractor.exceptions.ParsingException; |
| 12 | +import org.schabi.newpipe.extractor.linkhandler.LinkHandler; |
| 13 | +import org.schabi.newpipe.extractor.stream.*; |
| 14 | +import org.schabi.newpipe.extractor.utils.Localization; |
| 15 | +import org.schabi.newpipe.extractor.utils.Parser; |
| 16 | + |
| 17 | +import javax.annotation.Nonnull; |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +public class MediaCCCStreamExtractor extends StreamExtractor { |
| 23 | + |
| 24 | + private JsonObject data; |
| 25 | + private JsonObject conferenceData; |
| 26 | + |
| 27 | + public MediaCCCStreamExtractor(StreamingService service, LinkHandler linkHandler, Localization localization) { |
| 28 | + super(service, linkHandler, localization); |
| 29 | + } |
| 30 | + |
| 31 | + @Nonnull |
| 32 | + @Override |
| 33 | + public String getUploadDate() throws ParsingException { |
| 34 | + return data.getString("release_date"); |
| 35 | + } |
| 36 | + |
| 37 | + @Nonnull |
| 38 | + @Override |
| 39 | + public String getThumbnailUrl() throws ParsingException { |
| 40 | + return data.getString("thumb_url"); |
| 41 | + } |
| 42 | + |
| 43 | + @Nonnull |
| 44 | + @Override |
| 45 | + public String getDescription() throws ParsingException { |
| 46 | + return data.getString("description"); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public int getAgeLimit() throws ParsingException { |
| 51 | + return 0; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public long getLength() throws ParsingException { |
| 56 | + return data.getInt("length"); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public long getTimeStamp() throws ParsingException { |
| 61 | + return 0; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public long getViewCount() throws ParsingException { |
| 66 | + return data.getInt("view_count"); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public long getLikeCount() throws ParsingException { |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public long getDislikeCount() throws ParsingException { |
| 76 | + return -1; |
| 77 | + } |
| 78 | + |
| 79 | + @Nonnull |
| 80 | + @Override |
| 81 | + public String getUploaderUrl() throws ParsingException { |
| 82 | + return data.getString("conference_url"); |
| 83 | + } |
| 84 | + |
| 85 | + @Nonnull |
| 86 | + @Override |
| 87 | + public String getUploaderName() throws ParsingException { |
| 88 | + return data.getString("conference_url") |
| 89 | + .replace("https://api.media.ccc.de/public/conferences/", ""); |
| 90 | + } |
| 91 | + |
| 92 | + @Nonnull |
| 93 | + @Override |
| 94 | + public String getUploaderAvatarUrl() throws ParsingException { |
| 95 | + return conferenceData.getString("logo_url"); |
| 96 | + } |
| 97 | + |
| 98 | + @Nonnull |
| 99 | + @Override |
| 100 | + public String getDashMpdUrl() throws ParsingException { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + @Nonnull |
| 105 | + @Override |
| 106 | + public String getHlsUrl() throws ParsingException { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public List<AudioStream> getAudioStreams() throws IOException, ExtractionException { |
| 112 | + final JsonArray recordings = data.getArray("recordings"); |
| 113 | + final List<AudioStream> audioStreams = new ArrayList<>(); |
| 114 | + for(int i = 0; i < recordings.size(); i++) { |
| 115 | + final JsonObject recording = recordings.getObject(i); |
| 116 | + final String mimeType = recording.getString("mime_type"); |
| 117 | + if(mimeType.startsWith("audio")) { |
| 118 | + //first we need to resolve the actual video data from CDN |
| 119 | + final MediaFormat mediaFormat; |
| 120 | + if(mimeType.endsWith("opus")) { |
| 121 | + mediaFormat = MediaFormat.OPUS; |
| 122 | + } else if(mimeType.endsWith("mpeg")) { |
| 123 | + mediaFormat = MediaFormat.MP3; |
| 124 | + } else { |
| 125 | + throw new ExtractionException("Unknown media format: " + mimeType); |
| 126 | + } |
| 127 | + |
| 128 | + audioStreams.add(new AudioStream(recording.getString("recording_url"), mediaFormat, -1)); |
| 129 | + } |
| 130 | + } |
| 131 | + return audioStreams; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public List<VideoStream> getVideoStreams() throws IOException, ExtractionException { |
| 136 | + final JsonArray recordings = data.getArray("recordings"); |
| 137 | + final List<VideoStream> videoStreams = new ArrayList<>(); |
| 138 | + for(int i = 0; i < recordings.size(); i++) { |
| 139 | + final JsonObject recording = recordings.getObject(i); |
| 140 | + final String mimeType = recording.getString("mime_type"); |
| 141 | + if(mimeType.startsWith("video")) { |
| 142 | + //first we need to resolve the actual video data from CDN |
| 143 | + |
| 144 | + final MediaFormat mediaFormat; |
| 145 | + if(mimeType.endsWith("webm")) { |
| 146 | + mediaFormat = MediaFormat.WEBM; |
| 147 | + } else if(mimeType.endsWith("mp4")) { |
| 148 | + mediaFormat = MediaFormat.MPEG_4; |
| 149 | + } else { |
| 150 | + throw new ExtractionException("Unknown media format: " + mimeType); |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + videoStreams.add(new VideoStream(recording.getString("recording_url"), |
| 155 | + mediaFormat, |
| 156 | + Integer.toString(recording.getInt("height")))); |
| 157 | + } |
| 158 | + } |
| 159 | + return videoStreams; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionException { |
| 164 | + return null; |
| 165 | + } |
| 166 | + |
| 167 | + @Nonnull |
| 168 | + @Override |
| 169 | + public List<SubtitlesStream> getSubtitlesDefault() throws IOException, ExtractionException { |
| 170 | + return null; |
| 171 | + } |
| 172 | + |
| 173 | + @Nonnull |
| 174 | + @Override |
| 175 | + public List<SubtitlesStream> getSubtitles(MediaFormat format) throws IOException, ExtractionException { |
| 176 | + return null; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public StreamType getStreamType() throws ParsingException { |
| 181 | + return StreamType.VIDEO_STREAM; |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public StreamInfoItem getNextStream() throws IOException, ExtractionException { |
| 186 | + return null; |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public StreamInfoItemsCollector getRelatedStreams() throws IOException, ExtractionException { |
| 191 | + return null; |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public String getErrorMessage() { |
| 196 | + return null; |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException { |
| 201 | + try { |
| 202 | + data = JsonParser.object().from( |
| 203 | + downloader.download(getLinkHandler().getUrl())); |
| 204 | + conferenceData = JsonParser.object() |
| 205 | + .from(downloader.download(getUploaderUrl())); |
| 206 | + } catch (JsonParserException jpe) { |
| 207 | + throw new ExtractionException("Could not parse json returned by url: " + getLinkHandler().getUrl(), jpe); |
| 208 | + } |
| 209 | + |
| 210 | + } |
| 211 | + |
| 212 | + @Nonnull |
| 213 | + @Override |
| 214 | + public String getName() throws ParsingException { |
| 215 | + return data.getString("title"); |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public String getOriginalUrl() throws ParsingException { |
| 220 | + return data.getString("frontend_link"); |
| 221 | + } |
| 222 | +} |
0 commit comments