language: PHP (php 5.4.4)
date: 986 days 3 hours ago
link:
visibility: public
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
<?php
/**
* Classe qui utilise l'API de Google Book pour aller chercher des informations sur un livre en particulier ou faire des recherches par mot clé.
*  
* @author Olivier Arteau
* 
* Les deux principales méthodes sont :
* 
* getBooksByKeyword
*       - retourne un tableau de livre
*       
* getBookByISBN
*       - retourne le livre ou null si le livre n'existe pas
* 
* La structure d'un livres est la suivante :
*
* array(
*       'auteur' => '', // Auteur(s) du livre 
*       'date' => 0, // Date de publication au format timestamp
*       'identifier' => array(
*               'ISBN' => '', // ISBN de 10 chiffres
*               'ISBN2' => '', // ISBN de 13 chiffres
*               'googleid' => '' // Id Google du livre
*       ),
*       'titre' => '', // Titre du livre
*       'editeur' => '', // Éditeur du livre
*       'image' => '' // Lien vers une image miniature du livre
* );
* 
* Note: Les index sont absent pour les données qui ne sont pas disponible
 */
class BookSearcher {
        private $xpp;
        
        public function __construct() {
                
        }
        
        /**
         * Recherche de livre(s) par mot(s) clé(s). Retourne la liste des livres trouvés 
         * avec leur information.
         * 
         * @param (string) keyword(s)
         **/
        
        
        public function getBooksByKeyword($keyword) {
                $this->xpp = new XMLReader();
                $books = array();
                
                if ($this->xpp->open('http://books.google.com/books/feeds/volumes?q=' . urlencode($keyword))) {
                        $this->moveToEntry();
                        $books = array();
                        
                        while ($this->xpp->name == "entry") {
                                $books[] = $this->parseBook();
                        }
                }
                
                return $books;
        }
        
        /**
         * Cherche un livre par son ISBN et retourne un tableau contenant 
         * les informations relatives au livre
         * 
         * @param (string) ISBN
         **/
        
        public function getBookByISBN($isbn) {
                $this->xpp = new XMLReader();
                
                if ($this->xpp->open('http://books.google.com/books/feeds/volumes?q=' . urlencode($isbn))) {
                        $this->moveToEntry();
                        
                        $found = false;
                        while ($this->xpp->name == "entry") {
                                $book = $this->parseBook();
                                
                                // Si le livre est trouvé //
                                if (isset($book['identifier']) &&
                                        (strlen($isbn) == 10 && isset($book['identifier']['ISBN']) &&  $book['identifier']['ISBN'] == $isbn) || // ISBN
                                        (strlen($isbn) == 13 && isset($book['identifier']['ISBN2']) &&  $book['identifier']['ISBN2'] == $isbn)) // ISBN2
                                {
                                        $found = true;
                                        break;
                                }
                        }
                }
                
                return (!isset($book) || !$found) ? null : $book;
        }
        
        private function parseBook() {
                $book = array();
                        
                while ($this->xpp->read() && $this->xpp->name != "entry") {
                        if ($this->xpp->name{0} == "#")
                                continue;
                                
                        switch($this->xpp->name) {
                                case 'dc:creator':
                                        if (!isset($book['auteur']))
                                                $book['auteur'] = '';
                                        else
                                                $book['auteur'] .= ', ';
                                        $book['auteur'] .= $this->parseAuthor();
                                        break;
                                case 'dc:date':
                                        $dt = explode('-', $this->parseDate());
                                        
                                        $book['date'] = mktime(0,0,0, (isset($dt[2]) ? $dt[2] : 1), (isset($dt[1]) ? $dt[1] : 1), $dt[0]);
                                        break;
                                case 'dc:identifier':
                                        if (!isset($book['identifier'])) {
                                                $book['identifier'] = array();
                                        }
                                        $dt = $this->parseIdentifier();
                                        $book['identifier'][$dt['type']] = $dt['data'];
                                        break;
                                case 'dc:title':
                                        if (!isset($book['titre']))
                                                $book['titre'] = '';
                                        else
                                                $book['titre'] .= ', ';
                                        $book['titre'] .= $this->parseTitle();
                                        break;
                                case 'dc:publisher':
                                        $book['editeur'] = $this->parsePublisher();
                                        break;
                                case 'link' :
                                        $dt = $this->parseLink();
                                        
                                        if ($dt['name'] == 'thumbnail') {
                                                $book['image'] = $dt['href'];
                                        }
                                        break;
                        }
                }
                
                $this->xpp->read();
                
                return $book;
        }
        
        private function parsePublisher() {
                $this->xpp->read();
                $val = $this->xpp->value;
                $this->xpp->read();
                return $val;
        }
        
        private function parseAuthor() {
                $this->xpp->read();
                $val = $this->xpp->value;
                $this->xpp->read();
                return $val;
        }
 
        private function parseDate() {
                $this->xpp->read();
                $val = $this->xpp->value;
                $this->xpp->read();
                return $val;
        }
        
        private function parseTitle() {
                $this->xpp->read();
                $val = $this->xpp->value;
                $this->xpp->read();
                return $val;
        }
        
        private function parseLink() {
                $dt = array();
                $dt['type'] = $this->xpp->getAttribute('type');
                $dt['name'] = substr($this->xpp->getAttribute('rel'), strrpos($this->xpp->getAttribute('rel'), '/') + 1);
                $dt['href'] = html_entity_decode($this->xpp->getAttribute('href'));
                
                return $dt;
        }
        
        private function parseIdentifier() {
                $this->xpp->read();
                $val = $this->xpp->value;
                $this->xpp->read();
                
                $dt = array();
                if (substr($val,0,5) == 'ISBN:') {
                        $dt['type'] = 'ISBN';
                        $dt['data'] = trim(substr($val,5));
                        
                        if (strlen($dt['data']) == 13)
                                $dt['type'] .= '2';
                } else {
                        $dt['type'] = 'googleid';
                        $dt['data'] = $val;
                }
                
                return $dt;
        }
        
        private function moveToEntry() {
                // Positionne au début des entrées //
                while($this->xpp->name != "entry") {
                        if (!$this->xpp->read())
                                break;
                }
        }
}
?>