|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Response; |
|
6
|
|
|
use seregazhuk\PinterestBot\Helpers\Pagination; |
|
7
|
|
|
use seregazhuk\PinterestBot\Helpers\UrlBuilder; |
|
8
|
|
|
use seregazhuk\PinterestBot\Api\Traits\Searchable; |
|
9
|
|
|
use seregazhuk\PinterestBot\Api\Traits\CanBeDeleted; |
|
10
|
|
|
use seregazhuk\PinterestBot\Api\Traits\SendsMessages; |
|
11
|
|
|
use seregazhuk\PinterestBot\Api\Traits\UploadsImages; |
|
12
|
|
|
|
|
13
|
|
|
class Pins extends EntityProvider |
|
14
|
|
|
{ |
|
15
|
|
|
use Searchable, CanBeDeleted, UploadsImages, SendsMessages; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $loginRequiredFor = [ |
|
21
|
|
|
'like', |
|
22
|
|
|
'feed', |
|
23
|
|
|
'send', |
|
24
|
|
|
'copy', |
|
25
|
|
|
'move', |
|
26
|
|
|
'repin', |
|
27
|
|
|
'unLike', |
|
28
|
|
|
'create', |
|
29
|
|
|
'delete', |
|
30
|
|
|
'activity', |
|
31
|
|
|
'visualSimilar', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
protected $searchScope = 'pins'; |
|
35
|
|
|
protected $entityIdName = 'id'; |
|
36
|
|
|
|
|
37
|
|
|
protected $messageEntityName = 'pin'; |
|
38
|
|
|
|
|
39
|
|
|
protected $deleteUrl = UrlBuilder::RESOURCE_DELETE_PIN; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Likes pin with current ID. |
|
43
|
|
|
* @param string $pinId |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function like($pinId) |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->likePinMethodCall($pinId, UrlBuilder::RESOURCE_LIKE_PIN); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Removes your like from pin with current ID. |
|
53
|
|
|
* @param string $pinId |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public function unLike($pinId) |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->likePinMethodCall($pinId, UrlBuilder::RESOURCE_UNLIKE_PIN); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Create a pin. Returns created pin info. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $imageUrl |
|
65
|
|
|
* @param int $boardId |
|
66
|
|
|
* @param string $description |
|
67
|
|
|
* @param string $link |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public function create($imageUrl, $boardId, $description = '', $link = '') |
|
71
|
|
|
{ |
|
72
|
|
|
// Upload image if first argument is not url |
|
73
|
|
|
if (!filter_var($imageUrl, FILTER_VALIDATE_URL)) { |
|
74
|
|
|
$imageUrl = $this->upload($imageUrl); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$requestOptions = [ |
|
78
|
|
|
'method' => 'scraped', |
|
79
|
|
|
'description' => $description, |
|
80
|
|
|
'link' => empty($link) ? $imageUrl : $link, |
|
81
|
|
|
'image_url' => $imageUrl, |
|
82
|
|
|
'board_id' => $boardId, |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
return $this |
|
86
|
|
|
->execPostRequest($requestOptions, UrlBuilder::RESOURCE_CREATE_PIN, true) |
|
87
|
|
|
->getResponseData(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Edit pin by ID. You can move pin to a new board by setting this board id. |
|
92
|
|
|
* |
|
93
|
|
|
* @param int $pindId |
|
94
|
|
|
* @param string $description |
|
95
|
|
|
* @param string $link |
|
96
|
|
|
* @param int|null $boardId |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
public function edit($pindId, $description = '', $link = '', $boardId = null) |
|
100
|
|
|
{ |
|
101
|
|
|
$requestOptions = [ |
|
102
|
|
|
'id' => $pindId, |
|
103
|
|
|
'description' => $description, |
|
104
|
|
|
'link' => $link, |
|
105
|
|
|
'board_id' => $boardId, |
|
106
|
|
|
]; |
|
107
|
|
|
|
|
108
|
|
|
return $this->execPostRequest($requestOptions, UrlBuilder::RESOURCE_UPDATE_PIN); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Moves pin to a new board |
|
113
|
|
|
* |
|
114
|
|
|
* @param int $pinId |
|
115
|
|
|
* @param int $boardId |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
|
|
public function moveToBoard($pinId, $boardId) |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->edit($pinId, '', '', $boardId); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Make a repin. |
|
125
|
|
|
* |
|
126
|
|
|
* @param int $repinId |
|
127
|
|
|
* @param int $boardId |
|
128
|
|
|
* @param string $description |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
|
public function repin($repinId, $boardId, $description = '') |
|
132
|
|
|
{ |
|
133
|
|
|
$requestOptions = [ |
|
134
|
|
|
'board_id' => $boardId, |
|
135
|
|
|
'description' => stripslashes($description), |
|
136
|
|
|
'link' => stripslashes($repinId), |
|
137
|
|
|
'is_video' => null, |
|
138
|
|
|
'pin_id' => $repinId, |
|
139
|
|
|
]; |
|
140
|
|
|
|
|
141
|
|
|
return $this |
|
142
|
|
|
->execPostRequest($requestOptions, UrlBuilder::RESOURCE_REPIN, true) |
|
143
|
|
|
->getResponseData(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get information of a pin by PinID. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $pinId |
|
150
|
|
|
* @return array|bool |
|
151
|
|
|
*/ |
|
152
|
|
|
public function info($pinId) |
|
153
|
|
|
{ |
|
154
|
|
|
$requestOptions = [ |
|
155
|
|
|
'id' => $pinId, |
|
156
|
|
|
'field_set_key' => 'detailed', |
|
157
|
|
|
]; |
|
158
|
|
|
|
|
159
|
|
|
return $this->execGetRequest($requestOptions, UrlBuilder::RESOURCE_PIN_INFO); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get pins from a specific url. For example: https://pinterest.com/source/flickr.com/ will |
|
164
|
|
|
* return recent Pins from flickr.com |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $source |
|
167
|
|
|
* @param int $limit |
|
168
|
|
|
* @return Pagination |
|
169
|
|
|
*/ |
|
170
|
|
|
public function fromSource($source, $limit = Pagination::DEFAULT_LIMIT) |
|
171
|
|
|
{ |
|
172
|
|
|
$data = ['domain' => $source]; |
|
173
|
|
|
|
|
174
|
|
|
return $this->paginate($data, UrlBuilder::RESOURCE_DOMAIN_FEED, $limit); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Get the latest pin activity with pagination. |
|
179
|
|
|
* |
|
180
|
|
|
* @param string $pinId |
|
181
|
|
|
* @param int $limit |
|
182
|
|
|
* @return Pagination|false |
|
183
|
|
|
*/ |
|
184
|
|
|
public function activity($pinId, $limit = Pagination::DEFAULT_LIMIT) |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->getAggregatedActivity($pinId, [], $limit); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Get the pinners who have tied this pin |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $pinId |
|
193
|
|
|
* @param int $limit |
|
194
|
|
|
* @return Pagination|false |
|
195
|
|
|
*/ |
|
196
|
|
|
public function tried($pinId, $limit = Pagination::DEFAULT_LIMIT) |
|
197
|
|
|
{ |
|
198
|
|
|
$data = [ |
|
199
|
|
|
'field_set_key' => 'did_it', |
|
200
|
|
|
'show_did_it_feed' => true, |
|
201
|
|
|
]; |
|
202
|
|
|
|
|
203
|
|
|
return $this->getAggregatedActivity($pinId, $data, $limit); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param string $pinId |
|
208
|
|
|
* @param array $additionalData |
|
209
|
|
|
* @param int $limit |
|
210
|
|
|
* @return bool|Pagination |
|
211
|
|
|
*/ |
|
212
|
|
|
protected function getAggregatedActivity($pinId, $additionalData = [], $limit) |
|
213
|
|
|
{ |
|
214
|
|
|
$aggregatedPinId = $this->getAggregatedPinId($pinId); |
|
215
|
|
|
|
|
216
|
|
|
if (is_null($aggregatedPinId)) return false; |
|
217
|
|
|
|
|
218
|
|
|
$additionalData['aggregated_pin_data_id'] = $aggregatedPinId; |
|
219
|
|
|
|
|
220
|
|
|
return $this->paginate($additionalData, UrlBuilder::RESOURCE_ACTIVITY, $limit); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Get pins from user's feed |
|
225
|
|
|
* |
|
226
|
|
|
* @param int $limit |
|
227
|
|
|
* @return Pagination |
|
228
|
|
|
*/ |
|
229
|
|
|
public function feed($limit = Pagination::DEFAULT_LIMIT) |
|
230
|
|
|
{ |
|
231
|
|
|
return $this->paginate([], UrlBuilder::RESOURCE_USER_FEED, $limit); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @param string $pinId |
|
236
|
|
|
* @param int $limit |
|
237
|
|
|
* @return Pagination |
|
238
|
|
|
*/ |
|
239
|
|
|
public function related($pinId, $limit = Pagination::DEFAULT_LIMIT) |
|
240
|
|
|
{ |
|
241
|
|
|
return $this->paginate(['pin' => $pinId], UrlBuilder::RESOURCE_RELATED_PINS, $limit); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Copy pins to board |
|
246
|
|
|
* |
|
247
|
|
|
* @codeCoverageIgnore |
|
248
|
|
|
* @param array|string $pinIds |
|
249
|
|
|
* @param int $boardId |
|
250
|
|
|
* @return bool|Response |
|
251
|
|
|
*/ |
|
252
|
|
|
public function copy($pinIds, $boardId) |
|
253
|
|
|
{ |
|
254
|
|
|
return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_COPY); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Delete pins from board. |
|
259
|
|
|
* |
|
260
|
|
|
* @codeCoverageIgnore |
|
261
|
|
|
* @param string|array $pinIds |
|
262
|
|
|
* @param int $boardId |
|
263
|
|
|
* @return bool |
|
264
|
|
|
*/ |
|
265
|
|
|
public function deleteFromBoard($pinIds, $boardId) |
|
266
|
|
|
{ |
|
267
|
|
|
return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_DELETE); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @codeCoverageIgnore |
|
272
|
|
|
* Move pins to board |
|
273
|
|
|
* |
|
274
|
|
|
* @param string|array $pinIds |
|
275
|
|
|
* @param int $boardId |
|
276
|
|
|
* @return bool|Response |
|
277
|
|
|
*/ |
|
278
|
|
|
public function move($pinIds, $boardId) |
|
279
|
|
|
{ |
|
280
|
|
|
return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_MOVE); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @param string $pinId |
|
285
|
|
|
* @param array $crop |
|
286
|
|
|
* @return array|bool |
|
287
|
|
|
*/ |
|
288
|
|
|
public function visualSimilar($pinId, array $crop = []) |
|
289
|
|
|
{ |
|
290
|
|
|
$data = [ |
|
291
|
|
|
'pin_id' => $pinId, |
|
292
|
|
|
'crop' => $crop ? : [ |
|
293
|
|
|
"x" => 0.16, |
|
294
|
|
|
"y" => 0.16, |
|
295
|
|
|
"w" => 0.66, |
|
296
|
|
|
"h" => 0.66, |
|
297
|
|
|
"num_crop_actions" => 1 |
|
298
|
|
|
], |
|
299
|
|
|
'force_refresh' => true, |
|
300
|
|
|
'keep_duplicates' => false |
|
301
|
|
|
]; |
|
302
|
|
|
|
|
303
|
|
|
return $this->execGetRequest($data, UrlBuilder::RESOURCE_VISUAL_SIMILAR_PINS); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Saves the pin original image to the specified path. On success |
|
308
|
|
|
* returns full path to saved image. Otherwise returns false. |
|
309
|
|
|
* |
|
310
|
|
|
* @param int $pinId |
|
311
|
|
|
* @param string $path |
|
312
|
|
|
* @return false|string |
|
313
|
|
|
*/ |
|
314
|
|
|
public function saveOriginalImage($pinId, $path) |
|
315
|
|
|
{ |
|
316
|
|
|
$pinInfo = $this->info($pinId); |
|
317
|
|
|
if(!isset($pinInfo['images']['orig']['url'])) return false; |
|
318
|
|
|
|
|
319
|
|
|
$originalUrl = $pinInfo['images']['orig']['url']; |
|
320
|
|
|
$destination = $path . DIRECTORY_SEPARATOR . basename($originalUrl); |
|
321
|
|
|
|
|
322
|
|
|
file_put_contents($destination, file_get_contents($originalUrl)); |
|
323
|
|
|
|
|
324
|
|
|
return $destination; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* @param string $query |
|
329
|
|
|
* @param int $limit |
|
330
|
|
|
* @return Pagination |
|
331
|
|
|
*/ |
|
332
|
|
View Code Duplication |
public function searchInMyPins($query, $limit = Pagination::DEFAULT_LIMIT) |
|
|
|
|
|
|
333
|
|
|
{ |
|
334
|
|
|
return (new Pagination($limit)) |
|
335
|
|
|
->paginateOver(function($bookmarks = []) use ($query) { |
|
336
|
|
|
return $this->execSearchRequest($query, 'my_pins', $bookmarks); |
|
337
|
|
|
}); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Calls Pinterest API to like or unlike Pin by ID. |
|
342
|
|
|
* |
|
343
|
|
|
* @param string $pinId |
|
344
|
|
|
* @param string $resourceUrl |
|
345
|
|
|
* @return bool |
|
346
|
|
|
*/ |
|
347
|
|
|
protected function likePinMethodCall($pinId, $resourceUrl) |
|
348
|
|
|
{ |
|
349
|
|
|
return $this->execPostRequest(['pin_id' => $pinId], $resourceUrl); |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* @param string $pinId |
|
354
|
|
|
* @return int|null |
|
355
|
|
|
*/ |
|
356
|
|
|
protected function getAggregatedPinId($pinId) |
|
357
|
|
|
{ |
|
358
|
|
|
$pinInfo = $this->info($pinId); |
|
359
|
|
|
|
|
360
|
|
|
return isset($pinInfo['aggregated_pin_data']['id']) ? |
|
361
|
|
|
$pinInfo['aggregated_pin_data']['id'] : |
|
362
|
|
|
null; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* @param mixed $params |
|
367
|
|
|
* @return array |
|
368
|
|
|
*/ |
|
369
|
|
|
protected function getFeedRequestData($params = []) |
|
370
|
|
|
{ |
|
371
|
|
|
return ['domain' => $params['source']]; |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* @param string|array $pinIds |
|
376
|
|
|
* @param int $boardId |
|
377
|
|
|
* @param string $editUrl |
|
378
|
|
|
* @return bool |
|
379
|
|
|
*/ |
|
380
|
|
|
protected function bulkEdit($pinIds, $boardId, $editUrl) |
|
381
|
|
|
{ |
|
382
|
|
|
$pinIds = is_array($pinIds) ? $pinIds : [$pinIds]; |
|
383
|
|
|
|
|
384
|
|
|
$data = [ |
|
385
|
|
|
'board_id' => (string)$boardId, |
|
386
|
|
|
'pin_ids' => $pinIds, |
|
387
|
|
|
]; |
|
388
|
|
|
|
|
389
|
|
|
return $this->execPostRequest($data, $editUrl); |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.