59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
"""
|
|
Script to view content in your Notion database
|
|
"""
|
|
|
|
import requests
|
|
import json
|
|
|
|
class NotionDatabaseViewer:
|
|
def __init__(self, integration_token="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"):
|
|
self.integration_token = integration_token
|
|
self.headers = {
|
|
"Authorization": f"Bearer {integration_token}",
|
|
"Content-Type": "application/json",
|
|
"Notion-Version": "2022-06-28"
|
|
}
|
|
self.base_url = "https://api.notion.com/v1"
|
|
|
|
def query_database(self, database_id):
|
|
"""Query a specific database to get its content"""
|
|
url = f"{self.base_url}/databases/{database_id}/query"
|
|
response = requests.post(url, headers=self.headers, json={})
|
|
if response.status_code == 200:
|
|
return response.json()
|
|
else:
|
|
print(f"Error querying database: {response.status_code} - {response.text}")
|
|
return None
|
|
|
|
def main():
|
|
# Initialize the viewer
|
|
viewer = NotionDatabaseViewer()
|
|
|
|
# Use the database ID we found earlier
|
|
database_id = "2fbdaaa6-ba07-804a-b48a-ce9c0a107416"
|
|
|
|
print("正在查询数据库内容...")
|
|
result = viewer.query_database(database_id)
|
|
|
|
if result and 'results' in result:
|
|
pages = result['results']
|
|
print(f"在数据库中找到 {len(pages)} 个页面:\n")
|
|
|
|
for page in pages:
|
|
page_id = page['id']
|
|
# Extract the title from the page properties
|
|
title_property = page['properties'].get('名称', {})
|
|
if title_property and 'title' in title_property:
|
|
title_parts = title_property['title']
|
|
page_title = ''.join([part.get('text', {}).get('content', '') for part in title_parts])
|
|
else:
|
|
page_title = "[无标题]"
|
|
|
|
print(f"页面ID: {page_id}")
|
|
print(f"标题: {page_title}")
|
|
print("-" * 50)
|
|
else:
|
|
print("未能检索到数据库内容")
|
|
|
|
if __name__ == "__main__":
|
|
main() |