-
Notifications
You must be signed in to change notification settings - Fork 0
/
index1.html
273 lines (246 loc) · 10.2 KB
/
index1.html
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="styles.css">
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/annyang/1.4.0/annyang.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
</head>
<body>
<div class="container">
<div id="navigation">
</div>
<div class="searchLanding">
<div class="searchbox">
<input id="search" type="text" name="search" value="" placeholder="enter search query">
</div>
</div>
</div>
<div id="result" aria-live="polite">
<p id="recognized"></p>
<div id="matched"></div>
</div>
<script>
function validateForm() {
var x=document.forms["myForm"]["q"].value;
console.log('query string=' + x);
if (x==null || x=="")
var error = "Empty query, Please enter a query.";
else if(/\d/.test(x)) {
var regUKPostcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/;
var regUSCANZipcode = /(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)/;
var regLatLong = /^(-?(90|(\d|[1-8]\d)(\.\d{1,6}){0,1}))\,{1}(-?(180|(\d|\d\d|1[0-7]\d)(\.\d{1,6}){0,1}))$/;
var validIP = function(ip) {
var values = ip.split('.');
if(values.length != 4) return false;
for(var v in values)
if(values[v] < 0 || values[v] > 255) return false;
return true;
};
if(validIP(x)) {
updateDom(x);
return false;
} else if(regUSCANZipcode.test(x)) {
updateDom(x);
return false;
} else if(regUKPostcode.test(x)) {
updateDom(x);
return false;
} else if(regLatLong.test(x)) {
updateDom(x);
return false;
} else
error = "Please enter an valid US/UK/Canada Postal Code or IP or (Lat,Long) query.";
} else {
updateDom(x);
return false;
}
document.getElementById('status').innerHTML = error;
return false;
};
function updateDom(q) {
getLocalWeather(q, function(result) {
document.getElementById('weatherData').innerHTML = result;
document.getElementById('status').innerHTML = "";
});
return false;
}
function errorView(error) {
document.getElementById('status').innerHTML = error;
}
function resultView(jsonres) {
var template = '<img src="<%= icon %>" alt="Weather Condition" />\
<div><b><%= location %></b> </div>\
<div style="color:#07CFFC"><b><%= date %></b> </div>\
<div style="color:#1707FC"> <b><%= weatherDesc %></b> </div>\
<div style="color:#FC071B"><b><%= temp %>\u2103</b></div>\
<div>\
<li>Location: <b><%= location %></b></li>\
<li>Date: <b><%= date %></b></li>\
<li>Weather Condition: <b><%= weatherDesc %></b></li>\
<li>Temperature: <b><%= temp %>\u2103</b></li>\
<li>Max Temperature: <b><%= tempMaxC %>\u2103</b></li>\
<li>Min Temperature: <b><%= tempMinC %>\u2103</b></li>\
<li>Cloud Cover: <b><%= cloudcover %></b></li>\
<li>Humidity: <b><%= humidity %></b></li>\
<li>Observation Time: <b><%= observation_time %></b></li>\
<li>Precipitation: <b><%= precipMM %>MM</b></li>\
<li>Pressure: <b><%= pressure %></b></li>\
<li>Visibility: <b><%= visibility %></b></li>\
<li>Wind Direction 16Point: <b><%= winddir16Point %></b></li>\
<li>Wind Direction Degree: <b><%= winddirDegree %></b></li>\
<li>Wind Speed: <b><%= windspeed %>Kmph</b></li>\
</div>';
var vars =
{
location: jsonres.data.request[0].query,
date: jsonres.data.weather[0].date,
temp: jsonres.data.current_condition[0].temp_C,
weatherDesc: jsonres.data.current_condition[0].weatherDesc[0].value,
tempMaxC: jsonres.data.weather[0].tempMaxC,
tempMinC: jsonres.data.weather[0].tempMinC,
cloudcover: jsonres.data.current_condition[0].cloudcover,
humidity: jsonres.data.current_condition[0].humidity,
observation_time: jsonres.data.current_condition[0].observation_time,
precipMM: jsonres.data.current_condition[0].precipMM,
pressure: jsonres.data.current_condition[0].pressure,
visibility: jsonres.data.current_condition[0].visibility,
winddir16Point: jsonres.data.weather[0].winddir16Point,
winddirDegree: jsonres.data.weather[0].winddirDegree,
windspeed: jsonres.data.weather[0].windspeedKmph,
icon: jsonres.data.current_condition[0].weatherIconUrl[0].value,
};
return _.template(template, vars);
}
function getLocalWeather(q, onResult) {
var baseurl = "http://api.worldweatheronline.com/free/v1/weather.ashx";
//JSONP call
//Please note that 'key' here is only for demo purpose. You should replace
//it with your own key which can be obtained from registering on WWO website.
//Also you should put the key on server side on your own domain and get
//it using Ajax call when loading the parent html file, rather than put here
//at client side that everyone can see.
$.getJSON(baseurl + '?callback=?', {
q: q,
num_of_days: '1',
format: 'json',
key: 'xkq544hkar4m69qujdgujn7w'
})
.done(function( jsonres ) {
console.log('getJSON'+JSON.stringify(jsonres));
if(jsonres.data.hasOwnProperty('error'))
errorView(jsonres.data.error[0].msg);
else onResult(resultView(jsonres));
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ', ' + error;
errorView("Request Failed: " + err);
});
}
</script>
<script>
if (annyang) {
// Let's define our first command. First the text we expect, and then the function it should call
var commands = {
'play (the song) *song': playSong,
'play (the song) *song by *artist': playSong,
'stop': stop,
'google *term': function(term) {
var win = window.open('https://www.google.com/#q='+term, '_blank');
if(win){
//Browser has allowed it to be opened
win.focus();
}else{
//Broswer has blocked it
alert('Please allow popups for this site');
}
}
};
var showFlickr = function(tag) {
$('#flickrGallery').show();
$('#flickrLoader p').text('Searching for '+tag).fadeIn('fast');
var url = '//api.flickr.com/services/rest/?method=flickr.photos.search&api_key=e885e3321945f751d5ffd8e5e4405def
&sort=interestingness-desc&per_page=9&format=json&callback=jsonFlickrApi&tags='+tag;
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonFlickrApi',
contentType: "application/json",
dataType: 'jsonp'
});
scrollTo("#section_image_search");
};
var jsonFlickrApi = function(results) {
$('#flickrLoader p').fadeOut('slow');
var photos = results.photos.photo;
$.each(photos, function(index, photo) {
$(document.createElement("img"))
.attr({ src: '//farm'+photo.farm+'.staticflickr.com/'+photo.server+'/'+photo.id+'_'+photo.secret+'_s.jpg' })
.addClass("flickrGallery")
.appendTo(flickrGallery);
});
};
var audio = null;
function playSong(song, artist) {
var recognizedElement = document.getElementById('recognized');
recognizedElement.innerText = 'Recognized "' + song + (artist ? ' by ' + artist : '') + '"';
console.log("PlaySong", song);
var req = new XMLHttpRequest();
req.open('GET', 'https://api.spotify.com/v1/search?type=track&q=' + encodeURIComponent(song), true);
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200){
var data = JSON.parse(req.responseText);
var textField = document.getElementById('search').value;
if (!textField && data.tracks.items[0] ) {
stop();
var matchedElement = document.getElementById('matched');
console.log("reached");
matchedElement.innerHTML = '<div class="media"><img src="' + data.tracks.items[0].album.images[0].url + '" alt="Cover art of ' + data.tracks.items[0].album.name + '" width="300"><p>Playing ' + data.tracks.items[0].name + ' by ' + data.tracks.items[0].artists[0].name +'</p></div>';
audio = new Audio(data.tracks.items[0].preview_url);
audio.play();
}
}
};
req.send(null);
}
function stop() {
if (audio) {
audio.pause();
audio = null;
}
}
function stop() {
if (audio) {
audio.pause();
}
}
// Add our commands to annyang
annyang.addCommands(commands);
// Start listening. You can call this here, or attach this call to an event, button, etc.
annyang.start();
annyang.debug();
}
</script>
<h1>Smartbox</h1>
<form name="myForm" onsubmit="return validateForm()">
City or town name; IP address; US or Canada Postal Code or US Zipcode; (Latitude,longitude): <input type="text" name="q">
<input type="submit" value="Submit">
</form>
<div style="color:#B20528"><span id="status"></span></div>
<div id="weatherData"></div>
<script>
$(document).ready(function() {
$('body').keypress(function(e) {
if (e.keyCode === 13) {
recognition.abort();
commandText = $('#search').val();
$('.searchLanding, #navigation, .searchbox').addClass('submitted');
}
});
});
</script>
</body>
</html>