-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay.js
264 lines (234 loc) · 10.2 KB
/
play.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
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
var ss = window.speechSynthesis;
var greeting = new Greeting();
//because it's good practise to cancel ss before using it
ss.cancel();
//getting the Greeting text from the Hard coded greeting class, most probably you'd want to use something else, maybe an api IDK
var greeting_text = greeting.getGreeting();
if(greeting_text)
speak(greeting_text);
if(!greeting.doneToday())
greeting.setGDT();
var getQuotes = function(){
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://quotes.rest/qod.json?category=inspire", true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var data = JSON.parse(xhr.responseText);
if(data.success && data.success.total>0){
var quote = data.contents.quotes[0];
console.log(quote.quote.split("."));
document.getElementById('quote-top').style = "background:linear-gradient(rgba(0, 255, 0, 0.16),rgba(155, 19, 255, 0.56)),url('"+quote.background+"') no-repeat 50% 50%;background-size: cover;-webkit-background-size: cover;-moz-background-size: cover;-o-background-size: cover;-ms-background-size: cover;padding:0%";
document.getElementById('quote').innerText = quote.quote;
document.getElementById('quote-author').innerText = quote.author;
/*
if(quote.quote.length>150){
var msgs = quote.quote.split(".");
var initMsg = new SpeechSynthesisUtterance("One inspiration a day keeps the depression away. Your quote for the day is from " + quote.author + ", and says, ");
initMsg.language = 'en-US';
ss.speak(initMsg);
msgs.forEach(function(v,k){
var msg = new SpeechSynthesisUtterance(v);
msg.language = 'en-US';
ss.speak(msg)
})
}else{
var msg = new SpeechSynthesisUtterance("One inspiration a day keeps the depression away. Your quote for the day is from " + quote.author + ", and says, "+quote.quote);
msg.language = 'en-US';
ss.speak(msg)
}
*/ var msg = new SpeechSynthesisUtterance("One inspiration a day keeps the depression away. Your quote for the day is from " + quote.author + ", and says, "+quote.quote);
msg.language = 'en-US';
ss.speak(msg)
}
}
}
};
xhr.send();
};
//getQuotes();
var getForecast = function(){
var city = ls("city");
if(!city)
city = "Bulawayo"; //because its my home town
var url = "http://api.openweathermap.org/data/2.5/weather?q="+city+"&APPID=bcd6b50b317746d1c0898b4a567c6da3&units=metric";
fetch(url, { method: 'GET' })
.then(function(fetchResponse){ return fetchResponse.json() })
.then(function(response) {
var weather = response.weather[0];
//setting the image for the day
getForecastImage(weather);
var avgTemp = Math.round(number_format(response.main.temp,".",""));
var minTemp = Math.round(number_format(response.main.temp_min,".",""));
var weatherDesc = new SpeechSynthesisUtterance("Todays weather description is: " + weather.description+", and average temperature in, "+ls("city")+", will be " + avgTemp + "degrees celcius.");
ss.speak(weatherDesc);
document.getElementById('card__weather_description').innerText = weather.main;
document.getElementById('card__average_temperature').innerText = avgTemp+" °C";
document.getElementById('card__min_temperature').innerText = minTemp + "°C (min)";
var icons = new Skycons({"color": "white"});
var suggestion = {};
/*
//regex way
switch (true) {
case /xyz/.weather.description:
display("• Matched 'xyz' test");
break;
case /test/.test(str):
display("• Matched 'test' test");
break;
case /ing/.test(str):
display("• Matched 'ing' test");
break;
default:
display("• Didn't match any test");
break;
}
*/
switch(weather.main.toLowerCase()){
case 'thunderstorm' || 'rain' || 'shower rain':
suggestion.long = "You should carry an umbrella or raincoat with you on your way out.";
suggestion.short = "Carry umbrella or raincoat.";
icons.set("icon", 'rain');
break;
case 'clouds':
suggestion.long = "You never know, the rains might come, so just carry that umbrella in case.";
suggestion.short = "Clouds could mean rains are coming.";
icons.set("icon", 'cloudy');
break;
case 'clear sky' || 'sky':
suggestion.long = "You will have clear skies above you today, so, if you can spend much time outside and enjoy the goodness of God\'s creation.";
suggestion.short = "Feel free to wear anything.";
break;
default:
suggestion.short = "Unavailable";
suggestion.long = "Sometimes as a human being, you just have to do things on your own, So today, naah fam. I a'int telling you what to wear.";
}
icons.play();
if(suggestion.long && suggestion.short){
document.getElementById('card__suggestion').innerText = suggestion.short;
var advice = new SpeechSynthesisUtterance(suggestion.long);
ss.speak(advice);
}
/*
cardElement.querySelector('.card__title').textContent = response.name;
cardElement.querySelector('.card__desc').textContent = response.bio;
cardElement.querySelector('.card__img').setAttribute('src', response.avatar_url);
cardElement.querySelector('.card__following span').textContent = response.following;
cardElement.querySelector('.card__followers span').textContent = response.followers;
cardElement.querySelector('.card__temp span').textContent = response.company;
*/
})
.catch(function (error) {
//If user is offline and sent a request, store it in localStorage
//Once user comes online, trigger bg sync fetch from application tab to make the failed request
localStorage.setItem('request', name);
console.error(error);
});
}();
var getForecastImage = function(weather){
var per_page = 50;
var max;
var url = "https://pixabay.com/api/?per_page="+per_page+"&min_width=150&q=beautiful+"+weather.main+"&image_type=photo&key="+pixalbayKey+"&order=popular&pretty=true";
fetch(url, { method: 'GET' })
.then(function(fetchResponse){ return fetchResponse.json() })
.then(function(response) {
if(response.total>=1){
max = (response.total>1) ? response.total-1 : 1;
var image = response.hits[rand(0,max)];
if(!image)
image = response.hits[0];
document.getElementById('weather-card-top').style = "background:linear-gradient(rgba(155, 255, 0, 0.16),rgba(155, 200, 255, 0.56)),url('"+image.webformatURL+"') no-repeat 50% 50%;background-size: cover;-webkit-background-size: cover;-moz-background-size: cover;-o-background-size: cover;-ms-background-size: cover;padding:0%";
}
})
.catch(function (error) {
//If user is offline and sent a request, store it in localStorage
//Once user comes online, trigger bg sync fetch from application tab to make the failed request
localStorage.setItem('request', name);
console.error(error);
});
};
var getNews = function(){
var chosen = [];
var articles = {};
//tech
articles.tech = [];
var business_spoken = false;
var sports_spoken = false;
var entertainment_spoken = false;
var tech_spoken = false;
var preferences = ls("preferences");
articles.business = [];
articles.entertainment = [];
articles.sports = [];
articles.general = [];
$.each(preferences, function(key,val){
$.each(newsSources.categories[val], function(k,v){
var url = "https://newsapi.org/v1/articles?source="+v.short+"&apiKey=c5e14bdca75045708df957ef0d58569d";
fetch(url, { method:"GET"})
.then(function(fetchResponse){ return fetchResponse.json() })
.then(function(d) {
if(d.status=="ok"){
$.each(d.articles, function(kk,vv){
if(kk<1){
vv.source = v;
appendToNews(val,vv);
articles[val].push(vv);
}
});
if(val=="tech")
if(!tech_spoken){
//now you have to change the code so that the utterences are random
var techMsg = new SpeechSynthesisUtterance("In the world of Tech... " + articles.tech[rand(0,articles.tech.length-1)].title+".");
ss.speak(techMsg);
tech_spoken = true;
}
if(val=="business")
if(!business_spoken){
var article = articles.business[rand(0,articles.business.length-1)];
var businessMsg = new SpeechSynthesisUtterance("Today in business. " + article.title + ". This Article is written by " + article.author + " of " + article.source.name)
ss.speak(businessMsg);
business_spoken = true;
}
if(val=="sports")
if(!sports_spoken){
var sportsMsg = new SpeechSynthesisUtterance("Topping in sports, " + articles.sports[rand(0,articles.sports.length-1)].title+".");
ss.speak(sportsMsg);
sports_spoken = true;
}
if(val=="entertainment")
if(!entertainment_spoken) {
var entertainmentMsg = new SpeechSynthesisUtterance("Today in the glitz and glam, " + articles.entertainment[rand(0, articles.entertainment.length - 1)].title+".");
ss.speak(entertainmentMsg);
entertainment_spoken = true;
}
}
})
.catch(function (error) {
//If user is offline sent a request, store it in localStorage
//Once user comes online, trigger bg sync fetch from application tab to make the failed request
localStorage.setItem('request', name);
console.error(error);
})
});
});
};
//lazy
function appendToNews(type,vv){
$('.'+type+'-news').append('' +
'<a class="card" id="news-card">'+
'<div class="card__spinner"></div>'+
'<div class="card__container">'+
'<div class="news-card-top" id="news-card-top" style="background:linear-gradient(rgba(0, 0, 0, 0.88),rgba('+rand(0,255)+', '+rand(0,255)+', '+rand(0,255)+', 0.56)),url('+vv.urlToImage+') no-repeat 50% 50%;background-size: cover;-webkit-background-size: cover;-moz-background-size: cover;-o-background-size: cover;-ms-background-size: cover;padding:0%">'+
'<p onclick=goToURL("'+vv.url+'") class="" style="color:white;text-decoration:none;font-size:22pt;text-align:left;padding:2%">'+
'<span id="article-title">'+vv.title+'</span>'+
'</p>'+
'<p id="card__weather_description" class="card__weather_description" style="color:white;text-decoration:none;font-size:16pt;text-align:center;display: block;width: 100%;margin-bottom:2%">'+vv.source.name+'</p>'+
'</div>'+
'<p class="card__temp">'+
vv.description+
'</p>'+
'</div>'+
'</a>'
);
$('.'+type+'-news-title').show();
}