|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | +import time |
| 4 | +import smtplib |
| 5 | +from email.mime.text import MIMEText |
| 6 | + |
| 7 | +# Replace with your own email and password |
| 8 | +SENDER_EMAIL = '[email protected]' |
| 9 | +SENDER_PASSWORD = 'your_sender_password' |
| 10 | +RECIPIENT_EMAIL = '[email protected]' |
| 11 | + |
| 12 | +WISHLIST_URL = 'https://www.amazon.com/gp/registry/wishlist/YOUR_WISHLIST_ID' |
| 13 | +CHECK_INTERVAL = 3600 # Check every hour |
| 14 | + |
| 15 | +def get_wishlist_items(): |
| 16 | + headers = { |
| 17 | + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" |
| 18 | + } |
| 19 | + response = requests.get(WISHLIST_URL, headers=headers) |
| 20 | + |
| 21 | + if response.status_code == 200: |
| 22 | + soup = BeautifulSoup(response.content, 'html.parser') |
| 23 | + items = soup.find_all('li', class_='a-spacing-none g-item-sortable') |
| 24 | + |
| 25 | + wishlist = [] |
| 26 | + for item in items: |
| 27 | + name = item.find('a', class_='a-link-normal').get_text().strip() |
| 28 | + price_element = item.find('span', class_='a-offscreen') |
| 29 | + price = price_element.get_text().strip() if price_element else "Price not available" |
| 30 | + availability = "In stock" if "In Stock" in item.get_text() else "Out of stock" |
| 31 | + wishlist.append({'name': name, 'price': price, 'availability': availability}) |
| 32 | + |
| 33 | + return wishlist |
| 34 | + |
| 35 | + else: |
| 36 | + print("Failed to retrieve wishlist data from Amazon.") |
| 37 | + return [] |
| 38 | + |
| 39 | +def send_email(subject, message): |
| 40 | + msg = MIMEText(message) |
| 41 | + msg['Subject'] = subject |
| 42 | + msg['From'] = SENDER_EMAIL |
| 43 | + msg['To'] = RECIPIENT_EMAIL |
| 44 | + |
| 45 | + server = smtplib.SMTP('smtp.gmail.com', 587) |
| 46 | + server.starttls() |
| 47 | + server.login(SENDER_EMAIL, SENDER_PASSWORD) |
| 48 | + server.sendmail(SENDER_EMAIL, RECIPIENT_EMAIL, msg.as_string()) |
| 49 | + server.quit() |
| 50 | + |
| 51 | +def main(): |
| 52 | + while True: |
| 53 | + wishlist_items = get_wishlist_items() |
| 54 | + |
| 55 | + for item in wishlist_items: |
| 56 | + if "Price not available" not in item['price'] and "$" in item['price']: |
| 57 | + price = float(item['price'][1:]) |
| 58 | + if price < 100.00: # Set your desired price threshold |
| 59 | + subject = f"Wishlist Alert: Price drop for {item['name']}!" |
| 60 | + message = f"The price of {item['name']} has dropped to {item['price']}. Check it out on your wishlist: {WISHLIST_URL}" |
| 61 | + send_email(subject, message) |
| 62 | + elif item['availability'] == "In stock": |
| 63 | + subject = f"Wishlist Alert: {item['name']} is back in stock!" |
| 64 | + message = f"{item['name']} is now back in stock. Check it out on your wishlist: {WISHLIST_URL}" |
| 65 | + send_email(subject, message) |
| 66 | + |
| 67 | + time.sleep(CHECK_INTERVAL) |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + main() |
0 commit comments