-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathBookServices.php
68 lines (58 loc) · 1.72 KB
/
BookServices.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
<?php
namespace App\Services;
use GuzzleHttp\Client;
use Vision\Feature;
use Vision\Request\Image\LocalImage;
use Vision\Vision;
class BookServices
{
/**
* Fetch book details from $isbn.
*
* @param string $isbn
*
* @return mixed
*/
public static function getBookDetails($isbn)
{
$client = new Client();
$res = $client->request('GET', 'https://www.googleapis.com/books/v1/volumes?q=isbn:' . $isbn, [
'timeout' => 5.0,
]);
$book = json_decode($res->getBody(), true);
if (! isset($book['items'])) {
$res = $client->request('GET', 'https://www.googleapis.com/books/v1/volumes?q=ISBN:' . $isbn, [
'timeout' => 5.0,
]);
$book = json_decode($res->getBody(), true);
}
if (! isset($book['items'])) {
return 'please try again';
}
return $book;
}
/**
* Fetch ISBN form book image.
*
* @param mixed $file
*
* @return string
*/
public static function getISBN($file)
{
$apiKey = config('constants.google.vision-api-key');
$vision = new Vision($apiKey, [new Feature(Feature::TEXT_DETECTION, 100)]);
$response = $vision->request(new LocalImage($file->path()));
$faces = $response->getTextAnnotations();
$description = '';
$currentText = '';
foreach ($faces as $face) {
$faceDescription = $face->getDescription();
if (in_array(strtolower($currentText), ['isbn', 'sbn'])) {
$description = $faceDescription;
}
$currentText = $faceDescription;
}
return str_replace('-', '', trim($description));
}
}