Search in sources :

Example 1 with DefaultBackoffStrategy

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);
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) DefaultBackoffStrategy(org.apache.http.impl.client.DefaultBackoffStrategy) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpClient(org.apache.http.client.HttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ReadContext(com.jayway.jsonpath.ReadContext)

Aggregations

ReadContext (com.jayway.jsonpath.ReadContext)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 NameValuePair (org.apache.http.NameValuePair)1 HttpClient (org.apache.http.client.HttpClient)1 HttpGet (org.apache.http.client.methods.HttpGet)1 DefaultBackoffStrategy (org.apache.http.impl.client.DefaultBackoffStrategy)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1