diff --git a/.github/workflows/AutoUpdate.yml b/.github/workflows/AutoUpdate.yml new file mode 100644 index 0000000..bbb2a90 --- /dev/null +++ b/.github/workflows/AutoUpdate.yml @@ -0,0 +1,27 @@ +name: Auto Update + +on: + schedule: + - cron: "0 */12 * * *" # 每 12 小時執行一次 + workflow_dispatch: # 允許手動觸發 + +jobs: + run_yfinanceTest: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 # 檢出您的代碼 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.9' # Python 版本 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt # 如果有 requirements.txt 檔案,否則手動指定需求 + + - name: Run yfinanceTest.py + run: python yfinanceTest.py # 執行您的 yfinanceTest.py diff --git a/FinMine,py b/FinMine,py new file mode 100644 index 0000000..bbb9e30 --- /dev/null +++ b/FinMine,py @@ -0,0 +1,18 @@ +from FinMind.data import DataLoader + +api = DataLoader() +# api.login_by_token(api_token='token') +# api.login(user_id='user_id',password='password') +# df = api.taiwan_stock_daily( +# stock_id='2330', +# start_date='2024-08-02', +# end_date='2024-08-7' +# ) +df = api.us_stock_info() +print(df) +df = api.us_stock_price( + stock_id='AAPL', + start_date='2024-08-02', + end_date='2024-08-7' +) +print(df) \ No newline at end of file diff --git a/mongoDB.py b/mongoDB.py new file mode 100644 index 0000000..1215099 --- /dev/null +++ b/mongoDB.py @@ -0,0 +1,84 @@ +from pymongo import MongoClient + +class MongoDBClient: + def __init__(self, uri, db_name): + """ + 初始化 MongoDB 客戶端。 + + :param uri: MongoDB 連接字串 + :param db_name: 資料庫名稱 + """ + self.client = MongoClient(uri) + self.db = self.client[db_name] + self.user_collection = self.db['User'] + + def insert_users(self, users): + """ + 插入多筆使用者資料到 User 表格。 + + :param users: 使用者資料的列表 + """ + try: + result = self.user_collection.insert_many(users) + print(f"成功插入 {len(result.inserted_ids)} 筆資料。") + except Exception as e: + print(f"插入資料時出現錯誤: {e}") + + def find_user_by_name(self, name): + """ + 根據使用者姓名查詢使用者資料。 + + :param name: 使用者姓名 + :return: 查詢結果 + """ + try: + user = self.user_collection.find_one({"name": name}) + return user + except Exception as e: + print(f"查詢資料時出現錯誤: {e}") + + def list_all_users(self): + """ + 列出所有使用者資料。 + + :return: 使用者資料的列表 + """ + try: + users = list(self.user_collection.find()) + return users + except Exception as e: + print(f"查詢資料時出現錯誤: {e}") + +if __name__ == "__main__": + # 替換為您的 MongoDB 連接字串 + user_name = "test" + password = "test" + uri = f"mongodb+srv://{user_name}:{password}@cluster0.zp7plle.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0" + db_name = "DevOps_Final_Project" + + # 初始化客戶端 + mongo_client = MongoDBClient(uri, db_name) + + # 範例使用者資料 + users = [ + {"name": "張三", "email": "zhangsan@example.com", "age": 25}, + {"name": "李四", "email": "lisi@example.com", "age": 30}, + {"name": "王五", "email": "wangwu@example.com", "age": 28} + ] + + # 插入使用者資料 + mongo_client.insert_users(users) + + # 查詢特定使用者 + user = mongo_client.find_user_by_name("李四") + if user: + print("查詢結果:", user) + else: + print("找不到該使用者。") + + # 列出所有使用者 + all_users = mongo_client.list_all_users() + print("所有使用者資料:") + for user in all_users: + print(user) + diff --git a/twseAPI.py b/twseAPI.py new file mode 100644 index 0000000..054b936 --- /dev/null +++ b/twseAPI.py @@ -0,0 +1,21 @@ +# 引入requests庫 +import requests +import json, pandas as pd + +# 定義API的URL +# url = 'https://openapi.twse.com.tw/v1/exchangeReport/STOCK_DAY_ALL' +url = 'https://openapi.twse.com.tw/v1/exchangeReport/MI_5MINS' +# 發送GET請求 +res = requests.get(url) +print(res) +jsondata = json.loads(res.text) +# jsondata + +# 引入pandas庫 +# 將JSON數據轉換為DataFrame +df = pd.DataFrame(jsondata) +# df.set_index("Code", inplace=True) +# df.replace('', '0', inplace=True) +# df[df.columns.difference(['Name'])] = df[df.columns.difference(['Name'])].astype(float) +print(df) + diff --git a/yfinanceTest.py b/yfinanceTest.py new file mode 100644 index 0000000..62819d4 --- /dev/null +++ b/yfinanceTest.py @@ -0,0 +1,44 @@ +import yfinance as yf +import matplotlib.pyplot as plt + +#import yfinance as yf + +# Initialize the Ticker object for Apple (AAPL) +apple_stock = yf.Ticker("AAPL") + +# Fetch historical data for the past year +# df = apple_stock.history(period="5d") + +# # Display data +# print(df) + +# # Optional: Plot closing prices + +# plt.figure(figsize=(12, 6)) +# plt.plot(df.index, df['Close'], label='AAPL Close Price') +# plt.title('Apple Stock Closing Prices Over the Past Year') +# plt.xlabel('Date') +# plt.ylabel('Close Price (USD)') +# plt.legend() +# plt.show() + + +df = apple_stock.history(period="1y") + +# 計算每日漲跌幅 +df['Change'] = df['Close'].diff() # 計算每日與前一天的差異 +df['Color'] = df['Change'].apply(lambda x: 'red' if x < 0 else 'green') # 設定顏色,跌為紅色,漲為綠色 + +# 繪製圖表 +plt.figure(figsize=(12, 6)) +for i in range(1, len(df)): + plt.plot([df.index[i-1], df.index[i]], [df['Close'][i-1], df['Close'][i]], + color=df['Color'][i], linewidth=1.5) # 根據漲跌幅選顏色 + +# 添加標題和標籤 +plt.title('Apple Stock Closing Prices Over the Past Year with Gains and Losses') +plt.xlabel('Date') +plt.ylabel('Close Price (USD)') +plt.show() + +