2021-10-30 22:42:00 -05:00
|
|
|
import std/httpclient, std/json, std/logging, std/options, std/sequtils,
|
|
|
|
std/strutils, std/times
|
|
|
|
import timeutils
|
|
|
|
|
|
|
|
const NOTION_MAX_PAGE_SIZE* = 100
|
2021-11-07 07:18:57 -06:00
|
|
|
const NOTION_DATE_FORMAT = "YYYY-MM-dd"
|
2021-10-30 22:42:00 -05:00
|
|
|
|
|
|
|
proc parseDate(str: string): DateTime =
|
|
|
|
try: result = parseIso8601(str)
|
2021-11-07 07:18:57 -06:00
|
|
|
except: result = times.parse(str, NOTION_DATE_FORMAT)
|
2021-10-30 22:42:00 -05:00
|
|
|
|
|
|
|
## Utility functions for creating Page property values
|
|
|
|
## ---------------------------------------------------
|
|
|
|
|
2021-11-07 07:18:57 -06:00
|
|
|
proc makeDateProp*(d: Option[DateTime]): JsonNode =
|
|
|
|
if d.isSome: return %*{ "date": { "start": format(d.get, NOTION_DATE_FORMAT) } }
|
|
|
|
else: return %*{ "date": nil }
|
|
|
|
|
2021-10-30 22:42:00 -05:00
|
|
|
proc makeDateTimeProp*(d: Option[DateTime]): JsonNode =
|
|
|
|
if d.isSome: return %*{ "date": { "start": formatIso8601(d.get) } }
|
|
|
|
else: return %*{ "date": nil }
|
|
|
|
|
2021-11-07 07:18:57 -06:00
|
|
|
proc makeIntervalProp*(s: Option[DateTime], e: Option[DateTime]): JsonNode =
|
|
|
|
if not s.isSome: result = %*{ "date": nil }
|
|
|
|
|
|
|
|
result = %*{ "date": { "start": formatIso8601(s.get) } }
|
|
|
|
|
|
|
|
if e.isSome: result["date"]["end"] = %formatIso8601(e.get)
|
|
|
|
|
2021-10-30 22:42:00 -05:00
|
|
|
proc makeMultiSelectProp*(values: seq[string]): JsonNode =
|
2021-11-07 07:18:57 -06:00
|
|
|
if values.len == 0: return %*{ "multi_select": [] }
|
|
|
|
return %*{ "multi_select": values.mapIt(%*{ "name": it }) }
|
2021-10-30 22:42:00 -05:00
|
|
|
|
|
|
|
proc makeRelationProp*(ids: seq[string]): JsonNode =
|
2021-11-07 07:18:57 -06:00
|
|
|
if ids.len == 0: return %*{ "relation": [] }
|
|
|
|
return %*{ "relation": ids.mapIt(%*{ "id": it }) }
|
2021-10-30 22:42:00 -05:00
|
|
|
|
|
|
|
proc makeSelectProp*(value: string): JsonNode =
|
|
|
|
return %*{ "select": { "name": value } }
|
|
|
|
|
|
|
|
proc makeTextProp*(propType: string, value: string): JsonNode =
|
|
|
|
return %*{
|
|
|
|
propType: [
|
|
|
|
{
|
|
|
|
"type": "text",
|
|
|
|
"text": { "content": value }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
## Utility functions for reading Page property values
|
|
|
|
## --------------------------------------------------
|
|
|
|
|
|
|
|
proc getPropNode(page: JsonNode, propType, propName: string): JsonNode =
|
|
|
|
result = page{"properties", propName, propType}
|
|
|
|
|
|
|
|
if isNil(result):
|
|
|
|
raise newException(ValueError,
|
|
|
|
"could not find a " & propType & " property named '" & propName &
|
|
|
|
"' in the Notion page (id: " & page["id"].getStr & ")")
|
|
|
|
|
|
|
|
proc getEmail*(page: JsonNode, propName: string): string =
|
|
|
|
let propNode = page.getPropNode("email", propName)
|
|
|
|
return propNode.getStr
|
|
|
|
|
|
|
|
proc getMultiSelect*(page: JsonNode, propName: string): seq[string] =
|
|
|
|
let propNode = page.getPropNode("multi_select", propName)
|
|
|
|
return propNode.getElems.mapIt(it["name"].getStr)
|
|
|
|
|
|
|
|
proc getPhone*(page: JsonNode, propName: string): string =
|
|
|
|
let propNode = page.getPropNode("phone_number", propName)
|
|
|
|
return propNode.getStr
|
|
|
|
|
|
|
|
proc getRelationIds*(page: JsonNode, propName: string): seq[string] =
|
|
|
|
let propNode = page.getPropNode("relation", propName)
|
|
|
|
return propNode.getElems.mapIt(it["id"].getStr)
|
|
|
|
|
|
|
|
proc getText*(page: JsonNode, propName: string): string =
|
|
|
|
let propNode = page.getPropNode("rich_text", propName)
|
|
|
|
if propNode.len == 0: return ""
|
|
|
|
return propNode[0]["plain_text"].getStr
|
|
|
|
|
|
|
|
proc getTitle*(page: JsonNode, propName: string): string =
|
|
|
|
let propNode = page.getPropNode("title", propName)
|
|
|
|
if propNode.len == 0: return ""
|
|
|
|
return propNode[0]["plain_text"].getStr
|
|
|
|
|
|
|
|
proc getSelect*(page: JsonNode, propName: string): string =
|
|
|
|
let propNode = page.getPropNode("select", propName)
|
|
|
|
if propNode.kind == JNull: return ""
|
|
|
|
return propNode["name"].getStr
|
|
|
|
|
|
|
|
proc getDateTime*(page: JsonNode, propName: string): Option[DateTime] =
|
|
|
|
let propNode = page.getPropNode("date", propName)
|
|
|
|
if propNode.kind == JNull: result = none[DateTime]()
|
|
|
|
else: result = some(parseDate(propNode["start"].getStr))
|
|
|
|
|
|
|
|
proc newNotionClient*(apiVersion, integrationToken: string): HttpClient =
|
|
|
|
return newHttpClient(headers = newHttpHeaders([
|
|
|
|
("Content-Type", "application/json"),
|
|
|
|
("Authorization", "Bearer " & integrationToken),
|
|
|
|
("Notion-Version", apiVersion)
|
|
|
|
], true))
|
|
|
|
|
|
|
|
proc fetchAllPages*(
|
|
|
|
http: HttpClient,
|
|
|
|
url: string,
|
|
|
|
bodyTemplate = %*{ "page_size": NOTION_MAX_PAGE_SIZE}): seq[JsonNode] =
|
|
|
|
|
|
|
|
result = @[]
|
|
|
|
var nextCursor: string = ""
|
|
|
|
|
|
|
|
while true:
|
|
|
|
let body = parseJson($bodyTemplate)
|
|
|
|
if not nextCursor.isEmptyOrWhitespace: body["start_cursor"] = %nextCursor
|
|
|
|
|
|
|
|
debug "Fetching pages from database:\n\tPOST " & url & "\n\t" & $body
|
|
|
|
let jsonResp = parseJson(http.postContent(url, $body))
|
|
|
|
|
|
|
|
result = result & jsonResp["results"].getElems
|
|
|
|
if jsonResp.hasKey("next_cursor") and jsonResp["next_cursor"].kind != JNull:
|
|
|
|
nextCursor = jsonResp["next_cursor"].getStr
|
|
|
|
|
|
|
|
if nextCursor.isEmptyOrWhitespace: break
|