-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapod.html
134 lines (101 loc) · 3.16 KB
/
apod.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
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>APOD</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="loader.css">
</head>
<body>
<div id="app">
<div class="loading" v-if="loading">Loading…</div>
<div class="container py-2">
<h2>Busca de imagem/vídeo do dia no APOD</h2>
<form action="">
<div class="form-group row">
<div class="col-6">
<label>Data</label>
<input type="text" name="data" class="form-control" v-model:dataExibicao="dataExibicao">
</div>
</div>
<div class="form-group">
<button v-on:click="buscaImgPorData" class="btn btn-primary">Buscar</button>
</div>
</form>
<img :src="imgSrc" :title="imgTitulo" v-if="ehImg">
<div class="embed-responsive embed-responsive-16by9" v-else>
<iframe width="420" height="315"
:src="imgSrc">
</iframe>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="chave.js"></script>
<script>
var urlAPI = 'https://api.nasa.gov/planetary/apod?api_key=' + chaveApi + '&date=';
var app = new Vue({
el: '#app',
data: {
imgTitulo: '',
imgSrc: '',
dataExibicao: '',
dataFormatada: '',
ehImg: '',
loading: false
},
methods: {
carregaImg: function(){
this.loading = true;
axios.get(urlAPI+this.dataFormatada)
.then(function(resposta){
app.loading = false;
app.ehImg = resposta.data.url.indexOf("youtube") > 0 ? false : true;
app.imgSrc = resposta.data.url;
app.imgTitle = resposta.data.title;
})
.catch(function(){
app.loading = false;
console.error('Ocorreu um erro ao realizar a busca, tente novamente.');
});
},
buscaImgPorData: function(e){
this.loading = true;
e.preventDefault();
this.dataFormatada = this.formatarDataNasa(this.dataExibicao);
axios.get(urlAPI+this.dataFormatada)
.then(function(resposta){
this.loading = false;
app.ehImg = resposta.data.url.indexOf("youtube") > 0 ? false : true;
app.imgSrc = resposta.data.url;
app.imgTitle = resposta.data.title;
})
.catch(function(){
this.loading = false;
console.error('Ocorreu um erro ao realizar a busca, tente novamente.');
});
},
hoje: function(){
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
this.dataExibicao = dd + '/' + mm + '/' + yyyy;
this.dataFormatada = yyyy + '-' + mm + '-' + dd;
},
formatarDataNasa: function(data){
return data.split("/").reverse().join('-');
}
},
created: function(){
this.hoje();
this.carregaImg();
}
});
</script>
</body>
</html>