I have placed a Python script in /config/python_scripts/amz.py
When I try to run it via Developer tools -> services “python_script.amz”
I get the following error:
2021-01-19 13:53:53 ERROR (SyncWorker_2) [homeassistant.components.python_script] Error loading script amz.py: Line 49: "__name__" is an invalid variable name because it starts with "_"
Line 49 is if __name__ == '__main__':
This is the script
import requests
from bs4 import BeautifulSoup
import smtplib
URL = "https://www.amazon.co.uk/Nicorette-Cools-Lozenges-Stop-Smoking/dp/B00EV3ZHT6"
# "https://www.amazon.co.uk/Apple-Airpods-Wireless-Charging-latest/dp/B07PYM8FB8/ref=sr_1_6?keywords=airpods&qid=1581244893&sr=8-6"
HEADERS = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Safari/605.1.15"}
PRICE_VALUE = 14.50
EMAIL_ADDRESS = "******@gmail.com"
def trackPrices():
price = float(getPrice())
if price > PRICE_VALUE:
diff = float(price - PRICE_VALUE)
# diff = int(price - PRICE_VALUE)
print(f"Still £{diff} too expensive")
else:
print("Cheaper! Notifying...")
sendEmail()
pass
def getPrice():
page = requests.get(URL, headers=HEADERS)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find(id='productTitle').get_text().strip()
price = soup.find(id='priceblock_ourprice').get_text().strip()[1:6]
print(title)
print('***********')
print(price)
print('************')
print(URL)
return price
def sendEmail():
subject = "Amazon Price Dropped!"
mailtext = 'Subject:'+subject+'\n\n'+URL+'\n\n'+getPrice()
server = smtplib.SMTP(host='smtp.gmail.com', port=587)
server.ehlo()
server.starttls()
server.login(EMAIL_ADDRESS, '*******')
server.sendmail(EMAIL_ADDRESS, EMAIL_ADDRESS, mailtext)
pass
if __name__ == '__main__':
trackPrices()
getPrice()