Spaces:
Sleeping
Sleeping
| import requests | |
| from datetime import datetime, timezone | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv(override=True) | |
| NOTION_TOKEN = os.getenv("NOTION_TOKEN") | |
| DATABASE_ID = os.getenv("DATABASE_ID") | |
| 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 = 100 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"]) | |
| return results | |
| def create_page(data: dict): | |
| create_url = "https://api.notion.com/v1/pages" | |
| payload = {"parent": {"database_id": DATABASE_ID}, "properties": data} | |
| res = requests.post(create_url, headers=headers, json=payload) | |
| # print(res.status_code) | |
| return res | |
| # source = "Test Title" | |
| # url = "Test@amazon.com" | |
| # status = "Not Applied" # status type field cannot be updated via API | |
| # data = { | |
| # "Source": {"title": [{"text": {"content":source}}]}, | |
| # "URL": {"url": url} | |
| # } | |
| # pages = get_pages() | |
| # for page in pages: | |
| # print(page) | |
| # page_id = page["id"] | |
| # props = page["properties"] | |
| # source = props["Source"]["title"][0]["plain_text"] | |
| # url = props["URL"]["url"] | |
| # published = props["Status"]["status"]["name"] | |
| # print(source, url, published) | |
| # # print(create_page(data)) |