-
Notifications
You must be signed in to change notification settings - Fork 15
/
sending_current_stock_prices_with_line.py
81 lines (61 loc) · 3.27 KB
/
sending_current_stock_prices_with_line.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import requests
from fugle_realtime import HttpClient
import pandas as pd
import numpy as np
import os
# 匯入關注清單
portfolio_watchlist = open('portfolio_watchlist.txt', 'r').read().strip().split(',')
# secret token
FUGLE_API_TOKEN = os.environ['FUGLE_API_TOKEN']
LINE_NOTIFY_TOKEN = os.environ['LINE_NOTIFY_TOKEN']
def fugle_get_stock_price(portfolio):
stock_price_dataframe = pd.DataFrame(columns=['ticker',
'stock_name',
'date',
'update_time',
'price',
'yesterday_price',
'price_change'])
stock_price_dataframe['ticker'] = portfolio
# Fugle API 金鑰
api_client = HttpClient(api_token = FUGLE_API_TOKEN)
for i in range(len(portfolio)):
# 富果 API 讚! (再次強調非業配)
meta_data = api_client.intraday.meta(symbolId=portfolio[i])
price_data = api_client.intraday.chart(symbolId=portfolio[i])
# 股票名稱 & 昨日股價
stock_name = str(meta_data['data']['meta']['nameZhTw'])
yesterday_price = float(meta_data['data']['meta']['previousClose'])
# 即時股價 & 日期 & 即時股價更新時間
price = float(price_data['data']['chart']['c'][-1])
date = price_data['data']['info']['lastUpdatedAt'][5:7]+'/'+price_data['data']['info']['lastUpdatedAt'][8:10]
update_time = price_data['data']['info']['lastUpdatedAt'][11:19]
# 把數據塞進 DafaFrame 欄位
stock_price_dataframe['stock_name'][i] = stock_name
stock_price_dataframe['date'][i] = date
stock_price_dataframe['update_time'][i] = update_time
stock_price_dataframe['yesterday_price'][i] = yesterday_price
stock_price_dataframe['price'][i] = price
# 計算報酬率
stock_price_dataframe['price_change'] = ((stock_price_dataframe['price']/stock_price_dataframe['yesterday_price']-1)*100).astype('float').round(decimals = 2)
# 按照漲跌幅排序
stock_price_dataframe = stock_price_dataframe.sort_values(by=['price_change'], ascending=False).reset_index()
return stock_price_dataframe
def generate_message(stock_price_dataframe):
# 標題: 價格播報/ 日期/ 時間
message = '*價格播報*'+'\n'+'```'+stock_price_dataframe.date[0]+' '+stock_price_dataframe.update_time[0]+'```\n'
# 細項: 股票名稱/ 即時股價/ 漲跌%
for i in range(len(stock_price_dataframe)):
message += stock_price_dataframe.stock_name[i] + (6 - len(stock_price_dataframe.stock_name[i]))*' ' + str(stock_price_dataframe.price[i]) + (6 - len(str(stock_price_dataframe.price[i])))*' ' +'( _' + str(stock_price_dataframe.price_change[i]) + '%_ )' + '\n'
return message
def lineNotifyMessage(token, msg):
headers = {
"Authorization": "Bearer " + token,
"Content-Type" : "application/x-www-form-urlencoded"
}
payload = {'message': msg }
r = requests.post("https://notify-api.line.me/api/notify", headers = headers, params = payload)
return r.status_code
stock_price_dataframe = fugle_get_stock_price(portfolio_watchlist)
message = generate_message(stock_price_dataframe)
lineNotifyMessage(LINE_NOTIFY_TOKEN, message)