161 lines
4.1 KiB
Python
161 lines
4.1 KiB
Python
#!/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
|
|
|
|
# 持仓数据示例(需要用户提供实际数据)
|
|
# 请修改为你的实际持仓
|
|
POSITIONS = [
|
|
# 示例持仓,请替换为你的实际数据
|
|
# {
|
|
# "symbol": "AAPL",
|
|
# "name": "Apple Inc.",
|
|
# "quantity": 100,
|
|
# "cost_basis": 150.00
|
|
# },
|
|
# {
|
|
# "symbol": "TSLA",
|
|
# "name": "Tesla Inc.",
|
|
# "quantity": 50,
|
|
# "cost_basis": 200.00
|
|
# },
|
|
]
|
|
|
|
def add_stock_position(position):
|
|
"""添加单个持仓到Notion"""
|
|
|
|
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(f"✅ 已添加: {position['symbol']} - {position['name']}")
|
|
return True
|
|
else:
|
|
print(f"❌ 添加失败: {position['symbol']} - {response.text}")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"❌ 错误: {position['symbol']} - {e}")
|
|
return False
|
|
|
|
def main():
|
|
"""主函数"""
|
|
print("=" * 60)
|
|
print("添加美股持仓到Notion数据库")
|
|
print("=" * 60)
|
|
print(f"数据库ID: {DATABASE_ID}")
|
|
print(f"持仓数量: {len(POSITIONS)}")
|
|
print("=" * 60)
|
|
|
|
if not POSITIONS:
|
|
print("\n⚠️ 请先配置持仓数据!")
|
|
print("\n修改 POSITIONS 列表,添加你的实际持仓:")
|
|
print("""
|
|
POSITIONS = [
|
|
{
|
|
"symbol": "AAPL",
|
|
"name": "Apple Inc.",
|
|
"quantity": 100,
|
|
"cost_basis": 150.00
|
|
},
|
|
{
|
|
"symbol": "TSLA",
|
|
"name": "Tesla Inc.",
|
|
"quantity": 50,
|
|
"cost_basis": 200.00
|
|
},
|
|
]
|
|
""")
|
|
return
|
|
|
|
print("\n开始添加持仓...")
|
|
print()
|
|
|
|
success_count = 0
|
|
for position in POSITIONS:
|
|
if add_stock_position(position):
|
|
success_count += 1
|
|
|
|
print("\n" + "=" * 60)
|
|
print(f"完成!成功添加 {success_count}/{len(POSITIONS)} 个持仓")
|
|
print("=" * 60)
|
|
|
|
if success_count > 0:
|
|
print("\n💡 下一步:")
|
|
print("1. 设置每日价格查询任务")
|
|
print("2. 配置盈亏报告发送")
|
|
print("3. 添加S&P 500指数追踪")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|