Skip to content

Commit

Permalink
Merge pull request #979 from priyankeshh/pinterestTestPy
Browse files Browse the repository at this point in the history
Added pinterest_test.py
  • Loading branch information
nikhil25803 authored May 17, 2024
2 parents 190873e + 3b23acf commit 253ce2a
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 7 deletions.
71 changes: 64 additions & 7 deletions src/scrape_up/pinterest/pinterest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Pinterest:
| ---------------------- | ------------------------------------------------------------------ |
| `.get_today()` | Returns the list of todays topics |
| `.get_photo(your url)` | Returns the link to the image (so you dont need an account) |
| `.search_pins(keyword)`| Search for pins containing a specific keyword on Pinterest |
| `.get_pin_details(pin_url)`| Fetch details about a specific pin on Pinterest |
"""

def __init__(self):
Expand Down Expand Up @@ -64,18 +66,73 @@ def get_today(self):

def get_photo(self, url):
"""
Class - `Pinterest`
Example:
Class - `Pinterest`
Example:
```python
pinterestphoto = Pinterest()
photo = pinterestphoto.get_photo(your pinterest url)
pinterestphoto = Pinterest()
photo = pinterestphoto.get_photo(your pinterest url)
```
Returns: Photo Image URL | None
Returns: Photo Image URL | None
"""
try:
page = requests.get(url)
soup = bs(page.content, "html.parser")
image = soup.find("img", class_="hCL")
return {"alt": image.get("alt"), "image": image.get("src")}
except:
if image:
return {"alt": image.get("alt"), "image": image.get("src")}
else:
return None
except Exception as e:
return None

def search_pins(self, keyword):
"""
Search for pins containing a specific keyword on Pinterest.
Args:
keyword (str): The keyword to search for.
Returns:
list: A list of dictionaries containing information about the matching pins.
"""
try:
url = f"https://www.pinterest.com/search/pins/?q={keyword}"
page = requests.get(url)
soup = bs(page.content, "html.parser")
pins = []
for item in soup.find_all("div", class_="GrowthUnauthPinImage"):
link = item.find("a").get("href")
image = item.find("img").get("src")
pins.append({"link": link, "image": image})
return pins
except Exception as e:
return None

def get_pin_details(self, pin_url):
"""
Fetch details about a specific pin on Pinterest.
Args:
pin_url (str): The URL of the Pinterest pin.
Returns:
dict: A dictionary containing details about the pin, such as title, description, saves, and comments.
"""
try:
page = requests.get(pin_url)
soup = bs(page.content, "html.parser")
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,
}
except Exception as e:
return None

46 changes: 46 additions & 0 deletions src/test/pinterest_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest
from scrape_up.pinterest import Pinterest


class TestPinterest(unittest.TestCase):
def setUp(self):
self.pinterest = Pinterest()

def test_get_today(self):
today_topics = self.pinterest.get_today()
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)

def test_get_photo(self):
url = "https://pin.it/1ZhgQA5AG"
photo = self.pinterest.get_photo(url)
if photo:
self.assertIn("alt", photo)
self.assertIn("image", photo)

def test_search_pins(self):
keyword = "nature"
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)

def test_get_pin_details(self):
pin_url = "https://pin.it/1ZhgQA5AG"
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)


if __name__ == "__main__":
unittest.main()

0 comments on commit 253ce2a

Please sign in to comment.