From b6943de0cab856745fa9dc27b03c24fe3e07009f Mon Sep 17 00:00:00 2001 From: DnAp Date: Sat, 23 Mar 2019 17:40:19 +0200 Subject: [PATCH] [BingSearch] Add new bridge (#1046) --- bridges/BingSearchBridge.php | 119 +++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 bridges/BingSearchBridge.php diff --git a/bridges/BingSearchBridge.php b/bridges/BingSearchBridge.php new file mode 100644 index 00000000..ccfffdf9 --- /dev/null +++ b/bridges/BingSearchBridge.php @@ -0,0 +1,119 @@ + array( + 'category' => array( + 'name' => 'Categories', + 'type' => 'list', + 'values' => self::IMAGE_DISCOVER_CATEGORIES + ), + 'image_size' => array( + 'name' => 'Image size', + 'type' => 'list', + 'values' => array( + 'Small' => 'turl', + 'Full size' => 'imgurl' + ) + ) + ) + ); + + const IMAGE_DISCOVER_CATEGORIES = array( + 'Abstract' => 'abstract', + 'Animals' => 'animals', + 'Anime' => 'anime', + 'Architecture' => 'architecture', + 'Arts and Crafts' => 'arts-and-crafts', + 'Beauty' => 'beauty', + 'Cars and Motorcycles' => 'cars-and-motorcycles', + 'Cats' => 'cats', + 'Celebrities' => 'celebrities', + 'Comics' => 'comics', + 'DIY' => 'diy', + 'Dogs' => 'dogs', + 'Fitness' => 'fitness', + 'Food and Drink' => 'food-and-drink', + 'Funny' => 'funny', + 'Gadgets' => 'gadgets', + 'Gardening' => 'gardening', + 'Geeky' => 'geeky', + 'Hairstyles' => 'hairstyles', + 'Home Decor' => 'home-decor', + 'Marine Life' => 'marine-life', + 'Men\'s Fashion' => 'men%27s-fashion', + 'Nature' => 'nature', + 'Outdoors' => 'outdoors', + 'Parenting' => 'parenting', + 'Phone Wallpapers' => 'phone-wallpapers', + 'Photography' => 'photography', + 'Quotes' => 'quotes', + 'Recipes' => 'recipes', + 'Snow' => 'snow', + 'Tattoos' => 'tattoos', + 'Travel' => 'travel', + 'Video Games' => 'video-games', + 'Weddings' => 'weddings', + 'Women\'s Fashion' => 'women%27s-fashion', + ); + + public function getIcon() + { + return 'https://www.bing.com/sa/simg/bing_p_rr_teal_min.ico'; + } + + public function collectData() + { + $this->items = $this->imageDiscover($this->getInput('category')); + } + + public function getName() + { + if ($this->getInput('category')) { + if (isset(self::IMAGE_DISCOVER_CATEGORIES[$this->getInput('categories')])) { + $category = self::IMAGE_DISCOVER_CATEGORIES[$this->getInput('categories')]; + } else { + $category = 'Unknown'; + } + + return 'Best ' . $category . ' - Bing Image Discover'; + } + return parent::getName(); + } + + private function imageDiscover($category) + { + $html = getSimpleHTMLDOM(self::URI . '/discover/' . $category) + or returnServerError('Could not request ' . self::NAME); + $sizeKey = $this->getInput('image_size'); + + $items = []; + foreach ($html->find('a.iusc') as $element) { + $data = json_decode(htmlspecialchars_decode($element->getAttribute('m')), true); + + $item = array(); + $item['title'] = basename(rtrim($data['imgurl'], '/')); + $item['uri'] = $data['imgurl']; + $item['content'] = ' + ' . $item['title'] . ' +

Source:

'; + $item['enclosures'] = $data['imgurl']; + + $items[] = $item; + } + return $items; + } + + private function curUrl($url) + { + if (strlen($url) <= 80) { + return $url; + } + return substr($url, 0, 80) . '...'; + } +}