Add DeviantArt Collector by search

This commit is contained in:
Clément 2021-02-24 17:45:40 +01:00
parent 8fa8302792
commit 050788aabb
1 changed files with 9 additions and 2 deletions

View File

@ -11,6 +11,8 @@ class DeviantartCollector extends Collector
*
*/
public const RANDOM_URL = 'https://www.deviantart.com/popular/deviations';
public const SEARCH_URL = 'https://www.deviantart.com/search?q=%s';
/**
* Regular expression to extract the maximum deviantionID from homepage
*/
@ -31,7 +33,12 @@ class DeviantartCollector extends Collector
*/
public function getRandomImages(int $number = 1)
{
$html = file_get_contents(self::RANDOM_URL);
$word_to_search = $this->getConfig()->get('collector.keywords.keywords', false);
if (false !== $word_to_search) {
$html = file_get_contents(sprintf(self::SEARCH_URL, urlencode($word_to_search)));
} else {
$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);
@ -40,7 +47,7 @@ class DeviantartCollector extends Collector
}, $deviant_ids[1]);
$deviant_ids = array_unique($deviant_ids);
$index_to_download = array_rand($deviant_ids, $number);
$index_to_download = array_rand($deviant_ids, min($number, count($deviant_ids)));
if (!is_array($index_to_download)) {
$index_to_download = [$index_to_download];
}