use of org.apache.http.impl.client.DefaultBackoffStrategy in project selenium-tests by Wikia.
the class YoutubeVideoProvider method getLatestVideoForQuery.
/**
* This method returns latest youtube video(added no longer then hour ago) for a specified query.
* This one is using a YouTube Data API (v3) - see for reference -
* https://developers.google.com/youtube/v3/
*/
public static YoutubeVideo getLatestVideoForQuery(String searchQuery) {
HttpClient httpclient = HttpClientBuilder.create().setConnectionBackoffStrategy(new DefaultBackoffStrategy()).disableAutomaticRetries().build();
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("key", API_KEY));
nvps.add(new BasicNameValuePair("part", "snippet"));
nvps.add(new BasicNameValuePair("order", "date"));
nvps.add(new BasicNameValuePair("maxResults", "10"));
nvps.add(new BasicNameValuePair("q", searchQuery));
nvps.add(new BasicNameValuePair("publishedAfter", DateTime.now(DateTimeZone.forID("UTC")).minusMinutes(60).toString()));
nvps.add(new BasicNameValuePair("type", "video"));
HttpGet httpPost = new HttpGet("https://www.googleapis.com/youtube/v3/search?" + URLEncodedUtils.format(nvps, "utf-8"));
String videoTitle = null;
String videoUrl = null;
String videoId = null;
try {
HttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
ReadContext responseValue = JsonPath.parse(EntityUtils.toString(entity));
videoTitle = responseValue.read("$.items[0].snippet.title");
videoId = responseValue.read("$.items[0].id.videoId");
videoUrl = String.format("https://www.youtube.com/watch?v=%s", videoId);
} catch (IOException e) {
PageObjectLogging.log("A problem occurred while receiving a YouTube video", e, false);
}
return new YoutubeVideo(videoTitle, videoUrl, videoId);
}
Aggregations