#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 添加微软持仓到Notion数据库 """ import requests import json from datetime import datetime # Notion API 配置 NOTION_API_KEY = "ntn_c43902219395mirQBetIfYoww1qKCAF14GBRUQeDee29o2" DATABASE_ID = "2fb105ad-7873-8175-bbbd-e5b87cf101d9" # 美股持仓数据库 ID # 微软持仓数据 POSITION = { "symbol": "MSFT", "name": "Microsoft Corporation", "quantity": 1, "cost_basis": 439.00 } def add_stock_position(): """添加微软持仓到Notion""" print("=" * 60) print("添加微软持仓到Notion数据库") print("=" * 60) print(f"数据库ID: {DATABASE_ID}") print(f"股票代码: {POSITION['symbol']}") print(f"公司名称: {POSITION['name']}") print(f"持仓数量: {POSITION['quantity']}股") print(f"买入成本: ${POSITION['cost_basis']}/股") print("=" * 60) url = "https://api.notion.com/v1/pages" headers = { "Authorization": f"Bearer {NOTION_API_KEY}", "Content-Type": "application/json", "Notion-Version": "2022-06-28" } # 计算总成本 total_cost = POSITION["quantity"] * POSITION["cost_basis"] payload = { "parent": { "database_id": DATABASE_ID }, "properties": { "股票代码": { "title": [ { "text": { "content": POSITION["symbol"] } } ] }, "公司名称": { "rich_text": [ { "text": { "content": POSITION["name"] } } ] }, "持仓数量": { "number": POSITION["quantity"] }, "买入成本": { "number": POSITION["cost_basis"] }, "总成本": { "number": total_cost }, "最后更新": { "date": { "start": datetime.now().isoformat() } }, "备注": { "rich_text": [ { "text": { "content": "初始添加持仓" } } ] } } } try: response = requests.post(url, headers=headers, json=payload) if response.status_code == 200: result = response.json() print("\n✅ 微软持仓添加成功!") print("=" * 60) print(f"持仓ID: {result['id']}") print(f"股票代码: {POSITION['symbol']}") print(f"公司名称: {POSITION['name']}") print(f"持仓数量: {POSITION['quantity']}股") print(f"买入成本: ${POSITION['cost_basis']}/股") print(f"总成本: ${total_cost}") print("=" * 60) return True else: print(f"\n❌ 添加失败") print(f"状态码: {response.status_code}") print(f"错误信息: {response.text}") return False except Exception as e: print(f"\n❌ 发生错误: {e}") return False def main(): """主函数""" print("开始添加微软持仓...") print() success = add_stock_position() if success: print("\n💡 下一步:") print("1. 设置每日价格查询任务") print("2. 配置盈亏报告发送") print("3. 添加S&P 500指数追踪") print("4. 可以继续添加其他持仓") else: print("\n🔧 解决方案:") print("1. 检查Notion API密钥") print("2. 检查数据库ID是否正确") print("3. 检查网络连接") if __name__ == "__main__": main()