def create_thumbnail(self) -> tuple: url = self._url thumbnail_path = None try: response = requests.get(url) except requests.exceptions.ConnectionError: raise InvalidURLException('url is not valid') if response.status_code > 300: raise InvalidURLException('url is not valid') og_handler = OgImageHandler(response) favicon_handler = FaviconHandler(response) name = self.__get_title(response) if og_handler.has_og_image_meta(): thumbnail_path = og_handler.get_url() elif favicon_handler.set_favicon_image_link_tag( ) or favicon_handler.set_image_meta_tag(): thumbnail_path = favicon_handler.get_url() else: return None, name thumbnail_path = self.__pad_host(thumbnail_path) return thumbnail_path, name
def test_has_itemprop_meta_tag(self): self.mock_res.text = """ <html> <head> <meta itemprop="image" content="testmeta"/> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_image_meta_tag() self.assertTrue(result)
def test_has_favicon_image_link_tag(self): self.mock_res.text = """ <html> <head> <link rel="icon" href="test.com"/> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_favicon_image_link_tag() self.assertTrue(result)
def test_get_url_return_link_ref_attrs(self): self.mock_res.text = """ <html> <head> <link rel="icon" href="test_favicon"/> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_favicon_image_link_tag() self.assertTrue(result) url = handler.get_url() self.assertIsNotNone(url) self.assertEqual('test_favicon', url)
def test_get_url_return_meta_content(self): self.mock_res.text = """ <html> <head> <meta itemprop="image" content="testmeta"/> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_image_meta_tag() self.assertTrue(result) url = handler.get_url() self.assertIsNotNone(url) self.assertEqual('testmeta', url)
def test_has_no_itemprop_meta_tag(self): # meta tag does not exist self.mock_res.text = """ <html> <head> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_image_meta_tag() self.assertFalse(result) # meta:itemprop does not exist self.mock_res.text = """ <html> <head> <meta charset="utf8" content="testmeta"/> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_image_meta_tag() self.assertFalse(result) # meta:itemprop's value is not image self.mock_res.text = """ <html> <head> <meta itemprop="test" content="testmeta.icon"/> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_image_meta_tag() self.assertFalse(result) # meta has itemprop attribute but does not have content attribute self.mock_res.text = """ <html> <head> <meta itemprop="image"/> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_image_meta_tag() self.assertFalse(result) # meta content attribute has not value self.mock_res.text = """ <html> <head> <meta itemprop="image" content=""/> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_image_meta_tag() self.assertFalse(result)
def test_has_no_favicon_link_tag(self): # link tag does not exist self.mock_res.text = """ <html> <head> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_favicon_image_link_tag() self.assertFalse(result) # link:rel does not exist self.mock_res.text = """ <html> <head> <link test="test"/> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_favicon_image_link_tag() self.assertFalse(result) # link:ref's value is not icon self.mock_res.text = """ <html> <head> <link rel="stylesheet" href="test.icon"/> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_favicon_image_link_tag() self.assertFalse(result) # link has rel attribute but does not have href attribute self.mock_res.text = """ <html> <head> <link rel="icon" /> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_favicon_image_link_tag() self.assertFalse(result) # link href attribute has not value self.mock_res.text = """ <html> <head> <link rel="icon" href=""/> </head> <body> </body> </html> """ handler = FaviconHandler(self.mock_res) result = handler.set_favicon_image_link_tag() self.assertFalse(result)