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