diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 0d7c2856..2222b7f6 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -2,127 +2,76 @@ ## Our Pledge -We as members, contributors, and leaders pledge to make participation in our -community a harassment-free experience for everyone, regardless of age, body -size, visible or invisible disability, ethnicity, sex characteristics, gender -identity and expression, level of experience, education, socio-economic status, -nationality, personal appearance, race, religion, or sexual identity -and orientation. +We, as members, contributors, and leaders, pledge to create a harassment-free experience for everyone in our community. We welcome individuals regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. -We pledge to act and interact in ways that contribute to an open, welcoming, -diverse, inclusive, and healthy community. +We commit to fostering an environment of empathy, kindness, diversity, inclusivity, and health. ## Our Standards -Examples of behavior that contributes to a positive environment for our -community includes: +Examples of behavior that contributes to a positive environment include: -- Demonstrating empathy and kindness toward other people -- Being respectful of differing opinions, viewpoints, and experiences -- Giving and gracefully accepting constructive feedback -- Accepting responsibility and apologizing to those affected by our mistakes, - and learning from the experience -- Focusing on what is best not just for us as individuals, but for the - overall community +- Demonstrating empathy and kindness towards others +- Respecting differing opinions, viewpoints, and experiences +- Giving and accepting constructive feedback gracefully +- Taking responsibility for our mistakes, apologizing to those affected, and learning from the experience +- Considering the well-being of the entire community, not just ourselves Examples of unacceptable behavior include: -- The use of sexualized language or imagery, and sexual attention or - advances of any kind -- Trolling, insulting or derogatory comments, and personal or political attacks -- Public or private harassment -- Publishing others' private information, such as a physical or email - address, without their explicit permission -- Other conduct which could reasonably be considered inappropriate in a - professional setting +- Use of sexualized language, imagery, or advances +- Trolling, insults, derogatory comments, personal attacks, or political attacks +- Harassment, whether public or private +- Publishing others' private information without explicit permission +- Any conduct that could reasonably be considered inappropriate in a professional setting ## Enforcement Responsibilities -Community leaders are responsible for clarifying and enforcing our standards of -acceptable behavior and will take appropriate and fair corrective action in -response to any behavior that they deem inappropriate, threatening, offensive, -or harmful. - -Community leaders have the right and responsibility to remove, edit, or reject -comments, commits, code, wiki edits, issues, and other contributions that are -not aligned to this Code of Conduct, and will communicate reasons for moderation -decisions when appropriate. +Community leaders are responsible for enforcing these standards and will take fair corrective action in response to inappropriate behavior. They have the right to remove, edit, or reject contributions that do not align with this Code of Conduct, and will communicate reasons for their actions. ## Scope -This Code of Conduct applies within all community spaces and also applies when -an individual is officially representing the community in public spaces. -Examples of representing our community include using an official e-mail address, -posting via an official social media account, or acting as an appointed -representative at an online or offline event. +This Code of Conduct applies to all community spaces and when individuals are officially representing the community in public. This includes using official e-mail addresses, posting via official social media accounts, or acting as appointed representatives at events. ## Enforcement -Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported to the community leaders responsible for enforcement at -Gmail. -All complaints will be reviewed and investigated promptly and fairly. +Report instances of abusive, harassing, or otherwise unacceptable behavior to the community leaders responsible for enforcement at [contact email]. All complaints will be promptly and fairly reviewed and investigated. -All community leaders are obligated to respect the privacy and security of the -reporter of any incident. +Community leaders are required to respect the privacy and security of incident reporters. ## Enforcement Guidelines -Community leaders will follow these Community Impact Guidelines in determining -the consequences for any action they deem in violation of this Code of Conduct: +Community leaders will use these guidelines to determine consequences for violations: ### 1. Correction -**Community Impact**: Use of inappropriate language or other behavior deemed -unprofessional or unwelcome in the community. +**Community Impact**: Use of inappropriate language or behavior deemed unprofessional or unwelcome. -**Consequence**: A private, written warning from community leaders, providing -clarity around the nature of the violation and an explanation of why the -behavior was inappropriate. A public apology may be requested. +**Consequence**: A private, written warning from community leaders, explaining the violation and why it was inappropriate. A public apology may be requested. ### 2. Warning -**Community Impact**: A violation through a single incident or series -of actions. +**Community Impact**: A violation through a single incident or series of actions. -**Consequence**: A warning with consequences for continued behavior. No -interaction with the people involved, including unsolicited interaction with -those enforcing the Code of Conduct, for a specified period. This -includes avoiding interactions in community spaces as well as external channels -like social media. Violating these terms may lead to a temporary or -permanent ban. +**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved for a specified period, including in community spaces and external channels. Violating this may lead to a temporary or permanent ban. ### 3. Temporary Ban -**Community Impact**: A serious violation of community standards, including -sustained inappropriate behavior. +**Community Impact**: A serious violation of community standards. -**Consequence**: A temporary ban from any sort of interaction or public -communication with the community for a specified period. No public or -private interaction with the people involved, including unsolicited interaction -with those enforcing the Code of Conduct, is allowed during this period. -Violating these terms may lead to a permanent ban. +**Consequence**: A temporary ban from any interaction or public communication with the community. No interaction with the people involved is allowed, including unsolicited contact. Violating this may lead to a permanent ban. ### 4. Permanent Ban -**Community Impact**: Demonstrating a pattern of violation of community -standards, including sustained inappropriate behavior, harassment of an -individual, or aggression toward or disparagement of classes of individuals. +**Community Impact**: Demonstrating a pattern of violation of community standards. -**Consequence**: A permanent ban from any sort of public interaction within -the community. +**Consequence**: A permanent ban from any public interaction within the community. ## Attribution -This Code of Conduct is adapted from the [Contributor Covenant][homepage], -version 2.0, available at -https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. -Community Impact Guidelines were inspired by [Mozilla's code of conduct -enforcement ladder](https://github.com/mozilla/diversity). +Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). [homepage]: https://www.contributor-covenant.org -For answers to common questions about this code of conduct, see the FAQ at -https://www.contributor-covenant.org/faq. Translations are available at -https://www.contributor-covenant.org/translations. +For answers to common questions about this code of conduct, see the FAQ at https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. diff --git a/src/scrape_up/amazon/products.py b/src/scrape_up/amazon/products.py index c6691406..15ce0d6e 100644 --- a/src/scrape_up/amazon/products.py +++ b/src/scrape_up/amazon/products.py @@ -2,82 +2,86 @@ from bs4 import BeautifulSoup -# scraping amazon product page class Product: + """Class for fetching and retrieving product details from Amazon.""" + def __init__(self, product_name: str): - self.product_name = product_name + """ + Initialize the Product object with a product name. - def get_product(self): + Args: + product_name (str): The name of the product. """ - Class - `Product`\n - Example -\n - ```python - product = Product(product_name="watch") - product.get_product() - ``` - Return\n - ```python - return - { - "data": product_link, - "message": f"Product data has been fetched", + self.product_name = product_name + self.headers = { + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36" } - ``` + + def fetch_product_page(self): + """ + Fetch the HTML content of the Amazon search page for the product. + + Returns: + BeautifulSoup: A BeautifulSoup object containing the parsed HTML content of the search page. + + Raises: + Exception: If there is an error fetching the page. """ try: - product_name = self.product_name - product_name = product_name.replace(" ", "+") + product_name = self.product_name.replace(" ", "+") url = f"https://www.amazon.in/s?k={product_name}" - headers = { - "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \ - (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36" - } - r = requests.get(url, headers=headers) - soup = BeautifulSoup(r.content, "html.parser") - product = soup.find("div", {"class": "s-product-image-container"}) - product_link = product.find("a", {"class": "a-link-normal"})["href"] - product_link = "https://www.amazon.in" + product_link - return { - "data": product_link, - "message": f"Product data has been fetched", - } - except: - return { - "data": None, - "message": f"Unable to fetch product's data", - } + r = requests.get(url, headers=self.headers) + r.raise_for_status() # Raise HTTPError for bad responses + return BeautifulSoup(r.content, "html.parser") + except requests.RequestException as e: + raise Exception(f"Error fetching product page: {str(e)}") + + def get_product(self): + """ + Get the link to the product from the search page. + + Returns: + dict: A dictionary containing the product link and a message indicating success. + + Raises: + Exception: If there is an error fetching the product link. + """ + try: + soup = self.fetch_product_page() + product = soup.find("div", {"class": "s-result-item"}) + if product: + product_link = product.find("a", {"class": "a-link-normal"})["href"] + product_link = "https://www.amazon.in" + product_link + return { + "data": product_link, + "message": "Product data has been fetched", + } + else: + return { + "data": None, + "message": "Product not found", + } + except Exception as e: + raise Exception(f"Unable to fetch product's data: {str(e)}") - # Get product details def get_product_details(self): """ - Class - `Product`\n - Example -\n - ```python - product = Product(product_name="watch") - product.get_product_details() - ``` - Return\n - ```python - return - { - "data": product_details, - "message": f"Product detail has been fetched", - } - ``` + Get details of the product from the product page. + + Returns: + dict: A dictionary containing product details and a message indicating success. + + Raises: + Exception: If there is an error fetching the product details. """ try: product_link = self.get_product()["data"] - headers = { - "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \ - (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36" - } - r = requests.get(product_link, headers=headers) + r = requests.get(product_link, headers=self.headers) + r.raise_for_status() # Raise HTTPError for bad responses soup = BeautifulSoup(r.content, "html.parser") product_name = soup.find("span", {"id": "productTitle"}).text.strip() product_price = soup.find("span", {"class": "a-price-whole"}).text.strip() - product_rating = soup.find( - "span", {"class": "a-size-base a-color-base"} - ).text.strip() + product_rating = soup.find("span", {"class": "a-icon-alt"}).text.strip() product_details = { "product_name": product_name, "product_price": product_price, @@ -86,83 +90,53 @@ def get_product_details(self): } return { "data": product_details, - "message": f"Product detail has been fetched", - } - except: - return { - "data": None, - "message": f"Unable to fetch product detail", + "message": "Product detail has been fetched", } + except Exception as e: + raise Exception(f"Unable to fetch product detail: {str(e)}") - # Get product image def get_product_image(self): """ - Class - `Product`\n - Example -\n - ```python - product = Product(product_name="watch") - product.get_product_image() - ``` - Return\n - ```python - return - { - "data": product_image, - "message": f"Product image has been fetched", - } - ``` + Get the URL of the product image from the product page. + + Returns: + dict: A dictionary containing the product image URL and a message indicating success. + + Raises: + Exception: If there is an error fetching the product image. """ try: product_link = self.get_product()["data"] - headers = { - "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \ - (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36" - } - r = requests.get(product_link, headers=headers) + r = requests.get(product_link, headers=self.headers) + r.raise_for_status() # Raise HTTPError for bad responses soup = BeautifulSoup(r.content, "html.parser") - product_image = soup.find( - "img", {"class": "a-dynamic-image a-stretch-horizontal"} - )["src"] - + product_image = soup.find("div", {"id": "imgTagWrapperId"}).find("img")[ + "data-old-hires" + ] return { "data": product_image, - "message": f"Product image has been fetched", - } - except: - return { - "data": None, - "message": f"Unable to fetch product image", + "message": "Product image has been fetched", } + except Exception as e: + raise Exception(f"Unable to fetch product image: {str(e)}") - # Get customer reviews def customer_review(self): """ - Class - `Product`\n - Example -\n - ```python - product = Product(product_name="watch") - product.customer_review() - ``` - Return\n - ```python - return - { - "data": review, - "message": f"Product review has been fetched", - } - ``` + Get customer reviews of the product from the product page. + + Returns: + dict: A dictionary containing the product reviews and a message indicating success. + + Raises: + Exception: If there is an error fetching the product reviews. """ try: product_link = self.get_product()["data"] - headers = { - "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \ - (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36" - } - r = requests.get(product_link, headers=headers) + r = requests.get(product_link, headers=self.headers) + r.raise_for_status() # Raise HTTPError for bad responses soup = BeautifulSoup(r.content, "html.parser") - review_elements = soup.find_all("div", {"data-hook": "review"}) - + reviews = [] for review_element in review_elements: reviewer_name = review_element.find( "span", {"class": "a-profile-name"} @@ -181,13 +155,17 @@ def customer_review(self): review_text = review_element.find( "span", {"data-hook": "review-body"} ).text.strip() - review = [reviewer_name, rating, review_title, review_date, review_text] - return { - "data": review, - "message": f"Product review has been fetched", - } - except: + review = { + "reviewer_name": reviewer_name, + "rating": rating, + "review_title": review_title, + "review_date": review_date, + "review_text": review_text, + } + reviews.append(review) return { - "data": None, - "message": f"Unable to fetch product review", + "data": reviews, + "message": "Product reviews have been fetched", } + except Exception as e: + raise Exception(f"Unable to fetch product reviews: {str(e)}")