|
1 | 1 | import requests |
| 2 | +import os |
| 3 | +from dotenv import load_dotenv |
| 4 | + |
| 5 | +"load .env folder" |
| 6 | +load_dotenv() |
2 | 7 |
|
3 | 8 | STOCK_NAME = "TSLA" |
4 | 9 | COMPANY_NAME = "Tesla Inc" |
| 10 | +API_KEY=os.getenv("API_KEY") |
5 | 11 |
|
6 | | -STOCK_ENDPOINT = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=COMPANY_NAME&interval=5min&apikey=0CDA2IATNBQB8Q5Z" |
| 12 | +# You can now use api_key in your application |
| 13 | +STOCK_ENDPOINT = "https://www.alphavantage.co/query" |
7 | 14 | NEWS_ENDPOINT = "https://newsapi.org/v2/everything" |
8 | 15 |
|
9 | 16 | ## STEP 1: Use https://www.alphavantage.co/documentation/#daily |
10 | 17 | # When stock price increase/decreases by 5% between yesterday and the day before yesterday then print("Get News"). |
11 | 18 |
|
12 | 19 | #TODO 1. - Get yesterday's closing stock price. Hint: You can perform list comprehensions on Python dictionaries. e.g. [new_value for (key, value) in dictionary.items()] |
13 | | -response = requests.get(STOCK_ENDPOINT) |
| 20 | +parameter={ |
| 21 | + "function":"TIME_SERIES_DAILY", |
| 22 | + "symbol":"IBM", |
| 23 | + "apikey":API_KEY |
| 24 | +} |
| 25 | +response = requests.get(STOCK_ENDPOINT,params=parameter) |
14 | 26 | data = response.json() |
15 | | -print(data) |
| 27 | +Daily_Time_series=data["Time Series (Daily)"] |
| 28 | +specific_data=[value for (key,value) in Daily_Time_series.items()] |
| 29 | +yesterday_data=specific_data[0]["4. close"] |
| 30 | +print(yesterday_data) |
| 31 | + |
| 32 | + |
| 33 | +# print(yesterday) |
| 34 | +# print(data) |
16 | 35 | #TODO 2. - Get the day before yesterday's closing stock price |
| 36 | +a_day_before_yesterday=specific_data[1]["4. close"] |
| 37 | +print(a_day_before_yesterday) |
17 | 38 |
|
18 | 39 | #TODO 3. - Find the positive difference between 1 and 2. e.g. 40 - 20 = -20, but the positive difference is 20. Hint: https://www.w3schools.com/python/ref_func_abs.asp |
19 | 40 |
|
| 41 | +difference=abs(float(yesterday_data)-float(a_day_before_yesterday)) |
| 42 | +print(difference) |
| 43 | + |
20 | 44 | #TODO 4. - Work out the percentage difference in price between closing price yesterday and closing price the day before yesterday. |
21 | 45 |
|
| 46 | +difference_percentage = (difference/float(yesterday_data))*100 |
| 47 | +print("yesterday_percentage :",difference_percentage) |
| 48 | + |
22 | 49 | #TODO 5. - If TODO4 percentage is greater than 5 then print("Get News"). |
| 50 | +if difference_percentage> 5: |
| 51 | + print("getnews") |
23 | 52 |
|
24 | 53 | ## STEP 2: https://newsapi.org/ |
25 | 54 | # Instead of printing ("Get News"), actually get the first 3 news pieces for the COMPANY_NAME. |
26 | 55 |
|
27 | 56 | #TODO 6. - Instead of printing ("Get News"), use the News API to get articles related to the COMPANY_NAME. |
28 | 57 |
|
29 | | -#TODO 7. - Use Python slice operator to create a list that contains the first 3 articles. Hint: https://stackoverflow.com/questions/509211/understanding-slice-notation |
| 58 | +news_api_key=os.getenv("news_api_key") |
| 59 | +news_parameters={ |
| 60 | + "q":"IBM", |
| 61 | + "from":"2025-06-01", |
| 62 | + "sortedBy":"publishedAt", |
| 63 | + "apikey":news_api_key |
| 64 | +} |
| 65 | +news_data=requests.get(NEWS_ENDPOINT,params=news_parameters) |
| 66 | +print(news_data.json()) |
30 | 67 |
|
| 68 | +#TODO 7. - Use Python slice operator to create a list that contains the first 3 articles. Hint: https://stackoverflow.com/questions/509211/understanding-slice-notation |
| 69 | +first_3=news_data[:2] |
| 70 | +print(first_3) |
31 | 71 |
|
32 | 72 | ## STEP 3: Use twilio.com/docs/sms/quickstart/python |
33 | 73 | #to send a separate message with each article's title and description to your phone number. |
|
0 commit comments