-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
177 lines (158 loc) · 6.8 KB
/
index.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
<html>
<head>
<title>Random Book Picker</title>
<link rel="stylesheet" href="css/_normalize.css">
<link rel="stylesheet" href="css/loading.css">
<link rel="stylesheet" href="css/style.css">
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="https://unpkg.com/[email protected]/dist/axios.js"></script>
</head>
<body>
<div id="app">
<section class="main">
<div class="title">
<h1>Random Book Picker</h1>
</div>
<div class="urlForm" v-if="!hide">
<input type="text" v-model="goodreadsurl" placeholder="GoodReads URL"><button class="btn" @click="getBookList">Get Books</button>
<div class="instructions">
Copy the url of your GoodReads to read shelf and paste it in the field. Click Get Books to get your books from the shelf.
Once the books have loaded you can click Randomize to have it pick the next book for you to read.
</div>
</div>
<div v-else>
<div v-if="!loading">
<button v-if="!currentBook" @click="randomize" class="btn" >Randomize</button>
<button v-if="currentBook" @click="filteredBooklist = booklist; currentBook = null">Show all</button>
</div>
</div>
<div v-if="!loading" id="booklist">
<div class="row" v-if="!currentBook">
<div class="card" v-for="book in filteredBooklist">
<header>
<div class="bookTitle">{{book.title}}</div>
</header>
<div class="bookCover"><img :src="book.cover" /></div>
</div>
</div>
<div v-else>
<div class="currentbook">
<div class="container">
<h2>{{currentBook.title}}</h2>
<div class="">
<div class="row">
<img class="cover" :src="currentBook.cover"></img>
<div class="description" v-html="currentBook.description"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div v-else>
<div class="loading">
<div class="book">
<div class="inner">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<br>
<p>Loading...</p>
</div>
</div>
</section>
<footer class="footer">Copyright 2022</footer>
</div>
</body>
<script>
// import Vue
const { createApp } = Vue
// create the app
createApp({
data() {
return {
loading: null,
hide: false,
currentBook: null,
booklist: [],
filteredBooklist: [],
}
},
// define methods
methods: {
// get booklist from goodreads
async getBookList() {
// check if url is empty and if it is a goodreads url
if (!this.goodreadsurl || !this.goodreadsurl.includes("goodreads.com")) {
alert("Please enter a valid GoodReads URL")
return
}
this.loading = true
this.hide = true
// replace list with list_rss to get the xml
var grurl = this.goodreadsurl.replace("list", "list_rss")
// bypasse CORS and get the xml from goodreads
axios.get('https://api.allorigins.win/raw?url=' + grurl)
.then(response => {
// parse the xml
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(response.data, "text/xml");
var items = xmlDoc.getElementsByTagName("item");
// parse the xml and add the books to the booklist
for (var i = 0; i < items.length; i++) {
var book = {}
book.description = items[i].getElementsByTagName("book_description")[0].childNodes[0].nodeValue
book.title = items[i].getElementsByTagName("title")[0].childNodes[0].nodeValue
book.cover = items[i].getElementsByTagName("book_large_image_url")[0].childNodes[0].nodeValue
// get the isbn and query the google books api to get the description
//book.isbn = items[i].getElementsByTagName("isbn")[0].childNodes[0].nodeValue
this.booklist.push(book)
}
// set the filtered booklist to the booklist
this.loading = false
this.filteredBooklist = this.booklist
})
.catch(error => {
console.log(error)
})
},
// randomize the booklist and set the current book
randomize() {
this.currentBook = this.filteredBooklist[Math.floor(Math.random() * this.filteredBooklist.length)]
// add the description to the current book
//axios.get('https://www.googleapis.com/books/v1/volumes?q=isbn:' + this.currentBook.isbn)
// .then(response => {
// this.currentBook.description = response.data.items[0].volumeInfo.description
// })
// .catch(error => {
// console.log(error)
// })
}
},
mounted () {
}
}).mount('#app')
</script>
</html>