Skip to content

Commit

Permalink
Check
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhil25803 committed May 17, 2024
1 parent bcb8af4 commit 1d48cae
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
53 changes: 26 additions & 27 deletions src/scrape_up/pinterest/pinterest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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):
Expand All @@ -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
37 changes: 19 additions & 18 deletions src/test/pinterest_test.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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()

0 comments on commit 1d48cae

Please sign in to comment.