Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Uploader's Avatar on YouTube and PeerTube #723

Merged
merged 5 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public String getUploaderUrl() {
return "";
}

@Nullable
@Override
public String getUploaderAvatarUrl() throws ParsingException {
return null;
}

@Override
public boolean isUploaderVerified() throws ParsingException {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper;

import javax.annotation.Nullable;

public class BandcampDiscographStreamInfoItemExtractor extends BandcampStreamInfoItemExtractor {

private final JsonObject discograph;
Expand All @@ -18,6 +20,12 @@ public String getUploaderName() {
return discograph.getString("band_name");
}

@Nullable
@Override
public String getUploaderAvatarUrl() throws ParsingException {
return null;
}

@Override
public String getName() {
return discograph.getString("title");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.stream.StreamExtractor;

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


Expand Down Expand Up @@ -53,6 +54,12 @@ public String getUploaderName() {
return "";
}

@Nullable
@Override
public String getUploaderAvatarUrl() throws ParsingException {
return null;
}

/**
* Each track can have its own cover art. Therefore, unless a substitute is provided,
* the thumbnail is extracted using a stream extractor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.jsoup.nodes.Element;
import org.schabi.newpipe.extractor.exceptions.ParsingException;

import javax.annotation.Nullable;

public class BandcampSearchStreamInfoItemExtractor extends BandcampStreamInfoItemExtractor {

private final Element resultInfo, searchResult;
Expand All @@ -24,6 +26,12 @@ public String getUploaderName() {
}
}

@Nullable
@Override
public String getUploaderAvatarUrl() throws ParsingException {
return null;
}

@Override
public String getName() throws ParsingException {
return resultInfo.getElementsByClass("heading").text();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public String getUploaderUrl() throws ParsingException {
return "https://media.ccc.de/c/" + conferenceInfo.getString("slug");
}

@Nullable
@Override
public String getUploaderAvatarUrl() throws ParsingException {
return null;
}

@Override
public boolean isUploaderVerified() throws ParsingException {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public String getUploaderUrl() throws ParsingException {
.getUrl(); // web URL
}

@Nullable
@Override
public String getUploaderAvatarUrl() throws ParsingException {
return null;
}

@Override
public boolean isUploaderVerified() throws ParsingException {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public String getUploaderUrl() {
return event.getString("conference_url");
}

@Nullable
@Override
public String getUploaderAvatarUrl() throws ParsingException {
return null;
}

@Override
public boolean isUploaderVerified() throws ParsingException {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.schabi.newpipe.extractor.stream.StreamType;
import org.schabi.newpipe.extractor.utils.JsonUtils;

import javax.annotation.Nullable;

public class PeertubeStreamInfoItemExtractor implements StreamInfoItemExtractor {

protected final JsonObject item;
Expand Down Expand Up @@ -54,6 +56,12 @@ public String getUploaderUrl() throws ParsingException {
.fromId("accounts/" + name + "@" + host, baseUrl).getUrl();
}

@Nullable
@Override
public String getUploaderAvatarUrl() throws ParsingException {
return null;
FireMasterK marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public boolean isUploaderVerified() throws ParsingException {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor;
import org.schabi.newpipe.extractor.stream.StreamType;

import javax.annotation.Nullable;

import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps;

Expand Down Expand Up @@ -43,6 +45,12 @@ public String getUploaderUrl() {
return replaceHttpWithHttps(itemObject.getObject("user").getString("permalink_url"));
}

@Nullable
@Override
public String getUploaderAvatarUrl() throws ParsingException {
return null;
}

@Override
public boolean isUploaderVerified() throws ParsingException {
return itemObject.getObject("user").getBoolean("verified");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public String getUploaderUrl() {
return entryElement.select("author > uri").first().text();
}

@Nullable
@Override
public String getUploaderAvatarUrl() throws ParsingException {
return null;
}

@Override
public boolean isUploaderVerified() throws ParsingException {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory;
import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor;
import org.schabi.newpipe.extractor.stream.StreamType;
import org.schabi.newpipe.extractor.utils.JsonUtils;
import org.schabi.newpipe.extractor.utils.Utils;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -40,7 +41,7 @@
*/

public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
private JsonObject videoInfo;
private final JsonObject videoInfo;
private final TimeAgoParser timeAgoParser;
private StreamType cachedStreamType;

Expand Down Expand Up @@ -162,6 +163,18 @@ public String getUploaderUrl() throws ParsingException {
return url;
}

@Nullable
@Override
public String getUploaderAvatarUrl() throws ParsingException {

if(videoInfo.has("channelThumbnailSupportedRenderers")) {
return JsonUtils.getArray(videoInfo, "channelThumbnailSupportedRenderers.channelThumbnailWithLinkRenderer.thumbnail.thumbnails")
.getObject(0).getString("url");
}

return null;
}

@Override
public boolean isUploaderVerified() throws ParsingException {
return YoutubeParsingHelper.isVerified(videoInfo.getArray("ownerBadges"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class StreamInfoItem extends InfoItem {
private long duration = -1;

private String uploaderUrl = null;
private String uploaderAvatarUrl = null;
private boolean uploaderVerified = false;

public StreamInfoItem(int serviceId, String url, String name, StreamType streamType) {
Expand Down Expand Up @@ -82,6 +83,15 @@ public void setUploaderUrl(String uploaderUrl) {
this.uploaderUrl = uploaderUrl;
}

@Nullable
public String getUploaderAvatarUrl() {
return uploaderAvatarUrl;
}

public void setUploaderAvatarUrl(final String uploaderAvatarUrl) {
this.uploaderAvatarUrl = uploaderAvatarUrl;
}

@Nullable
public String getTextualUploadDate() {
return textualUploadDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ public interface StreamInfoItemExtractor extends InfoItemExtractor {

String getUploaderUrl() throws ParsingException;

/**
* Get the uploader's avatar
*
* @return The uploader's avatar url or {@code null} if not provided by the service.
* @throws ParsingException if there is an error in the extraction
*/
@Nullable
String getUploaderAvatarUrl() throws ParsingException;

/**
* Whether the uploader has been verified by the service's provider.
* If there is no verification implemented, return <code>false</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public StreamInfoItem extract(StreamInfoItemExtractor extractor) throws ParsingE
} catch (Exception e) {
addError(e);
}
try {
resultItem.setUploaderAvatarUrl(extractor.getUploaderAvatarUrl());
} catch (Exception e) {
addError(e);
}
try {
resultItem.setUploaderVerified(extractor.isUploaderVerified());
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package org.schabi.newpipe.extractor.services.youtube.search;

import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.schabi.newpipe.downloader.DownloaderFactory;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.MetaInfo;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.*;
import org.schabi.newpipe.extractor.channel.ChannelInfoItem;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.search.SearchExtractor;
import org.schabi.newpipe.extractor.services.DefaultSearchExtractorTest;
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
import org.schabi.newpipe.extractor.stream.Description;

import org.schabi.newpipe.extractor.channel.ChannelInfoItem;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;

import javax.annotation.Nullable;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -29,14 +23,11 @@

import static java.util.Collections.singletonList;
import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertEmptyErrors;
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
import static org.schabi.newpipe.extractor.services.DefaultTests.assertNoDuplicatedItems;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.CHANNELS;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.PLAYLISTS;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.VIDEOS;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.*;

public class YoutubeSearchExtractorTest {

Expand Down Expand Up @@ -320,4 +311,36 @@ public void testAtLeastOneVerified() throws IOException, ExtractionException {
assertTrue(verified);
}
}

public static class VideoUploaderAvatar extends DefaultSearchExtractorTest {
private static SearchExtractor extractor;
private static final String QUERY = "sidemen";

@BeforeClass
public static void setUp() throws Exception {
YoutubeParsingHelper.resetClientVersionAndKey();
YoutubeParsingHelper.setNumberGenerator(new Random(1));
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "video_uploader_avatar"));
extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), "");
extractor.fetchPage();
}

@Override public SearchExtractor extractor() { return extractor; }
@Override public StreamingService expectedService() { return YouTube; }
@Override public String expectedName() { return QUERY; }
@Override public String expectedId() { return QUERY; }
@Override public String expectedUrlContains() { return "youtube.com/results?search_query=" + QUERY; }
@Override public String expectedOriginalUrlContains() { return "youtube.com/results?search_query=" + QUERY; }
@Override public String expectedSearchString() { return QUERY; }
@Nullable @Override public String expectedSearchSuggestion() { return null; }
@Override public InfoItem.InfoType expectedInfoItemType() { return InfoItem.InfoType.STREAM; }

@Test
public void testUploaderAvatar() throws IOException, ExtractionException {
final List<InfoItem> items = extractor.getInitialPage().getItems();
for (final InfoItem item : items) {
assertNotNull(((StreamInfoItem) item).getUploaderAvatarUrl());
FireMasterK marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Loading