Skip to content

Commit

Permalink
Implement @TobiGr's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
wb9688 committed Apr 20, 2020
1 parent 979c5a7 commit b51699a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,9 @@ private JsonObject getVideoTab() throws ParsingException {
if (getTextFromObject(videoTab.getObject("content").getObject("sectionListRenderer")
.getArray("contents").getObject(0).getObject("itemSectionRenderer")
.getArray("contents").getObject(0).getObject("messageRenderer")
.getObject("text")).equals("This channel has no videos."))
.getObject("text")).equals("This channel has no videos.")) {
return null;
}

this.videoTab = videoTab;
return videoTab;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import com.grack.nanojson.JsonWriter;

import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader;
Expand All @@ -20,18 +19,17 @@
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
import org.schabi.newpipe.extractor.utils.Utils;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Nonnull;

import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getValidJsonResponseBody;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getValidJsonResponseBody;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ALBUMS;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ARTISTS;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_PLAYLISTS;
Expand Down Expand Up @@ -251,7 +249,7 @@ public String getUrl() throws ParsingException {
public String getName() throws ParsingException {
final String name = getTextFromObject(info.getArray("flexColumns").getObject(0)
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
if (name != null && !name.isEmpty()) {
if (!name.isEmpty()) {
return name;
}
throw new ParsingException("Could not get name");
Expand All @@ -261,7 +259,7 @@ public String getName() throws ParsingException {
public long getDuration() throws ParsingException {
final String duration = getTextFromObject(info.getArray("flexColumns").getObject(3)
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
if (duration != null && !duration.isEmpty()) {
if (!duration.isEmpty()) {
return YoutubeParsingHelper.parseDurationString(duration);
}
throw new ParsingException("Could not get duration");
Expand All @@ -271,7 +269,7 @@ public long getDuration() throws ParsingException {
public String getUploaderName() throws ParsingException {
final String name = getTextFromObject(info.getArray("flexColumns").getObject(1)
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
if (name != null && !name.isEmpty()) {
if (!name.isEmpty()) {
return name;
}
throw new ParsingException("Could not get uploader name");
Expand All @@ -294,7 +292,13 @@ public String getUploaderUrl() throws ParsingException {
.getObject(1).getObject("musicResponsiveListItemFlexColumnRenderer")
.getObject("text").getArray("runs").getObject(0).getObject("navigationEndpoint");

return getUrlFromNavigationEndpoint(navigationEndpoint);
final String url = getUrlFromNavigationEndpoint(navigationEndpoint);

if (url != null && !url.isEmpty()) {
return url;
}

throw new ParsingException("Could not get uploader URL");
}
}

Expand All @@ -315,7 +319,7 @@ public long getViewCount() throws ParsingException {
}
final String viewCount = getTextFromObject(info.getArray("flexColumns").getObject(2)
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
if (viewCount != null && !viewCount.isEmpty()) {
if (!viewCount.isEmpty()) {
return Utils.mixedNumberWordToLong(viewCount);
}
throw new ParsingException("Could not get view count");
Expand Down Expand Up @@ -355,7 +359,7 @@ public String getThumbnailUrl() throws ParsingException {
public String getName() throws ParsingException {
final String name = getTextFromObject(info.getArray("flexColumns").getObject(0)
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
if (name != null && !name.isEmpty()) {
if (!name.isEmpty()) {
return name;
}
throw new ParsingException("Could not get name");
Expand All @@ -374,7 +378,7 @@ public String getUrl() throws ParsingException {
public long getSubscriberCount() throws ParsingException {
final String viewCount = getTextFromObject(info.getArray("flexColumns").getObject(2)
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
if (viewCount != null && !viewCount.isEmpty()) {
if (!viewCount.isEmpty()) {
return Utils.mixedNumberWordToLong(viewCount);
}
throw new ParsingException("Could not get subscriber count");
Expand Down Expand Up @@ -410,7 +414,7 @@ public String getThumbnailUrl() throws ParsingException {
public String getName() throws ParsingException {
final String name = getTextFromObject(info.getArray("flexColumns").getObject(0)
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
if (name != null && !name.isEmpty()) {
if (!name.isEmpty()) {
return name;
}
throw new ParsingException("Could not get name");
Expand All @@ -435,7 +439,7 @@ public String getUploaderName() throws ParsingException {
name = getTextFromObject(info.getArray("flexColumns").getObject(1)
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
}
if (name != null && !name.isEmpty()) {
if (!name.isEmpty()) {
return name;
}
throw new ParsingException("Could not get uploader name");
Expand All @@ -448,7 +452,7 @@ public long getStreamCount() throws ParsingException {
}
final String count = getTextFromObject(info.getArray("flexColumns").getObject(2)
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
if (count != null && !count.isEmpty()) {
if (!count.isEmpty()) {
if (count.contains("100+")) {
return ITEM_COUNT_MORE_THAN_100;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;

import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
Expand All @@ -15,9 +14,8 @@
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.Utils;

import java.io.IOException;

import javax.annotation.Nonnull;
import java.io.IOException;

import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
Expand Down Expand Up @@ -83,7 +81,7 @@ public String getNextPageUrl() {
@Override
public String getName() throws ParsingException {
String name = getTextFromObject(playlistInfo.getObject("title"));
if (name == null || !name.isEmpty()) return name;
if (!name.isEmpty()) return name;

return initialData.getObject("microformat").getObject("microformatDataRenderer").getString("title");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.ScriptableObject;
Expand Down Expand Up @@ -33,10 +32,11 @@
import org.schabi.newpipe.extractor.stream.StreamType;
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
import org.schabi.newpipe.extractor.stream.VideoStream;
import org.schabi.newpipe.extractor.utils.JsonUtils;
import org.schabi.newpipe.extractor.utils.Parser;
import org.schabi.newpipe.extractor.utils.Utils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
Expand All @@ -50,9 +50,6 @@
import java.util.Locale;
import java.util.Map;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
Expand Down Expand Up @@ -120,7 +117,7 @@ public String getName() throws ParsingException {
assertPageFetched();
String title = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("title"));

if (title == null || title.isEmpty()) {
if (title.isEmpty()) {
title = playerResponse.getObject("videoDetails").getString("title");

if (title == null || title.isEmpty()) throw new ParsingException("Could not get name");
Expand Down Expand Up @@ -200,7 +197,7 @@ public Description getDescription() throws ParsingException {
assertPageFetched();
// description with more info on links
String description = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("description"), true);
if (description != null && !description.isEmpty()) return new Description(description, Description.HTML);
if (!description.isEmpty()) return new Description(description, Description.HTML);

// raw non-html description
return new Description(playerResponse.getObject("videoDetails").getString("shortDescription"), Description.PLAIN_TEXT);
Expand Down Expand Up @@ -252,7 +249,7 @@ public long getViewCount() throws ParsingException {
String views = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("viewCount")
.getObject("videoViewCountRenderer").getObject("viewCount"));

if (views == null || views.isEmpty()) {
if (views.isEmpty()) {
views = playerResponse.getObject("videoDetails").getString("viewCount");

if (views == null || views.isEmpty()) throw new ParsingException("Could not get view count");
Expand Down Expand Up @@ -333,7 +330,7 @@ public String getUploaderName() throws ParsingException {
String uploaderName = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("owner")
.getObject("videoOwnerRenderer").getObject("title"));

if (uploaderName == null || uploaderName.isEmpty()) {
if (uploaderName.isEmpty()) {
uploaderName = playerResponse.getObject("videoDetails").getString("author");

if (uploaderName == null || uploaderName.isEmpty()) throw new ParsingException("Could not get uploader name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public String getUrl() throws ParsingException {
@Override
public String getName() throws ParsingException {
String name = getTextFromObject(videoInfo.getObject("title"));
if (name == null || !name.isEmpty()) return name;
if (!name.isEmpty()) return name;
throw new ParsingException("Could not get name");
}

Expand All @@ -105,15 +105,15 @@ public long getDuration() throws ParsingException {

String duration = getTextFromObject(videoInfo.getObject("lengthText"));

if (duration == null || duration.isEmpty()) {
if (duration.isEmpty()) {
for (Object thumbnailOverlay : videoInfo.getArray("thumbnailOverlays")) {
if (((JsonObject) thumbnailOverlay).has("thumbnailOverlayTimeStatusRenderer")) {
duration = getTextFromObject(((JsonObject) thumbnailOverlay)
.getObject("thumbnailOverlayTimeStatusRenderer").getObject("text"));
}
}

if (duration == null || duration.isEmpty()) throw new ParsingException("Could not get duration");
if (duration.isEmpty()) throw new ParsingException("Could not get duration");
}

return YoutubeParsingHelper.parseDurationString(duration);
Expand All @@ -123,13 +123,13 @@ public long getDuration() throws ParsingException {
public String getUploaderName() throws ParsingException {
String name = getTextFromObject(videoInfo.getObject("longBylineText"));

if (name == null || name.isEmpty()) {
if (name.isEmpty()) {
name = getTextFromObject(videoInfo.getObject("ownerText"));

if (name == null || name.isEmpty()) {
if (name.isEmpty()) {
name = getTextFromObject(videoInfo.getObject("shortBylineText"));

if (name == null || name.isEmpty()) throw new ParsingException("Could not get uploader name");
if (name.isEmpty()) throw new ParsingException("Could not get uploader name");
}
}

Expand Down Expand Up @@ -169,7 +169,7 @@ public String getTextualUploadDate() throws ParsingException {
}

final String publishedTimeText = getTextFromObject(videoInfo.getObject("publishedTimeText"));
if (publishedTimeText != null && !publishedTimeText.isEmpty()) return publishedTimeText;
if (!publishedTimeText.isEmpty()) return publishedTimeText;

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;

import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
Expand All @@ -33,9 +32,8 @@
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;

import java.io.IOException;

import javax.annotation.Nonnull;
import java.io.IOException;

import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
Expand Down Expand Up @@ -72,13 +70,8 @@ public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) {
@Nonnull
@Override
public String getName() throws ParsingException {
String name;
try {
name = getTextFromObject(initialData.getObject("header").getObject("feedTabbedHeaderRenderer").getObject("title"));
} catch (Exception e) {
throw new ParsingException("Could not get Trending name", e);
}
if (name != null && !name.isEmpty()) {
String name = getTextFromObject(initialData.getObject("header").getObject("feedTabbedHeaderRenderer").getObject("title"));
if (!name.isEmpty()) {
return name;
}
throw new ParsingException("Could not get Trending name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint)
return null;
}

/**
* Get the text from a JSON object that has either a simpleText or a runs array.
* @param textObject JSON object to get the text from
* @param html whether to return HTML, by parsing the navigationEndpoint
* @return text in the JSON object or an empty string
*/
public static String getTextFromObject(JsonObject textObject, boolean html) throws ParsingException {
if (textObject.has("simpleText")) return textObject.getString("simpleText");

Expand Down

0 comments on commit b51699a

Please sign in to comment.