-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
71 lines (61 loc) · 3.13 KB
/
index.php
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
<?php Kirby::plugin('mirthe/bookblock', [
'options' => [
'cache' => true
],
'tags' => [
'bookblock' => [
'attr' =>[
'isbn'
],
'html' => function($tag) {
$isbn = $tag->isbn;
// not sure if Google API is (and remains) free. But Open Library doesn't offer description..
// $url = "https://openlibrary.org/api/books?bibkeys=ISBN:9780980200447&jscmd=details&format=json";
// $url = "https://openlibrary.org/isbn/".$isbn.".json";
// TODO Try others from https://blog.hubspot.com/website/api-books
$url = "https://www.googleapis.com/books/v1/volumes?q=isbn:".$isbn;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$rawdata = curl_exec($ch);
curl_close($ch);
$completebookinfo = json_decode($rawdata, true);
// print_r($completebookinfo); exit();
if (isset($completebookinfo['items'])) {
$bookinfo = $completebookinfo['items'][0]['volumeInfo'];
// TODO offer both links?
// $booklink = $bookinfo['canonicalVolumeLink'];
$booklink = "https://www.goodreads.com/search?q=". $isbn;
$mijnoutput = '<div class="well">';
if (array_key_exists('imageLinks', $bookinfo)) {
$mijnoutput .= '<div class="well-img"><img src="'.str_replace('http://', 'https://', $bookinfo['imageLinks']['thumbnail']).'" alt="" width="128"></div>';
}
$mijnoutput .= '<div class="well-body">';
$mijnoutput .= '<p><a href="'.$booklink.'">'.$bookinfo['title']."</a> - ".$bookinfo['authors'][0]."<br>";
$mijnoutput .= 'Gepubliceerd '. $bookinfo['publishedDate'] ." • ";
$mijnoutput .= $bookinfo['pageCount']." pagina's</p>";
if (array_key_exists('description', $bookinfo)) {
$mijnoutput .= '<p>'.mb_strimwidth($bookinfo['description'], 0, 350, '…')."</p>";
// TODO add collapse if text is longer
}
if (array_key_exists('categories', $bookinfo)) {
$i = 0;
$mijnoutput .= "<ul class=\"genres\">";
foreach ($bookinfo['categories'] as $genre) {
$mijnoutput .= '<li>'. $genre . "</li>";
if (++$i == 5) {
break;
}
}
$mijnoutput .= "</ul>";
}
$mijnoutput .= '</div></div>';
} else {
$mijnoutput = '<p><small>Error in Bookblock - ISBN '.$isbn. '</small></p>';
}
return $mijnoutput;
}
]
]
]);
?>