seregazhuk /
php-pinterest-bot
| 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 | |||
| 42 | protected $entityIdName = 'id'; |
||
| 43 | |||
| 44 | protected $messageEntityName = 'pin'; |
||
| 45 | |||
| 46 | protected $deleteUrl = UrlBuilder::RESOURCE_DELETE_PIN; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Create a pin. Returns created pin info. |
||
| 50 | * |
||
| 51 | * @param string $imageUrl |
||
| 52 | * @param int $boardId |
||
| 53 | * @param string $description |
||
| 54 | * @param string $link |
||
| 55 | * @param string $title |
||
| 56 | * @return array |
||
| 57 | */ |
||
| 58 | public function create($imageUrl, $boardId, $description = '', $link = '', $title = '', $sectionId = null) |
||
| 59 | { |
||
| 60 | // Upload image if first argument is a local file |
||
| 61 | if (file_exists($imageUrl)) { |
||
| 62 | $imageUrl = $this->upload($imageUrl); |
||
| 63 | } |
||
| 64 | |||
| 65 | $requestOptions = [ |
||
| 66 | 'method' => 'scraped', |
||
| 67 | 'description' => $description, |
||
| 68 | 'link' => $link, |
||
| 69 | 'image_url' => $imageUrl, |
||
| 70 | 'board_id' => $boardId, |
||
| 71 | 'title' => $title, |
||
| 72 | ]; |
||
| 73 | |||
| 74 | if ($sectionId !== null) { |
||
| 75 | $requestOptions['section'] = $sectionId; |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->post(UrlBuilder::RESOURCE_CREATE_PIN, $requestOptions); |
||
| 79 | |||
| 80 | return $this->response->getResponseData(); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Edit pin by ID. You can move pin to a new board by setting this board id. |
||
| 85 | * |
||
| 86 | * @param int $pindId |
||
| 87 | * @param string $description |
||
| 88 | * @param string $link |
||
| 89 | * @param int|null $boardId |
||
| 90 | * @param string $title |
||
| 91 | * @param int|null $sectionId |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | |||
| 95 | public function edit($pindId, $description = '', $link = '', $boardId = null, $title = '', $sectionId = null) |
||
| 96 | { |
||
| 97 | $requestOptions = ['id' => $pindId]; |
||
| 98 | |||
| 99 | if (!empty($description)) { |
||
| 100 | $requestOptions['description'] = $description; |
||
| 101 | } |
||
| 102 | |||
| 103 | if (!empty($link)) { |
||
| 104 | $requestOptions['link'] = stripslashes($link); |
||
| 105 | } |
||
| 106 | |||
| 107 | if ($boardId !== null) { |
||
| 108 | $requestOptions['board_id'] = $boardId; |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | if (!empty($title)) { |
||
| 113 | $requestOptions['title'] = $title; |
||
| 114 | } |
||
| 115 | |||
| 116 | if ($sectionId !== null) { |
||
| 117 | $requestOptions['board_section_id'] = $sectionId; |
||
| 118 | } |
||
| 119 | |||
| 120 | return $this->post(UrlBuilder::RESOURCE_UPDATE_PIN, $requestOptions); |
||
|
0 ignored issues
–
show
|
|||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Moves pin to a new board |
||
| 125 | * |
||
| 126 | * @param int $pinId |
||
| 127 | * @param int $boardId |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | public function moveToBoard($pinId, $boardId) |
||
| 131 | { |
||
| 132 | return $this->edit($pinId, '', '', $boardId); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Make a repin. |
||
| 137 | * |
||
| 138 | * @param int $repinId |
||
| 139 | * @param int $boardId |
||
| 140 | * @param string $description |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | public function repin($repinId, $boardId, $description = '', $sectionId = null) |
||
| 144 | { |
||
| 145 | $requestOptions = [ |
||
| 146 | 'board_id' => $boardId, |
||
| 147 | 'description' => stripslashes($description), |
||
| 148 | 'link' => '', |
||
| 149 | 'is_video' => null, |
||
| 150 | 'pin_id' => $repinId, |
||
| 151 | ]; |
||
| 152 | |||
| 153 | if ($sectionId !== null) { |
||
| 154 | $requestOptions['section'] = $sectionId; |
||
| 155 | } |
||
| 156 | |||
| 157 | $this->post(UrlBuilder::RESOURCE_REPIN, $requestOptions); |
||
| 158 | |||
| 159 | return $this->response->getResponseData(); |
||
|
0 ignored issues
–
show
|
|||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get information of a pin by PinID. |
||
| 164 | * |
||
| 165 | * @param string $pinId |
||
| 166 | * @return array|bool |
||
| 167 | */ |
||
| 168 | public function info($pinId) |
||
| 169 | { |
||
| 170 | $requestOptions = [ |
||
| 171 | 'id' => $pinId, |
||
| 172 | 'field_set_key' => 'detailed', |
||
| 173 | ]; |
||
| 174 | |||
| 175 | return $this->get(UrlBuilder::RESOURCE_PIN_INFO, $requestOptions); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get pins from a specific url. For example: https://pinterest.com/source/flickr.com/ will |
||
| 180 | * return recent Pins from flickr.com |
||
| 181 | * |
||
| 182 | * @param string $source |
||
| 183 | * @param int $limit |
||
| 184 | * @return Pagination |
||
| 185 | */ |
||
| 186 | public function fromSource($source, $limit = Pagination::DEFAULT_LIMIT) |
||
| 187 | { |
||
| 188 | $data = ['domain' => $source]; |
||
| 189 | |||
| 190 | return $this->paginate(UrlBuilder::RESOURCE_DOMAIN_FEED, $data, $limit); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get the latest pin activity with pagination. |
||
| 195 | * |
||
| 196 | * @param string $pinId |
||
| 197 | * @param int $limit |
||
| 198 | * @return Pagination |
||
| 199 | */ |
||
| 200 | public function activity($pinId, $limit = Pagination::DEFAULT_LIMIT) |
||
| 201 | { |
||
| 202 | return $this->getAggregatedActivity($pinId, [], $limit); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param string $pinId |
||
| 207 | * @param array $additionalData |
||
| 208 | * @param int $limit |
||
| 209 | * @return Pagination |
||
| 210 | */ |
||
| 211 | protected function getAggregatedActivity($pinId, $additionalData = [], $limit) |
||
| 212 | { |
||
| 213 | $aggregatedPinId = $this->getAggregatedPinId($pinId); |
||
| 214 | |||
| 215 | if ($aggregatedPinId === null) { |
||
| 216 | return new Pagination(); |
||
| 217 | } |
||
| 218 | |||
| 219 | $additionalData['aggregated_pin_data_id'] = $aggregatedPinId; |
||
| 220 | |||
| 221 | return $this->paginate(UrlBuilder::RESOURCE_ACTIVITY, $additionalData, $limit); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get pins from user's feed |
||
| 226 | * |
||
| 227 | * @param int $limit |
||
| 228 | * @return Pagination |
||
| 229 | */ |
||
| 230 | public function feed($limit = Pagination::DEFAULT_LIMIT) |
||
| 231 | { |
||
| 232 | return $this->paginate(UrlBuilder::RESOURCE_USER_FEED, [], $limit); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string $pinId |
||
| 237 | * @param int $limit |
||
| 238 | * @return Pagination |
||
| 239 | */ |
||
| 240 | public function related($pinId, $limit = Pagination::DEFAULT_LIMIT) |
||
| 241 | { |
||
| 242 | $requestData = [ |
||
| 243 | 'pin' => $pinId, |
||
| 244 | 'add_vase' => true, |
||
| 245 | ]; |
||
| 246 | |||
| 247 | return $this->paginate(UrlBuilder::RESOURCE_RELATED_PINS, $requestData, $limit); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Copy pins to board |
||
| 252 | * |
||
| 253 | * @param array|string $pinIds |
||
| 254 | * @param int $boardId |
||
| 255 | * @return bool|Response |
||
| 256 | */ |
||
| 257 | public function copy($pinIds, $boardId) |
||
| 258 | { |
||
| 259 | return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_COPY); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Delete pins from board. |
||
| 264 | * |
||
| 265 | * @param string|array $pinIds |
||
| 266 | * @param int $boardId |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | public function deleteFromBoard($pinIds, $boardId) |
||
| 270 | { |
||
| 271 | return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_DELETE); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Move pins to board |
||
| 276 | * |
||
| 277 | * @param string|array $pinIds |
||
| 278 | * @param int $boardId |
||
| 279 | * @return bool|Response |
||
| 280 | */ |
||
| 281 | public function move($pinIds, $boardId) |
||
| 282 | { |
||
| 283 | return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_MOVE); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $pinId |
||
| 288 | * @param int $limit |
||
| 289 | * @return Pagination |
||
| 290 | */ |
||
| 291 | public function visualSimilar($pinId, $limit = Pagination::DEFAULT_LIMIT) |
||
| 292 | { |
||
| 293 | $data = [ |
||
| 294 | 'pin_id' => $pinId, |
||
| 295 | // Some magic numbers, I have no idea about them |
||
| 296 | '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->paginate(UrlBuilder::RESOURCE_VISUAL_SIMILAR_PINS, $data, $limit); |
||
| 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'])) { |
||
| 322 | return false; |
||
| 323 | } |
||
| 324 | |||
| 325 | $originalUrl = $pinInfo['images']['orig']['url']; |
||
| 326 | $destination = $path . DIRECTORY_SEPARATOR . basename($originalUrl); |
||
| 327 | |||
| 328 | FileHelper::saveTo($originalUrl, $destination); |
||
| 329 | |||
| 330 | return $destination; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $query |
||
| 335 | * @param int $limit |
||
| 336 | * @return Pagination |
||
| 337 | */ |
||
| 338 | public function searchInMyPins($query, $limit = Pagination::DEFAULT_LIMIT) |
||
| 339 | { |
||
| 340 | return $this->paginateCustom( |
||
| 341 | function () use ($query) { |
||
| 342 | return $this->execSearchRequest($query, 'my_pins'); |
||
| 343 | } |
||
| 344 | )->take($limit); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns trending pins from http://pinterest.com/discover page. Uses topic id, that can be received |
||
| 349 | * from $bot->topics->explore() method. |
||
| 350 | * |
||
| 351 | * @param string $topicId |
||
| 352 | * @param int $limit |
||
| 353 | * @return Pagination |
||
| 354 | */ |
||
| 355 | public function explore($topicId, $limit = Pagination::DEFAULT_LIMIT) |
||
| 356 | { |
||
| 357 | $data = [ |
||
| 358 | 'aux_fields' => [], |
||
| 359 | 'prepend' => false, |
||
| 360 | 'offset' => 180, |
||
| 361 | 'section_id' => $topicId, |
||
| 362 | ]; |
||
| 363 | |||
| 364 | return $this->paginate(UrlBuilder::RESOURCE_EXPLORE_PINS, $data, $limit); |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get pin analytics, like numbers of clicks, views and repins |
||
| 369 | * @param $pinId |
||
| 370 | * @return array|bool|Response |
||
| 371 | */ |
||
| 372 | public function analytics($pinId) |
||
| 373 | { |
||
| 374 | // Pinterest requires pinId to be a string |
||
| 375 | $pinId = (string)$pinId; |
||
| 376 | |||
| 377 | return $this->get(UrlBuilder::RESOURCE_PIN_ANALYTICS, ['pin_id' => $pinId]); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @param string $pinId |
||
| 382 | * @return int|null |
||
| 383 | */ |
||
| 384 | protected function getAggregatedPinId($pinId) |
||
| 385 | { |
||
| 386 | $pinInfo = $this->info($pinId); |
||
| 387 | |||
| 388 | return $pinInfo['aggregated_pin_data']['id'] ?? null; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string|array $pinIds |
||
| 393 | * @param int $boardId |
||
| 394 | * @param string $editUrl |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | protected function bulkEdit($pinIds, $boardId, $editUrl) |
||
| 398 | { |
||
| 399 | $data = [ |
||
| 400 | 'board_id' => $boardId, |
||
| 401 | 'pin_ids' => (array)$pinIds, |
||
| 402 | ]; |
||
| 403 | |||
| 404 | return $this->post($editUrl, $data); |
||
|
0 ignored issues
–
show
|
|||
| 405 | } |
||
| 406 | } |
||
| 407 |