spur-chatbot / read_notion.py
snehasquasher's picture
Upload folder using huggingface_hub
b0d4092
import requests
from apiKey import *
from Constants import *
NOTION_TOKEN = NOTION_API_KEY
DATABASE_ID = NOTION_DB
headers = {
"Authorization": "Bearer " + NOTION_TOKEN,
"Content-Type": "application/json",
"Notion-Version": "2022-06-28",
}
def get_pages(num_pages=None):
"""
If num_pages is None, get all pages, otherwise just the defined number.
"""
url = f"https://api.notion.com/v1/databases/{DATABASE_ID}/query"
get_all = num_pages is None
page_size = MAX_PAGES_TO_READ if get_all else num_pages
payload = {"page_size": page_size}
response = requests.post(url, json=payload, headers=headers)
data = response.json()
# Comment this out to dump all data to a file
# import json
# with open('db.json', 'w', encoding='utf8') as f:
# json.dump(data, f, ensure_ascii=False, indent=4)
results = data["results"]
while data["has_more"] and get_all:
payload = {"page_size": page_size, "start_cursor": data["next_cursor"]}
url = f"https://api.notion.com/v1/databases/{DATABASE_ID}/query"
response = requests.post(url, json=payload, headers=headers)
data = response.json()
results.extend(data["results"])
''' for r in results :
print(r)'''
return results
#get_pages()