154 lines
4.3 KiB
Python
154 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
创建美股持仓Notion数据库
|
|
"""
|
|
|
|
import requests
|
|
import json
|
|
|
|
# Notion API 配置
|
|
NOTION_API_KEY = "ntn_c43902219395mirQBetIfYoww1qKCAF14GBRUQeDee29o2"
|
|
PARENT_PAGE_ID = "2fb105ad787380a9bd3dffec4f226e7a" # 父页面 ID
|
|
|
|
def create_stock_portfolio_database():
|
|
"""创建美股持仓数据库"""
|
|
|
|
print("=" * 60)
|
|
print("创建美股持仓Notion数据库")
|
|
print("=" * 60)
|
|
print(f"父页面ID: {PARENT_PAGE_ID}")
|
|
print("=" * 60)
|
|
|
|
# Notion API 端点
|
|
url = "https://api.notion.com/v1/databases"
|
|
|
|
# 请求头
|
|
headers = {
|
|
"Authorization": f"Bearer {NOTION_API_KEY}",
|
|
"Content-Type": "application/json",
|
|
"Notion-Version": "2022-06-28"
|
|
}
|
|
|
|
# 请求体 - 创建数据库
|
|
payload = {
|
|
"parent": {
|
|
"page_id": PARENT_PAGE_ID
|
|
},
|
|
"title": [
|
|
{
|
|
"type": "text",
|
|
"text": {
|
|
"content": "美股持仓数据库"
|
|
}
|
|
}
|
|
],
|
|
"properties": {
|
|
"股票代码": {
|
|
"title": {}
|
|
},
|
|
"公司名称": {
|
|
"rich_text": {}
|
|
},
|
|
"持仓数量": {
|
|
"number": {
|
|
"format": "number"
|
|
}
|
|
},
|
|
"买入成本": {
|
|
"number": {
|
|
"format": "dollar"
|
|
}
|
|
},
|
|
"当前价格": {
|
|
"number": {
|
|
"format": "dollar"
|
|
}
|
|
},
|
|
"总成本": {
|
|
"number": {
|
|
"format": "dollar"
|
|
}
|
|
},
|
|
"当前市值": {
|
|
"number": {
|
|
"format": "dollar"
|
|
}
|
|
},
|
|
"盈亏金额": {
|
|
"number": {
|
|
"format": "dollar"
|
|
}
|
|
},
|
|
"盈亏百分比": {
|
|
"number": {
|
|
"format": "percent"
|
|
}
|
|
},
|
|
"最后更新": {
|
|
"date": {}
|
|
},
|
|
"备注": {
|
|
"rich_text": {}
|
|
}
|
|
}
|
|
}
|
|
|
|
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"数据库名称: 美股持仓数据库")
|
|
print(f"数据库URL: {result['url']}")
|
|
print("=" * 60)
|
|
print("\n数据库字段:")
|
|
print("- 股票代码 (标题)")
|
|
print("- 公司名称 (富文本)")
|
|
print("- 持仓数量 (数字)")
|
|
print("- 买入成本 (数字,美元)")
|
|
print("- 当前价格 (数字,美元)")
|
|
print("- 总成本 (数字,美元)")
|
|
print("- 当前市值 (数字,美元)")
|
|
print("- 盈亏金额 (数字,美元)")
|
|
print("- 盈亏百分比 (数字,百分比)")
|
|
print("- 最后更新 (日期)")
|
|
print("- 备注 (富文本)")
|
|
print("=" * 60)
|
|
return result['id']
|
|
else:
|
|
print(f"\n❌ 创建失败")
|
|
print(f"状态码: {response.status_code}")
|
|
print(f"错误信息: {response.text}")
|
|
return None
|
|
|
|
except Exception as e:
|
|
print(f"\n❌ 发生错误: {e}")
|
|
return None
|
|
|
|
def main():
|
|
"""主函数"""
|
|
print("开始创建美股持仓数据库...")
|
|
print()
|
|
|
|
database_id = create_stock_portfolio_database()
|
|
|
|
if database_id:
|
|
print("\n💡 下一步:")
|
|
print("1. 保存数据库ID到配置文件")
|
|
print("2. 添加持仓数据到数据库")
|
|
print("3. 设置每日价格查询任务")
|
|
print("4. 配置盈亏报告发送")
|
|
else:
|
|
print("\n🔧 解决方案:")
|
|
print("1. 检查Notion API密钥")
|
|
print("2. 检查父页面ID是否正确")
|
|
print("3. 检查网络连接")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|