Skip to content

Instantly share code, notes, and snippets.

@shiyuugohirao
Last active February 21, 2024 03:15
Show Gist options
  • Save shiyuugohirao/8ead9f0d43f4d52ebf1fefa2ac2f2fc3 to your computer and use it in GitHub Desktop.
Save shiyuugohirao/8ead9f0d43f4d52ebf1fefa2ac2f2fc3 to your computer and use it in GitHub Desktop.
simply use to send Notion for e.g. log
/*
ofxNotion
by shugohirao
simply use to send Notion for e.g. log
doc : https://shiyuugo.notion.site/ofxNotion-91b51c9d9fee458e94c1e9261dc7b294?pvs=4
*/
#pragma once
#include "ofMain.h"
namespace ofxNotion {
static string DatabaseID;
static string SecretToken;
static ofHttpRequest NotionRequest;
static string StampFormat = "%Y/%m/%d %H:%M:%S:%i";
static string LogDateFormat = "%Y%m%dT%H%M%S.%i+0900";
inline void setupNotion(string databaseID, string secretToken) {
DatabaseID = databaseID;
SecretToken = secretToken;
NotionRequest.url = "https://api.notion.com/v1/pages";
NotionRequest.method = ofHttpRequest::POST;
NotionRequest.contentType = "application/json";
NotionRequest.headers["Authorization"] = "Bearer " + SecretToken;
NotionRequest.headers["Notion-Version"] = " 2022-06-28 ";
NotionRequest.timeoutSeconds = 5000;
// ofLogNotice("ofxNotion", "setup ofxNotion\nDatabaseID: %s\nSecretToken: %s", DatabaseID.c_str(), SecretToken.c_str());
}
inline void setupNotionFromFile(string path) {
ofBuffer buff = ofFile(path).readToBuffer();
string databaseID = ofTrim(buff.getNextLine());
string secretToken = ofTrim(buff.getNextLine());
setupNotion(databaseID, secretToken);
}
inline void sendLogToNotion(ofLogLevel level, const string& message) {
thread t([=]() {
string select, color;
switch (level) {
case OF_LOG_VERBOSE: select = "VERBOSE", color = "gray"; break;
case OF_LOG_NOTICE: select = "NOTICE", color = "green"; break;
case OF_LOG_WARNING: select = "WARNING", color = "yellow"; break;
case OF_LOG_ERROR: select = "ERROR", color = "pink"; break;
case OF_LOG_FATAL_ERROR: select = "FATAL_ERROR", color = "red"; break;
}
ofJson json;
json["parent"]["database_id"] = DatabaseID;
json["properties"]["log"] = ofJson::parse("{\"title\": [ {\"text\": { \"content\": \"" + ofGetTimestampString(StampFormat) + "\"}}] }");
json["properties"]["date"] = ofJson::parse("{\"date\":{\"start\":\"" + ofGetTimestampString(LogDateFormat) + "\"}}");
json["properties"]["tag"] = ofJson::parse("{\"select\":{ \"name\":\"" + select + "\", \"color\":\"" + color + "\"}}");
json["properties"]["message"] = ofJson::parse("{\"rich_text\": [ {\"text\": { \"content\": \"" + message + "\"}}] }");
NotionRequest.body = json.dump();
ofHttpResponse res = ofURLFileLoader().handleRequest(NotionRequest);
if (res.status == 200)
ofLogVerbose("ofxNotion", "send log: %s, message:%s\nstatus:%d\ndata:%s", select.c_str(), message.c_str(), res.status, res.data.getText().c_str());
else
ofLogError("ofxNotion", "send log: %s, message:%s\nstatus:%d\ndata:%s", select.c_str(), message.c_str(), res.status, res.data.getText().c_str());
});
t.detach();
}
inline void sendLogToNotion(ofLogLevel level, const char* format, ...) {
va_list args;
va_start(args, format);
string message = ofVAArgsToString(format, args);
va_end(args);
sendLogToNotion(level, message);
}
}; // namespace ofxNotion
@shiyuugohirao
Copy link
Author

ofxNotion
simply use to send Notion for e.g. log
doc : https://shiyuugo.notion.site/ofxNotion-91b51c9d9fee458e94c1e9261dc7b294?pvs=4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment