Skip to content

Commit 736cbc0

Browse files
committed
feat: Ask for product's URL
- validate price to compare with (Day 47)
1 parent 9c7a691 commit 736cbc0

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,5 @@
7474
- CSS 3
7575
- BeautifulSoup
7676
- Web Scraping
77-
- Selenium WebDriver
77+
- Selenium WebDriver
78+
- Regular Expressions (Regex)

day047/main.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import requests
23
import smtplib
34
from bs4 import BeautifulSoup
@@ -10,7 +11,16 @@
1011
FROM_PASSWORD = environ.get("FROM_PASSWORD")
1112
TO_EMAIL = environ.get("TO_EMAIL")
1213

13-
URL = "https://www.amazon.in/dp/B09RHCYZ82"
14+
# Define a regular expression pattern for Amazon product URLs
15+
amazon_url_pattern = r"https?://(?:www\.)?amazon\.(?:com|ca|co\.uk|de|fr|it|es|co\.jp|com\.au|com\.mx|nl|com\.br|ae|in)/(?:[\w-]+/)?(?:dp|gp/product)/(?:\w{10}|\w{13})"
16+
17+
# Keep asking for a valid Amazon product URL from the user
18+
while True:
19+
URL = input("Please provide the URL of the product on Amazon:\n")
20+
if re.match(amazon_url_pattern, URL):
21+
break
22+
else:
23+
print("Invalid Amazon product URL. Please try again.")
1424

1525
http_headers = {
1626
"Accept-Language": "en-US,en;q=0.9",
@@ -20,15 +30,28 @@
2030
response = requests.get(URL, headers=http_headers)
2131
soup = BeautifulSoup(response.text, 'html.parser')
2232

23-
price = float(soup.find(name="span", class_="a-offscreen").getText()[1:])
33+
price_str = soup.find(name="span", class_="a-offscreen").getText().replace(",", "")
34+
if price_match := re.search(r"([\d,]+(?:\.\d{1,2})?)", price_str):
35+
price = float(price_match[1].replace(",", ""))
36+
else:
37+
print("Could not extract price information. Exiting.")
38+
exit()
39+
2440
product_name = " ".join(soup.find(
2541
name="span", class_="a-size-large product-title-word-break").getText().split())
2642

27-
if price < 400.00:
43+
user_price_str = input("What amount do you want to compare the product with?\n")
44+
if user_price_match := re.search(r"([\d,]+(?:\.\d{1,2})?)", user_pri ce_str):
45+
user_price = float(user_price_match[1].replace(",", ""))
46+
else:
47+
print("Could not extract user price information. Exiting.")
48+
exit()
49+
50+
if price < user_price:
2851
with smtplib.SMTP("smtp.mail.yahoo.com", 587) as connection:
2952
connection.starttls()
3053
connection.login(FROM_EMAIL, FROM_PASSWORD)
3154
connection.sendmail(from_addr=FROM_EMAIL,
3255
to_addrs=TO_EMAIL,
33-
msg=f"Subject: Price Drop Alert!\n\n{product_name} is now available in{price}\n\nVisit: {URL}".encode('utf-8')
56+
msg=f"Subject: Price Drop Alert!\n\n{product_name} is now available for{price}\n\nVisit: {URL}".encode('utf-8')
3457
)

day048/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from selenium import webdriver
2-
from selenium.webdriver.chrome.service import Service
2+
from selenium.webdriver.edge.service import Service
33
from selenium.webdriver.common.by import By
44
import time
55

@@ -23,7 +23,6 @@
2323
7: "Time machine",
2424
}
2525

26-
2726
def cookie_clicker():
2827
check = time.time() + 5
2928
while True:

0 commit comments

Comments
 (0)