diff --git a/composer.json b/composer.json index b0eac6e..3e14f94 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "fabpot/goutte": "^4.0", "ext-imagick": "*", "samwilson/phpflickr": "^4.14", - "ext-curl": "*" + "ext-curl": "*", + "ext-json": "*" }, "autoload": { "psr-4": { diff --git a/src/Collector/CaraCollector.php b/src/Collector/CaraCollector.php new file mode 100644 index 0000000..fda5b80 --- /dev/null +++ b/src/Collector/CaraCollector.php @@ -0,0 +1,65 @@ +getRandomImages(1); + } + + public function getRandomImages(int $number) + { + $curl = curl_init(); + + curl_setopt_array($curl, [ + CURLOPT_URL => 'https://cara.app/api/explore/latestImages', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "GET", + CURLOPT_HTTPHEADER => [ + "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:147.0) Gecko/20100101 Firefox/147.0", + ], + CURLOPT_COOKIEJAR => [ + + ] + ]); + + $response = curl_exec($curl); + + curl_close($curl); + try { + $response = json_decode($response, true, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $e) { + return 0; + } + + $images = array_map(static function($image) { + return sprintf('https://images.cara.app/%s', $image['photo']); + }, $response); + + $count = 0; + + foreach ($images as $image) { + if ($count >= $number) { + break; + } + file_put_contents($this->getPoolDirectory() . '/' . basename(parse_url($image, PHP_URL_PATH)), file_get_contents($image)); + } + + return $count; + } +} \ No newline at end of file