use of com.google.api.client.util.Joiner in project api-samples by youtube.
the class GeolocationSearch method main.
/**
* Initialize a YouTube object to search for videos on YouTube. Then
* display the name and thumbnail image of each video in the result set.
*
* @param args command line args.
*/
public static void main(String[] args) {
// Read the developer key from the properties file.
Properties properties = new Properties();
try {
InputStream in = GeolocationSearch.class.getResourceAsStream("/" + PROPERTIES_FILENAME);
properties.load(in);
} catch (IOException e) {
System.err.println("There was an error reading " + PROPERTIES_FILENAME + ": " + e.getCause() + " : " + e.getMessage());
System.exit(1);
}
try {
// This object is used to make YouTube Data API requests. The last
// argument is required, but since we don't need anything
// initialized when the HttpRequest is initialized, we override
// the interface and provide a no-op function.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("youtube-cmdline-geolocationsearch-sample").build();
// Prompt the user to enter a query term.
String queryTerm = getInputQuery();
// Prompt the user to enter location coordinates.
String location = getInputLocation();
// Prompt the user to enter a location radius.
String locationRadius = getInputLocationRadius();
// Define the API request for retrieving search results.
YouTube.Search.List search = youtube.search().list("id,snippet");
// Set your developer key from the {{ Google Cloud Console }} for
// non-authenticated requests. See:
// {{ https://cloud.google.com/console }}
String apiKey = properties.getProperty("youtube.apikey");
search.setKey(apiKey);
search.setQ(queryTerm);
search.setLocation(location);
search.setLocationRadius(locationRadius);
// Restrict the search results to only include videos. See:
// https://developers.google.com/youtube/v3/docs/search/list#type
search.setType("video");
// As a best practice, only retrieve the fields that the
// application uses.
search.setFields("items(id/videoId)");
search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
// Call the API and print results.
SearchListResponse searchResponse = search.execute();
List<SearchResult> searchResultList = searchResponse.getItems();
List<String> videoIds = new ArrayList<String>();
if (searchResultList != null) {
// Merge video IDs
for (SearchResult searchResult : searchResultList) {
videoIds.add(searchResult.getId().getVideoId());
}
Joiner stringJoiner = Joiner.on(',');
String videoId = stringJoiner.join(videoIds);
// Call the YouTube Data API's youtube.videos.list method to
// retrieve the resources that represent the specified videos.
YouTube.Videos.List listVideosRequest = youtube.videos().list("snippet, recordingDetails").setId(videoId);
VideoListResponse listResponse = listVideosRequest.execute();
List<Video> videoList = listResponse.getItems();
if (videoList != null) {
prettyPrint(videoList.iterator(), queryTerm);
}
}
} catch (GoogleJsonResponseException e) {
System.err.println("There was a service error: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
} catch (IOException e) {
System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
}
Aggregations