Completed
Pull Request — master (#262)
by Sergey
02:36
created

Pins::explore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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