Files
Athou_commafeed/src/main/java/com/commafeed/backend/favicon/YoutubeFaviconFetcher.java

101 lines
3.3 KiB
Java
Raw Normal View History

2014-10-26 12:25:44 +01:00
package com.commafeed.backend.favicon;
2015-06-04 12:28:20 +02:00
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
2014-10-26 12:25:44 +01:00
import javax.inject.Inject;
import javax.inject.Singleton;
2015-06-04 12:28:20 +02:00
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
2014-10-26 12:25:44 +01:00
2015-06-04 12:28:20 +02:00
import com.commafeed.CommaFeedConfiguration;
2014-10-26 12:25:44 +01:00
import com.commafeed.backend.HttpGetter;
import com.commafeed.backend.HttpGetter.HttpResult;
2014-10-27 05:23:36 +01:00
import com.commafeed.backend.model.Feed;
2015-06-04 12:28:20 +02:00
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.Channel;
import com.google.api.services.youtube.model.ChannelListResponse;
import com.google.api.services.youtube.model.Thumbnail;
2014-10-26 12:25:44 +01:00
2015-08-17 16:30:27 +02:00
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
2014-10-26 12:25:44 +01:00
@Slf4j
2015-08-17 16:30:27 +02:00
@RequiredArgsConstructor(onConstructor = @__({ @Inject }) )
2014-10-26 12:25:44 +01:00
@Singleton
public class YoutubeFaviconFetcher extends AbstractFaviconFetcher {
private final HttpGetter getter;
2015-06-04 12:28:20 +02:00
private final CommaFeedConfiguration config;
2014-10-26 12:25:44 +01:00
@Override
public Favicon fetch(Feed feed) {
2014-10-27 05:23:36 +01:00
String url = feed.getUrl();
2015-06-04 12:28:20 +02:00
if (!url.toLowerCase().contains("youtube.com/feeds/videos.xml")) {
2014-10-26 12:25:44 +01:00
return null;
}
2015-06-04 12:28:20 +02:00
String googleAuthKey = config.getApplicationSettings().getGoogleAuthKey();
if (googleAuthKey == null) {
log.debug("no google auth key configured");
2014-10-26 12:25:44 +01:00
return null;
}
byte[] bytes = null;
String contentType = null;
try {
2015-06-04 12:28:20 +02:00
List<NameValuePair> params = URLEncodedUtils.parse(url.substring(url.indexOf("?") + 1), StandardCharsets.UTF_8);
Optional<NameValuePair> userId = params.stream().filter(nvp -> nvp.getName().equalsIgnoreCase("user")).findFirst();
2015-08-18 13:14:35 +02:00
Optional<NameValuePair> channelId = params.stream().filter(nvp -> nvp.getName().equalsIgnoreCase("channel_id")).findFirst();
2015-06-04 12:28:20 +02:00
if (!userId.isPresent() && !channelId.isPresent()) {
return null;
}
2014-10-26 12:25:44 +01:00
2015-06-04 12:28:20 +02:00
YouTube youtube = new YouTube.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(),
new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("CommaFeed").build();
YouTube.Channels.List list = youtube.channels().list("snippet");
list.setKey(googleAuthKey);
if (userId.isPresent()) {
list.setForUsername(userId.get().getValue());
} else {
list.setId(channelId.get().getValue());
}
2014-10-26 12:25:44 +01:00
2015-06-04 12:28:20 +02:00
log.debug("contacting youtube api");
ChannelListResponse response = list.execute();
if (response.getItems().isEmpty()) {
log.debug("youtube api returned no items");
2014-10-26 12:25:44 +01:00
return null;
}
2015-06-04 12:28:20 +02:00
Channel channel = response.getItems().get(0);
Thumbnail thumbnail = channel.getSnippet().getThumbnails().getDefault();
log.debug("fetching favicon");
HttpResult iconResult = getter.getBinary(thumbnail.getUrl(), TIMEOUT);
2014-10-26 12:25:44 +01:00
bytes = iconResult.getContent();
contentType = iconResult.getContentType();
} catch (Exception e) {
log.debug("Failed to retrieve YouTube icon", e);
}
if (!isValidIconResponse(bytes, contentType)) {
return null;
2014-10-26 12:25:44 +01:00
}
return new Favicon(bytes, contentType);
2014-10-26 12:25:44 +01:00
}
}