From 050788aabb21164d6b46fed2ea7b51b044da4d8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Wed, 24 Feb 2021 17:45:40 +0100 Subject: [PATCH] :sparkles: Add DeviantArt Collector by search --- src/Collector/DeviantartCollector.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Collector/DeviantartCollector.php b/src/Collector/DeviantartCollector.php index b71d725..74263de 100644 --- a/src/Collector/DeviantartCollector.php +++ b/src/Collector/DeviantartCollector.php @@ -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]; }