-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
191 lines (167 loc) · 6.33 KB
/
script.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
// Carregar o JSON
function readTextFile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function () {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
}
rawFile.send(null);
}
// let num = 0;
readTextFile("./dados/pessoas.json", function (text) {
var data = JSON.parse(text);
// Selecionar o div onde vao ser colocados os cards
var cards = document.getElementById("dados");
// Selecionar o div na sidebar onde estao os botoes dos users
var btns = document.getElementById("btns");
// Loop dos dados que foi buscar ao json
for (let i in data) {
// Cria a div com o card que contem os dados do utilizador
var el = document.createElement("div");
el.className = "card p-md-3 p-2 mt-md-4 mt-1 bg-dark text-light border-primary";
el.id = "card" + i;
el.innerHTML =
'<div class="row">\n' +
'<div class="col-md-4 text-center"> \n' +
'<img src="' + data[i].img +
'" class="card-img-top p-1 border border-primary" alt="Foto Perfil" style="max-width: 60vw;">\n' +
'</div>\n' +
'<div class="col-md-8">\n' +
'<div class="card-body">\n' +
'<h3 class="card-title"> ' + data[i].nome + ' </h3>\n' +
'<h6 class="card-subtitle mb-2 text-primary"> ' + data[i].numero + ' </h6>\n' +
'<p class="card-text"> ' + data[i].curso + ' </p>\n' +
'<p class="card-text mt-2"> ' + data[i].obv + '</p> \n' +
'</div>\n' +
'</div>\n' +
'</div>\n';
cards.append(el);
// Botao para ir rapidamente para o card
var bt = document.createElement("a");
bt.className = "btn btn-dark";
bt.innerHTML = data[i].nome;
bt.href = "#card" + i
btns.append(bt);
// num = parseInt(i) + 1;
}
});
/* ---------------------------------------------------------------------- */
/* -------------------------- Cat as a service -------------------------- */
/* ---------------------------------------------------------------------- */
// Cria o elemento com o imagem do gato
function getCats() {
// API para mostrar fotos de gatos
var cat = document.getElementById("cat");
cat.innerHTML = '<div> <img src="https://cataas.com/cat" alt="CATAAS Cat Picture"> </div>';
}
// getCats();
document.getElementById("catBtn").addEventListener("click", getCats);
/* ---------------------------------------------------------------------- */
/* --------------------------- OpenWeatherMap --------------------------- */
/* ---------------------------------------------------------------------- */
async function getWeather() {
let weather = document.querySelector('#openWeather').value;
let url = 'api.openweathermap.org/data/2.5/weather';
let token = '7881712858c48cc3f110e0132c47e411';
let doc = document.getElementById("w");
doc.style.display = 'block';
// Verifica se existe input
if (weather) {
doc.innerHTML = '<div class="d-flex justify-content-center">' +
'<div class="spinner-border" role="status">' +
'<span class="visually-hidden">Loading...</span>' +
'</div>' +
'</div>'
// Tenta fazer o pedido á api
try {
let res = await fetch('https://' + url + '?q=' + weather + '&appid=' + token + '&lang=PT');
// Verifica se recebeu resposta
if (!res.ok) {
// Not found
if (res.status == '404') {
// alert('Parametros incoretos');
doc.innerHTML = 'Parametros incoretos'
return;
}
}
// Dados de meteriologia
let dados = await res.json();
console.log(dados);
doc.innerHTML = '<div class="card-body">\n' +
'<img src="http://openweathermap.org/img/w/' + dados.weather[0].icon + '.png"\n' +
'<h2 class="card-title"> ' + dados.weather[0].description + ' </h2>\n' +
'<h6 class="card-subtitle mb-2 text-primary"> ' + (dados.main.temp - 273).toFixed(1) + ' ºC </h6>\n' +
'<h6 class="card-subtitle mb-2 text-primary"> Min: ' + (dados.main.temp_min - 273).toFixed(1) + ' ºC | Max: ' + (dados.main.temp_max - 273).toFixed(1) + ' ºC </h6>\n' +
'<p class="card-subtitle mb-2 text-primary"> Humidade: ' + dados.main.humidity + '% </p>\n' +
'<p class="card-subtitle mb-2 text-primary"> Vento: ' + dados.wind.speed + 'Km/h ' + dados.wind.deg + 'º </p>\n' +
'<p class="card-text">' + dados.name + ' - ' + dados.sys.country + ' </p>\n' +
'</div>';
} catch (error) {
console.log(error);
}
// alert(num);
}
}
document.getElementById("weather").addEventListener("click", getWeather);
/* ---------------------------------------------------------------------- */
/* -------------------------------- IMDB -------------------------------- */
/* ---------------------------------------------------------------------- */
async function getSerie() {
let input = document.querySelector('#imdbInput').value;
let url = 'https://imdb-api.com/en/API/SearchSeries/';
let token = 'k_2kvwvxlm';
let doc = document.getElementById("imdbDiv");
doc.style.display = 'block';
// Verifica se existe input
if (input) {
doc.innerHTML = '<div class="d-flex justify-content-center">' +
'<div class="spinner-border" role="status">' +
'<span class="visually-hidden">Loading...</span>' +
'</div>' +
'</div>'
// Tenta fazer o pedido á api
try {
let res = await fetch(url + token + '/' + input);
console.log(res);
// Verifica se recebeu resposta
if (!res.ok) {
// Not found
if (res.status == '404') {
alert('Parametros incoretos');
doc.innerHTML('Parametros incoretos')
return;
}
}
// Dados de meteriologia
let dados = await res.json();
console.log(dados);
data = dados.results;
doc.innerHTML = "";
for(let i in data) {
var el = document.createElement("div");
el.className = "card p-md-3 p-2 mt-md-4 mt-1 bg-dark text-light border-primary";
el.id = "card" + i;
el.innerHTML =
'<div class="row">\n' +
'<div class="col-md-4 text-center"> \n' +
'<img src="' + data[i].image +
'" class="card-img-top p-1 border border-primary" alt="Foto Perfil" style="max-width: 60vw;">\n' +
'</div>\n' +
'<div class="col-md-8">\n' +
'<div class="card-body">\n' +
'<h3 class="card-title"> ' + data[i].title + ' </h3>\n' +
'<h6 class="card-subtitle mb-2 text-primary"> ' + data[i].description + ' </h6>\n' +
'</div>\n' +
'</div>\n' +
'</div>\n';
doc.append(el);
}
} catch (error) {
console.log(error);
}
}
}
document.getElementById('imdb').addEventListener("click", getSerie);