get channel banner

This commit is contained in:
Christian Schabesberger
2016-08-01 01:56:19 +02:00
parent 9a0f61e60b
commit 6beb36f92f
25 changed files with 420 additions and 57 deletions
@@ -43,4 +43,8 @@ public abstract class ChannelExtractor {
public abstract String getChannelName() throws ParsingException;
public abstract String getAvatarUrl() throws ParsingException;
public abstract String getBannerUrl() throws ParsingException;
public abstract String getFeedUrl() throws ParsingException;
public int getServiceId() {
return serviceId;
}
}
@@ -0,0 +1,69 @@
package org.schabi.newpipe.extractor;
import android.util.Log;
import java.util.List;
import java.util.Vector;
/**
* Created by Christian Schabesberger on 31.07.16.
*
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
* ChannelInfo.java is part of NewPipe.
*
* NewPipe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPipe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/
public class ChannelInfo {
public void addException(Exception e) {
errors.add(e);
}
public static ChannelInfo getInfo(ChannelExtractor extractor, Downloader dl)
throws ParsingException {
ChannelInfo info = new ChannelInfo();
// importand data
info.service_id = extractor.getServiceId();
info.channel_name = extractor.getChannelName();
try {
info.avatar_url = extractor.getAvatarUrl();
} catch (Exception e) {
info.errors.add(e);
}
try {
info.banner_url = extractor.getBannerUrl();
} catch (Exception e) {
info.errors.add(e);
}
try {
info.feed_url = extractor.getFeedUrl();
} catch(Exception e) {
info.errors.add(e);
}
return info;
}
public int service_id = -1;
public String channel_name = "";
public String avatar_url = "";
public String banner_url = "";
public String feed_url = "";
public List<Throwable> errors = new Vector<>();
}
@@ -43,5 +43,5 @@ public class SearchResult {
public String suggestion = "";
public List<StreamPreviewInfo> resultList = new Vector<>();
public List<Exception> errors = new Vector<>();
public List<Throwable> errors = new Vector<>();
}
@@ -81,6 +81,7 @@ public abstract class StreamExtractor {
public abstract String getTitle() throws ParsingException;
public abstract String getDescription() throws ParsingException;
public abstract String getUploader() throws ParsingException;
public abstract String getChannelUrl() throws ParsingException;
public abstract int getLength() throws ParsingException;
public abstract long getViewCount() throws ParsingException;
public abstract String getUploadDate() throws ParsingException;
@@ -188,6 +188,11 @@ public class StreamInfo extends AbstractVideoInfo {
} catch(Exception e) {
streamInfo.addException(e);
}
try {
streamInfo.channel_url = extractor.getChannelUrl();
} catch(Exception e) {
streamInfo.addException(e);
}
try {
streamInfo.description = extractor.getDescription();
} catch(Exception e) {
@@ -258,6 +263,7 @@ public class StreamInfo extends AbstractVideoInfo {
}
public String uploader_thumbnail_url = "";
public String channel_url = "";
public String description = "";
public List<VideoStream> video_streams = null;
@@ -279,5 +285,5 @@ public class StreamInfo extends AbstractVideoInfo {
//in seconds. some metadata is not passed using a StreamInfo object!
public int start_position = 0;
public List<Exception> errors = new Vector<>();
public List<Throwable> errors = new Vector<>();
}
@@ -27,7 +27,7 @@ import java.util.Vector;
public class StreamPreviewInfoCollector {
private List<StreamPreviewInfo> itemList = new Vector<>();
private List<Exception> errors = new Vector<>();
private List<Throwable> errors = new Vector<>();
private UrlIdHandler urlIdHandler;
private int serviceId = -1;
@@ -40,7 +40,7 @@ public class StreamPreviewInfoCollector {
return itemList;
}
public List<Exception> getErrors() {
public List<Throwable> getErrors() {
return errors;
}
@@ -2,14 +2,30 @@ package org.schabi.newpipe.extractor.services.youtube;
import android.util.Log;
/*
import com.steadystate.css.dom.CSSStyleDeclarationImpl;
import com.steadystate.css.dom.CSSStyleSheetImpl;
import com.steadystate.css.parser.CSSOMParser;
import com.steadystate.css.parser.SACParserCSS3;
import org.w3c.css.sac.CSSParseException;
import org.w3c.css.sac.InputSource;
import org.w3c.dom.css.CSSRule;
import org.w3c.dom.css.CSSRuleList;
import org.w3c.dom.css.CSSStyleSheet;
import java.io.StringReader;
*/
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.schabi.newpipe.extractor.ChannelExtractor;
import org.schabi.newpipe.extractor.Downloader;
import org.schabi.newpipe.extractor.ExtractionException;
import org.schabi.newpipe.extractor.Parser;
import org.schabi.newpipe.extractor.ParsingException;
import org.schabi.newpipe.extractor.UrlIdHandler;
import java.io.IOException;
/**
@@ -36,6 +52,8 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
private static final String TAG = YoutubeChannelExtractor.class.toString();
// private CSSOMParser cssParser = new CSSOMParser(new SACParserCSS3());
private Downloader downloader;
private final Document doc;
private final String siteUrl;
@@ -45,17 +63,21 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
throws ExtractionException, IOException {
super(urlIdHandler, url, dl, serviceId);
siteUrl = url;
siteUrl = urlIdHandler.cleanUrl(url);
Log.d(TAG, siteUrl);
downloader = dl;
String pageContent = downloader.download(url);
doc = Jsoup.parse(pageContent, url);
Log.d(TAG, pageContent);
}
@Override
public String getChannelName() throws ParsingException {
return getUrlIdHandler().getId(siteUrl);
try {
return doc.select("span[class=\"qualified-channel-title-text\"]").first()
.select("a").first().text();
} catch(Exception e) {
throw new ParsingException("Could not get channel name");
}
}
@Override
@@ -70,6 +92,41 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
@Override
public String getBannerUrl() throws ParsingException {
return "https://yt3.ggpht.com/-oF0YbeAGkaA/VBgrKvEGY1I/AAAAAAAACdw/nx02iZSseFw/w2120-fcrop64=1,00005a57ffffa5a8-nd-c0xffffffff-rj-k-no/Channel-Art-Template-%2528Photoshop%2529.png";
String cssContent = "";
try {
Element el = doc.select("div[id=\"gh-banner\"]").first().select("style").first();
cssContent = el.html();
// todo: parse this using a css parser
/*
CSSStyleSheet sheet = cssParser.parseStyleSheet(
new org.w3c.css.sac.InputSource(
new StringReader(cssContent)), null, null);
CSSRuleList rules = sheet.getCssRules();
for (int i = 0; i < rules.getLength(); i++) {
final CSSRule rule = rules.item(i);
System.out.println(rule.getCssText());
}
*/
String url = "https:" + Parser.matchGroup1("url\\((.*)\\)", cssContent);
if(url.contains("s.ytimg.com")) {
return null;
} else {
return url;
}
/* } catch(CSSParseException csse) {
throw new ParsingException("Could not parse css: " + cssContent); */
} catch(Exception e) {
throw new ParsingException("Could not get Banner", e);
}
}
@Override
public String getFeedUrl() throws ParsingException {
return siteUrl + "/feed";
}
private String getUserUrl() throws ParsingException {
return doc.select("span[class=\"qualified-channel-title-text\"]").first()
.select("a").first().attr("abs:href");
}
}
@@ -27,15 +27,11 @@ import org.schabi.newpipe.extractor.UrlIdHandler;
public class YoutubeChannelUrlIdHandler implements UrlIdHandler {
public String getUrl(String channelId) {
return "https://www.youtube.com/user/" + channelId + "/videos";
return "https://www.youtube.com/" + channelId;
}
public String getId(String siteUrl) throws ParsingException {
try {
return Parser.matchGroup1("/user/(.*)", siteUrl);
} catch(Exception e) {
throw new ParsingException("Could not get channel/user id", e);
}
return Parser.matchGroup1("/(user/[A-Za-z0-9_-]*|channel/[A-Za-z0-9_-]*)", siteUrl);
}
public String cleanUrl(String siteUrl) throws ParsingException {
@@ -45,6 +41,7 @@ public class YoutubeChannelUrlIdHandler implements UrlIdHandler {
public boolean acceptUrl(String videoUrl) {
return (videoUrl.contains("youtube") ||
videoUrl.contains("youtu.be")) &&
videoUrl.contains("/user/");
( videoUrl.contains("/user/") ||
videoUrl.contains("/channel/"));
}
}
@@ -685,6 +685,16 @@ public class YoutubeStreamExtractor extends StreamExtractor {
return pageUrl;
}
@Override
public String getChannelUrl() throws ParsingException {
try {
return doc.select("div[class=\"yt-user-info\"]").first().children()
.select("a").first().attr("abs:href");
} catch(Exception e) {
throw new ParsingException("Could not get channel link", e);
}
}
@Override
public StreamInfo.StreamType getStreamType() throws ParsingException {
//todo: if implementing livestream support this value should be generated dynamically