~didrocks/+junk/face-detection-15.04

« back to all changes in this revision

Viewing changes to facedetection/www/bower_components/sw-toolbox/recipes/cache-expiration-options/app.js

  • Committer: Didier Roche
  • Date: 2016-05-10 23:09:11 UTC
  • Revision ID: didier.roche@canonical.com-20160510230911-c7xr490zrj3yrzxd
New version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* eslint-env browser */
 
2
'use strict';
 
3
 
 
4
// Please register for your own YouTube API key!
 
5
// https://developers.google.com/youtube/v3/getting-started#before-you-start
 
6
const API_KEY = 'AIzaSyC4trKMxwT42TUFHmikCc4xxQTWWxq5S0g';
 
7
const API_URL = 'https://www.googleapis.com/youtube/v3/search';
 
8
 
 
9
function serializeUrlParams(params) {
 
10
  return Object.keys(params).map(key => {
 
11
    return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
 
12
  }).join('&');
 
13
}
 
14
 
 
15
function youtubeSearch(searchTerm, maxResults) {
 
16
  let params = {
 
17
    part: 'snippet',
 
18
    maxResults: maxResults,
 
19
    order: 'date',
 
20
    key: API_KEY,
 
21
    q: searchTerm
 
22
  };
 
23
 
 
24
  let url = new URL(API_URL);
 
25
  url.search = serializeUrlParams(params);
 
26
 
 
27
  return fetch(url).then(response => {
 
28
    if (response.ok) {
 
29
      return response.json();
 
30
    }
 
31
    throw new Error(`${response.status}: ${response.statusText}`);
 
32
  }).then(function(json) {
 
33
    return json.items;
 
34
  });
 
35
}
 
36
 
 
37
document.querySelector('#search').addEventListener('submit', event => {
 
38
  event.preventDefault();
 
39
 
 
40
  var results = document.querySelector('#results');
 
41
  while (results.firstChild) {
 
42
    results.removeChild(results.firstChild);
 
43
  }
 
44
 
 
45
  let searchTerm = document.querySelector('#searchTerm').value;
 
46
  let maxResults = document.querySelector('#maxResults').value;
 
47
 
 
48
  youtubeSearch(searchTerm, maxResults).then(videos => {
 
49
    videos.forEach(video => {
 
50
      let img = document.createElement('img');
 
51
      img.src = video.snippet.thumbnails.medium.url;
 
52
      results.appendChild(img);
 
53
    });
 
54
  }).catch(error => console.warn('YouTube search failed due to', error));
 
55
});