🎉 Hello world
This commit is contained in:
65
src/Collector/DeviantartCollector.php
Normal file
65
src/Collector/DeviantartCollector.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\WebGobbler\Collector;
|
||||
|
||||
use Goutte\Client;
|
||||
use Shikiryu\WebGobbler\Collector;
|
||||
|
||||
class DeviantartCollector extends Collector
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public const RANDOM_URL = 'https://www.deviantart.com/popular/deviations';
|
||||
/**
|
||||
* Regular expression to extract the maximum deviantionID from homepage
|
||||
*/
|
||||
public const RE_ALLDEVIATIONID = '/href="https:\/\/www.deviantart.com\/[^\/]+\/art\/([^"]+)"/m';
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getRandomImage()
|
||||
{
|
||||
return $this->getRandomImages(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $number
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getRandomImages(int $number = 1)
|
||||
{
|
||||
$html = file_get_contents(self::RANDOM_URL);
|
||||
preg_match_all(self::RE_ALLDEVIATIONID, $html, $deviant_ids);
|
||||
$deviant_ids = array_map(static function ($deviant_id) {
|
||||
$array = explode('-', $deviant_id);
|
||||
|
||||
return (int)end($array);
|
||||
}, $deviant_ids[1]);
|
||||
$deviant_ids = array_unique($deviant_ids);
|
||||
|
||||
$index_to_download = array_rand($deviant_ids, $number);
|
||||
if (!is_array($index_to_download)) {
|
||||
$index_to_download = [$index_to_download];
|
||||
}
|
||||
|
||||
foreach ($index_to_download as $deviant_id) {
|
||||
$client = new Client();
|
||||
$crawler = $client->request('GET', 'https://www.deviantart.com/deviation/'.$deviant_ids[$deviant_id]);
|
||||
$img_url = $crawler->filter('[data-hook="art_stage"] img')->eq(0)->attr('src');
|
||||
file_put_contents($this->getPoolDirectory() . '/' . basename(parse_url($img_url, PHP_URL_PATH)), file_get_contents($img_url));
|
||||
}
|
||||
|
||||
return $number; // Fixme
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'collector_deviantart';
|
||||
}
|
||||
}
|
43
src/Collector/LocalCollector.php
Normal file
43
src/Collector/LocalCollector.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\WebGobbler\Collector;
|
||||
|
||||
use Shikiryu\WebGobbler\Collector;
|
||||
|
||||
class LocalCollector extends Collector
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $directory_to_scan = '';
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
public $filepaths = [];
|
||||
|
||||
/**
|
||||
* @return int|void
|
||||
*/
|
||||
public function getRandomImage()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'collector_local';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $number
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getRandomImages(int $number)
|
||||
{
|
||||
// TODO: Implement getRandomImages() method.
|
||||
}
|
||||
}
|
61
src/Collector/YahooImageCollector.php
Normal file
61
src/Collector/YahooImageCollector.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\WebGobbler\Collector;
|
||||
|
||||
use Goutte\Client;
|
||||
use Shikiryu\WebGobbler\Collector;
|
||||
|
||||
class YahooImageCollector extends Collector
|
||||
{
|
||||
public const SEARCH_URL = 'https://images.search.yahoo.com/search/images?p=%s';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'collector_yahooimage';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getRandomImage()
|
||||
{
|
||||
$word_to_search = $this->generateRandomWord();
|
||||
|
||||
$client = new Client();
|
||||
$crawler = $client->request('GET', sprintf(self::SEARCH_URL, $word_to_search));
|
||||
$imgs = $crawler->filter('noscript img');
|
||||
if ($imgs->count() < 20) {
|
||||
return $this->getRandomImage();
|
||||
}
|
||||
|
||||
$img_url = $imgs->eq(random_int(0, $imgs->count() - 1))->attr('src');
|
||||
$parsed_url = parse_url( $img_url );
|
||||
parse_str( $parsed_url['query'] , $url_vars );
|
||||
$name = $url_vars['id'];
|
||||
unset($url_vars['w'], $url_vars['h']);
|
||||
$parsed_url['query'] = $url_vars;
|
||||
$img_url = $this->reverse_url($parsed_url);
|
||||
|
||||
file_put_contents($this->getPoolDirectory() . '/'. $name . '.jpg', file_get_contents($img_url));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $number
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getRandomImages(int $number)
|
||||
{
|
||||
$count = 0;
|
||||
for ($i = 0; $i < $number; $i++) {
|
||||
$count += $this->getRandomImage();
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user