|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Hummingbird Anime Client |
|
4
|
|
|
* |
|
5
|
|
|
* An API client for Hummingbird to manage anime and manga watch lists |
|
6
|
|
|
* |
|
7
|
|
|
* @package HummingbirdAnimeClient |
|
8
|
|
|
* @author Timothy J. Warren |
|
9
|
|
|
* @copyright Copyright (c) 2015 - 2016 |
|
10
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient |
|
11
|
|
|
* @license MIT |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace Aviat\AnimeClient\Model; |
|
14
|
|
|
|
|
15
|
|
|
use GuzzleHttp\Client; |
|
16
|
|
|
use GuzzleHttp\Cookie\CookieJar; |
|
17
|
|
|
use GuzzleHttp\Psr7\Request; |
|
18
|
|
|
use GuzzleHttp\Psr7\ResponseInterface; |
|
19
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
20
|
|
|
|
|
21
|
|
|
use Aviat\Ion\Di\ContainerInterface; |
|
22
|
|
|
use Aviat\AnimeClient\Model as BaseModel; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Base model for api interaction |
|
26
|
|
|
* |
|
27
|
|
|
* @method ResponseInterface get(string $uri, array $options); |
|
28
|
|
|
* @method ResponseInterface delete(string $uri, array $options); |
|
29
|
|
|
* @method ResponseInterface head(string $uri, array $options); |
|
30
|
|
|
* @method ResponseInterface options(string $uri, array $options); |
|
31
|
|
|
* @method ResponseInterface patch(string $uri, array $options); |
|
32
|
|
|
* @method ResponseInterface post(string $uri, array $options); |
|
33
|
|
|
* @method ResponseInterface put(string $uri, array $options); |
|
34
|
|
|
*/ |
|
35
|
|
|
class API extends BaseModel { |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Base url for making api requests |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $base_url = ''; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The Guzzle http client object |
|
45
|
|
|
* @var object |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $client; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Cookie jar object for api requests |
|
51
|
|
|
* @var object |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $cookieJar; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Constructor |
|
57
|
|
|
* |
|
58
|
|
|
* @param ContainerInterface $container |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct(ContainerInterface $container) |
|
61
|
|
|
{ |
|
62
|
|
|
parent::__construct($container); |
|
63
|
|
|
$this->init(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Set up the class properties |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function init() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->cookieJar = new CookieJar(); |
|
74
|
|
|
$this->client = new Client([ |
|
75
|
|
|
'base_uri' => $this->base_url, |
|
76
|
|
|
'cookies' => TRUE, |
|
77
|
|
|
'http_errors' => FALSE, |
|
78
|
|
|
'defaults' => [ |
|
79
|
|
|
'cookies' => $this->cookieJar, |
|
80
|
|
|
'headers' => [ |
|
81
|
|
|
'User-Agent' => "Tim's Anime Client/2.0", |
|
82
|
|
|
'Accept-Encoding' => 'application/json' |
|
83
|
|
|
], |
|
84
|
|
|
'timeout' => 25, |
|
85
|
|
|
'connect_timeout' => 25 |
|
86
|
|
|
] |
|
87
|
|
|
]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Magic methods to call guzzle api client |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $method |
|
94
|
|
|
* @param array $args |
|
95
|
|
|
* @return ResponseInterface|null |
|
96
|
|
|
*/ |
|
97
|
|
|
public function __call($method, $args) |
|
98
|
|
|
{ |
|
99
|
|
|
$valid_methods = [ |
|
100
|
|
|
'get', |
|
101
|
|
|
'delete', |
|
102
|
|
|
'head', |
|
103
|
|
|
'options', |
|
104
|
|
|
'patch', |
|
105
|
|
|
'post', |
|
106
|
|
|
'put' |
|
107
|
|
|
]; |
|
108
|
|
|
|
|
109
|
|
|
if ( ! in_array($method, $valid_methods)) |
|
110
|
|
|
{ |
|
111
|
|
|
return NULL; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
array_unshift($args, strtoupper($method)); |
|
115
|
|
|
return call_user_func_array([$this->client, 'request'], $args); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the data for the specified library entry |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $id |
|
122
|
|
|
* @param string $status |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
|
|
public function get_library_item($id, $status) |
|
126
|
|
|
{ |
|
127
|
|
|
$data = $this->_get_list_from_api($status); |
|
128
|
|
|
$index_array = array_column($data, 'id'); |
|
129
|
|
|
|
|
130
|
|
|
$key = array_search($id, $index_array); |
|
131
|
|
|
|
|
132
|
|
|
return $key !== FALSE |
|
133
|
|
|
? $data[$key] |
|
134
|
|
|
: []; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Sort the manga entries by their title |
|
139
|
|
|
* |
|
140
|
|
|
* @codeCoverageIgnore |
|
141
|
|
|
* @param array $array |
|
142
|
|
|
* @param string $sort_key |
|
143
|
|
|
* @return void |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function sort_by_name(&$array, $sort_key) |
|
146
|
|
|
{ |
|
147
|
|
|
$sort = array(); |
|
148
|
|
|
|
|
149
|
|
|
foreach ($array as $key => $item) |
|
150
|
|
|
{ |
|
151
|
|
|
$sort[$key] = $item[$sort_key]['title']; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
array_multisort($sort, SORT_ASC, $array); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Attempt login via the api |
|
159
|
|
|
* |
|
160
|
|
|
* @codeCoverageIgnore |
|
161
|
|
|
* @param string $username |
|
162
|
|
|
* @param string $password |
|
163
|
|
|
* @return string|false |
|
164
|
|
|
*/ |
|
165
|
|
|
public function authenticate($username, $password) |
|
166
|
|
|
{ |
|
167
|
|
|
$response = $this->post('https://hummingbird.me/api/v1/users/authenticate', [ |
|
168
|
|
|
'form_params' => [ |
|
169
|
|
|
'username' => $username, |
|
170
|
|
|
'password' => $password |
|
171
|
|
|
] |
|
172
|
|
|
]); |
|
173
|
|
|
|
|
174
|
|
|
if ($response->getStatusCode() === 201) |
|
175
|
|
|
{ |
|
176
|
|
|
return json_decode($response->getBody(), TRUE); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return FALSE; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Dummy function that should be abstract. Is not abstract because |
|
184
|
|
|
* this class is used concretely for authorizing API calls |
|
185
|
|
|
* |
|
186
|
|
|
* @TODO Refactor, and make this abstract |
|
187
|
|
|
* @param string $status |
|
188
|
|
|
* @return array |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function _get_list_from_api($status) |
|
191
|
|
|
{ |
|
192
|
|
|
return []; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
// End of BaseApiModel.php |