import requests
from datetime import datetime,timedelta
from twilio.rest import Client
# 관심있는 주식 관련 소스
STOCK_NAME = "TSLA"
COMPANY_NAME = "Tesla Inc"
# twilio 정보
account_sid = ""
auth_token = ""
from_number = "+"
to_number = "+"
# 엔드포인트
STOCK_ENDPOINT = "<https://www.alphavantage.co/query>"
NEWS_ENDPOINT = "<https://newsapi.org/v2/everything>"
# API 키
STOCK_API_KEY = "ZDB69DNR8GYS0MKM"
NEWS_API_KEY = "c1026d4660034a68b1d670762d088b31"
# 주식 정보 가져오기
stock_params = {
"function" : "TIME_SERIES_DAILY",
"symbol" : STOCK_NAME,
"apikey" : STOCK_API_KEY
}
stock_response = requests.get(url=STOCK_ENDPOINT,params=stock_params)
data = stock_response.json()["Time Series (Daily)"]
daily_data = [value for key,value in data.items()]
yesterday_closing_price = float(daily_data[0]["4. close"])
before_closing_price = float(daily_data[1]["4. close"])
difference = yesterday_closing_price - before_closing_price
# 비교 퍼센트
difference_percente = round((difference / yesterday_closing_price) * 100)
# 기호 지정
up_down = ""
if difference > 0 :
up_down = "🔺"
else :
up_down = "🔻"
# 주식 폭이 1% 이상이면 뉴스 정보 가져오기
if abs(difference_percente) > 1 :
new_params = {
"q" : COMPANY_NAME,
"apikey" : NEWS_API_KEY
}
news_response = requests.get(url=NEWS_ENDPOINT,params=new_params)
news_data = news_response.json()
formatted_article = [f"{STOCK_NAME}: {up_down}{difference_percente}%\\nHeadline: {article['title']}. \\nBrief: {article['description']}" for article in news_data["articles"][:3]]
client = Client(account_sid, auth_token)
for article in formatted_article :
message = client.messages.create(
body=formatted_article,
from_='+18722469869',
to='+821028919394'
)