Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9fa3a8da77 |
@@ -12,7 +12,8 @@
|
||||
"fabpot/goutte": "^4.0",
|
||||
"ext-imagick": "*",
|
||||
"samwilson/phpflickr": "^4.14",
|
||||
"ext-curl": "*"
|
||||
"ext-curl": "*",
|
||||
"ext-json": "*"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
||||
65
src/Collector/CaraCollector.php
Normal file
65
src/Collector/CaraCollector.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\WebGobbler\Collector;
|
||||
|
||||
use Shikiryu\WebGobbler\Collector;
|
||||
|
||||
class CaraCollector extends Collector
|
||||
{
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'collector_cara';
|
||||
}
|
||||
|
||||
public function getRandomImage()
|
||||
{
|
||||
return $this->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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user