/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.youtube.gdata.test;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import com.google.gdata.client.youtube.YouTubeQuery;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.Link;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.util.ServiceException;
public class YouTubeCheckDuplicates {
/**
* Given an initialized YouTubeService object and a search term, this
* function tries to figure out how many unique video IDs are returned.
*
* @param service An initialized YouTubeService object.
* @param searchTerm A string to search for.
* @throws MalformedURLException This should never be thrown.
* @throws IOException Problem communicating with the server.
* @throws ServiceException Problem communicating with the server.
*/
public static void countUniques(YouTubeService service, String searchTerm) throws MalformedURLException, IOException, ServiceException {
YouTubeQuery query = null;
VideoFeed videoFeed = null;
HashSet<String> uniques = null;
uniques = new HashSet<String>(1000);
query = new YouTubeQuery(
new URL("http://gdata.youtube.com/feeds/api/videos"));
System.out.print("Querying for " + searchTerm);
query.setVideoQuery(searchTerm);
query.setMaxResults(25);
String queryUrl = query.getUrl().toString();
do {
videoFeed = service.getFeed(new URL(queryUrl), VideoFeed.class);
for(VideoEntry entry : videoFeed.getEntries() ) {
uniques.add(entry.getId());
}
//check if we have more results
Link nextLink = videoFeed.getLink("next", "application/atom+xml");
if(nextLink != null) {
queryUrl = nextLink.getHref();
}
else {
queryUrl = null;
}
System.out.print(".");
}
while (queryUrl != null);
System.out.println();
System.out.println("Unique entries:" + uniques.size());
}
public static void main(String[] args) {
YouTubeService service = new YouTubeService("YouTubeCheckDuplicates");
//some search terms with results way larger than 1000
String[] searchTerms = {"kittens", "puppies", "funny", "cute"};
try {
for(String searchTerm : searchTerms) {
countUniques(service, searchTerm);
}
}
catch(Exception ex) {
System.err.println("Ohnoes! Exception");
System.err.println(ex.toString());
System.err.println(ex.getMessage());
ex.printStackTrace();
}
//obscure Tron reference
System.out.println("End of line, program!");
}
}