diff --git a/src/scrape_up/pinterest/pinterest.py b/src/scrape_up/pinterest/pinterest.py index a5f2df99..14ae7c64 100644 --- a/src/scrape_up/pinterest/pinterest.py +++ b/src/scrape_up/pinterest/pinterest.py @@ -65,32 +65,26 @@ def get_today(self): return None def get_photo(self, url): - - """ - Class - `Pinterest` - Example: - ```python - pinterestphoto = Pinterest() - photo = pinterestphoto.get_photo(your pinterest url) - ``` - Returns: Photo Image URL | None """ - try: - page = requests.get(url) - soup = bs(page.content, "html.parser") - image = soup.find("img", class_="hCL") - print("Image tag:", image) # Add this line - if image: - return {"alt": image.get("alt"), "image": image.get("src")} - else: - print("No image found") # Add this line + Class - `Pinterest` + Example: + ```python + pinterestphoto = Pinterest() + photo = pinterestphoto.get_photo(your pinterest url) + ``` + Returns: Photo Image URL | None + """ + try: + page = requests.get(url) + soup = bs(page.content, "html.parser") + image = soup.find("img", class_="hCL") + if image: + return {"alt": image.get("alt"), "image": image.get("src")} + else: + return None + except Exception as e: return None - except Exception as e: - print("Error:", e) - return None - - def search_pins(self, keyword): """ Search for pins containing a specific keyword on Pinterest. @@ -112,7 +106,6 @@ def search_pins(self, keyword): pins.append({"link": link, "image": image}) return pins except Exception as e: - print("Error:", e) return None def get_pin_details(self, pin_url): @@ -131,8 +124,14 @@ def get_pin_details(self, pin_url): title = soup.find("meta", property="og:title").get("content") description = soup.find("meta", property="og:description").get("content") saves = soup.find("meta", property="pinterestapp:saves").get("content") - comments = soup.find("meta", property="pinterestapp:comments").get("content") - return {"title": title, "description": description, "saves": saves, "comments": comments} + comments = soup.find("meta", property="pinterestapp:comments").get( + "content" + ) + return { + "title": title, + "description": description, + "saves": saves, + "comments": comments, + } except Exception as e: - print("Error:", e) return None diff --git a/src/test/pinterest_test.py b/src/test/pinterest_test.py index d2a3ec92..b289f506 100644 --- a/src/test/pinterest_test.py +++ b/src/test/pinterest_test.py @@ -1,8 +1,8 @@ import unittest -from scrape_up import pinterest +from scrape_up.pinterest import Pinterest -class TestPinterest(unittest.TestCase): +class TestPinterest(unittest.TestCase): def setUp(self): self.pinterest = Pinterest() @@ -11,35 +11,36 @@ def test_get_today(self): self.assertIsInstance(today_topics, list, "Expected get_today to return a list") if today_topics: for topic in today_topics: - self.assertIn('link', topic) - self.assertIn('title', topic) - self.assertIn('subtitle', topic) - self.assertIn('image', topic) + self.assertIn("link", topic) + self.assertIn("title", topic) + self.assertIn("subtitle", topic) + self.assertIn("image", topic) def test_get_photo(self): - url = "https://www.pinterest.com/pin/1234567890/" # replace with a valid Pinterest pin URL + url = "https://www.pinterest.com/pin/1234567890/" photo = self.pinterest.get_photo(url) if photo: - self.assertIn('alt', photo) - self.assertIn('image', photo) + self.assertIn("alt", photo) + self.assertIn("image", photo) def test_search_pins(self): keyword = "nature" - pins = self.pinterest.search_pins(keyword) + pins = self.pinterest.search_pins(keyword=keyword) self.assertIsInstance(pins, list, "Expected search_pins to return a list") if pins: for pin in pins: - self.assertIn('link', pin) - self.assertIn('image', pin) + self.assertIn("link", pin) + self.assertIn("image", pin) def test_get_pin_details(self): - pin_url = "https://www.pinterest.com/pin/1234567890/" # replace with a valid Pinterest pin URL + pin_url = "https://www.pinterest.com/pin/1234567890/" details = self.pinterest.get_pin_details(pin_url) if details: - self.assertIn('title', details) - self.assertIn('description', details) - self.assertIn('saves', details) - self.assertIn('comments', details) + self.assertIn("title", details) + self.assertIn("description", details) + self.assertIn("saves", details) + self.assertIn("comments", details) + -if __name__ == '__main__': +if __name__ == "__main__": unittest.main()