-
Notifications
You must be signed in to change notification settings - Fork 16
Sabina #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Sabina #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .env |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import requests | ||
| import time | ||
| import os | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| load_dotenv() | ||
|
|
||
| API_KEY = os.getenv('API_KEY') #IEX CLOUD API | ||
|
|
||
| WEBHOOKS_KEY = os.getenv('WEBHOOKS_KEY') #WEBHOOKS KEY | ||
|
|
||
|
|
||
|
|
||
| IFTTT_URL = f"https://maker.ifttt.com/trigger/notify/with/key/{WEBHOOKS_KEY}" #IFTTT URL | ||
|
|
||
|
|
||
|
|
||
| PRICE_DROP_THRESHOLD = 0.25 #THRESHOLD FOR PRICE DROP | ||
|
|
||
| stock_symbols = ["AAPL", "GOOGL", "MSFT" , "NKE", "TSLA"] | ||
|
|
||
|
|
||
|
|
||
| #FUNCTION THAT RETURNS LATEST PRICE STOCKS | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good use of comments to clarify code |
||
| def get_stock_price(symbol): | ||
| url = f"https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={API_KEY}" | ||
| response = requests.get(url) | ||
| data = response.json() | ||
| return data['latestPrice'] | ||
|
|
||
|
|
||
| #FUNCTION THAT SENDS IFTTT NOTIFICATION | ||
| def send_notification(symbol, price): | ||
|
|
||
| payload = { | ||
| "value1" : symbol, | ||
| "value2" : str(price) | ||
| } | ||
|
|
||
| requests.post(IFTTT_URL, json=payload) | ||
|
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| while True: | ||
| try: | ||
| for symbol in stock_symbols: | ||
| current_stock_price = get_stock_price(symbol) | ||
| print(f"The latest price of {symbol} is {current_stock_price:} GBP") | ||
|
|
||
| if current_stock_price <= (get_stock_price(symbol) - PRICE_DROP_THRESHOLD): | ||
| send_notification(symbol, current_stock_price) | ||
| print(f"{symbol} price dropped by at least £0.25 GBP.") | ||
|
|
||
|
|
||
|
|
||
| time.sleep(300) #CHECKING PRICE AGAIN EVERY FIVE MINS | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Frequency of every 5 mins aligns with the rate at which the source updates its stock prices, and so could have explained that in separate documentation |
||
|
|
||
| except Exception as e: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could have used exceptions more to avoid disruptions that come from all the inputs |
||
| print("AN ERROR OCCURED!", e) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good use of variables to avoid magic numbers- falls within good coding standards