✨ Add DeviantArt Collector by search
This commit is contained in:
		| @@ -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]; | ||||
|         } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user