-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.js
79 lines (69 loc) · 2.45 KB
/
search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Your use of the YouTube API must comply with the Terms of Service:
// https://developers.google.com/youtube/terms
urlArr = [];
imgArr = [];
addedGmap = false;
// Helper function to display JavaScript value on HTML page.
function showResponse(response) {
var id = null;
var src = 'https://learn.getgrav.org/user/pages/11.troubleshooting/01.page-not-found/error-404.png';
var url = null;
try {
id = response.items[0].id.videoId;
src = 'https://img.youtube.com/vi/' + id + '/1.jpg';
url = 'https://youtube.com/watch?v=' + id;
}
catch(e) {
console.log("Error");
}
finally {
urlArr.push(url);
imgArr.push(src);
}
}
// Called automatically when JavaScript client library is loaded.
function onClientLoad() {
gapi.client.load('youtube', 'v3', onYouTubeApiLoad);
}
// Called automatically when YouTube API interface is loaded (see line 9).
function onYouTubeApiLoad() {
gapi.client.setApiKey('AIzaSyB0xaH8jN_ZhFwflugHhfPJPkZEPXSjKeY');
search();
}
// Called automatically with the response of the YouTube API request.
function onSearchResponse(response) {
showResponse(response);
if(!addedGmap) {
addedGmap = true;
var gmapScript = document.createElement("script");
gmapScript.src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB0xaH8jN_ZhFwflugHhfPJPkZEPXSjKeY&callback=initMap";
document.body.appendChild(gmapScript);
}
}
function search() {
// check if user entered a song
if (window.location.href != "https://tmlabonte.github.io/SBHacks2018/")
{
var cities = ["Los Angeles", "New York", "India", "Australia", "China", "Brazil", "London", "Nigeria", "Russia", "Egypt", "Korea", "South Africa", "Colombia"];
for (var i=0; i < cities.length; i++) {
// Use the JavaScript client library to create a search.list() API call.
var request = gapi.client.youtube.search.list({
q: query.song + " dance " + cities[i],
maxResults: 1,
part: "id",
order: "viewCount",
type: "video",
videoDefinition: "high"
});
// Send the request to the API server,
// and invoke onSearchRepsonse() with the response.
request.execute(onSearchResponse);
}
}
else {
addedGmap = true;
var gmapScript = document.createElement("script");
gmapScript.src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB0xaH8jN_ZhFwflugHhfPJPkZEPXSjKeY&callback=initMap";
document.body.appendChild(gmapScript);
}
}