Tool not using own notion lib anymore

This commit is contained in:
2024-09-10 15:51:18 +02:00
parent ea9c650f82
commit 322905809c
5 changed files with 66 additions and 27 deletions

4
.vscode/launch.json vendored
View File

@@ -17,7 +17,9 @@
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/src/helper/poo_recorder_helper/main.go",
"args": ["reverse"]
"args": [
"reverse"
]
}
]
}

View File

@@ -8,4 +8,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
)

View File

@@ -8,5 +8,11 @@ github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -6,11 +6,13 @@ package cmd
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/jomei/notionapi"
"github.com/spf13/cobra"
"github.com/t-liu93/home-automation-backend/util/notion"
"golang.org/x/term"
)
var notionToken string
@@ -20,31 +22,70 @@ var notionTableId string
var reverseCmd = &cobra.Command{
Use: "reverse",
Short: "Reverse given poo recording table",
Run: reverseRun,
Long: `Reverse the given poo recording table. Provide the Notion API token and the table ID to reverse.
The Notion API token can be obtained from https://www.notion.so/my-integrations. The table ID can be obtained from the URL of the table.
The token and table ID will be input in the following prompt.
`,
Run: readCredentials,
}
func reverseRun(cmd *cobra.Command, args []string) {
func readCredentials(cmd *cobra.Command, args []string) {
if notionToken == "" || notionTableId == "" {
fmt.Print("Enter Notion API token: ")
pw, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatalf("failed to read NOTION API Token: %v", err)
}
notionToken = string(pw)
fmt.Print("\nEnter Notion table ID: ")
tableId, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatalf("failed to read NOTION table ID: %v", err)
}
notionTableId = string(tableId)
}
reverseRun()
}
func reverseRun() {
client := notionapi.NewClient(notionapi.Token(notionToken))
rows := []notionapi.Block{}
fmt.Println("Reverse table ID: ", notionTableId)
notion.Init(notionToken)
headerBlock := <-notion.AGetBlockChildren(notionTableId, "", 100)
headerId := headerBlock.Children[0].GetID()
block, err := client.Block.Get(context.Background(), notionapi.BlockID(notionTableId))
if err != nil {
log.Fatalf("Failed to get table detail: %v", err)
}
if block.GetType().String() != "table" {
log.Fatalf("Block ID %s is not a table", notionTableId)
}
headerBlock, _ := client.Block.GetChildren(context.Background(), notionapi.BlockID(notionTableId), &notionapi.Pagination{
StartCursor: "",
PageSize: 100,
})
headerId := headerBlock.Results[0].GetID()
nextCursor := headerId.String()
hasMore := true
for hasMore {
blockChildren := <-notion.AGetBlockChildren(notionTableId, nextCursor, 100)
rows = append(rows, blockChildren.Children...)
blockChildren, _ := client.Block.GetChildren(context.Background(), notionapi.BlockID(notionTableId), &notionapi.Pagination{
StartCursor: notionapi.Cursor(nextCursor),
PageSize: 100,
})
rows = append(rows, blockChildren.Results...)
hasMore = blockChildren.HasMore
nextCursor = blockChildren.NextCursor
}
rows = rows[1:]
rowsR := reverseTable(rows)
for _, row := range rowsR {
notion.GetClient().Block.Delete(context.Background(), row.GetID())
fmt.Println("Deleted row: ", row.GetID())
nrRowsToDelete := len(rowsR)
for index, row := range rowsR {
client.Block.Delete(context.Background(), row.GetID())
if index%10 == 0 || index == nrRowsToDelete-1 {
fmt.Printf("Deleted %d/%d rows\n", index, nrRowsToDelete)
}
time.Sleep(400 * time.Millisecond)
}
after := headerId
fmt.Println("Writing rows back to table")
for len(rowsR) > 0 {
var rowsToWrite []notionapi.Block
if len(rowsR) > 100 {
@@ -52,13 +93,12 @@ func reverseRun(cmd *cobra.Command, args []string) {
} else {
rowsToWrite = rowsR
}
res, err := notion.GetClient().Block.AppendChildren(context.Background(), notionapi.BlockID(notionTableId), &notionapi.AppendBlockChildrenRequest{
client.Block.AppendChildren(context.Background(), notionapi.BlockID(notionTableId), &notionapi.AppendBlockChildrenRequest{
After: after,
Children: rowsToWrite,
})
after = rowsToWrite[len(rowsToWrite)-1].GetID()
rowsR = rowsR[len(rowsToWrite):]
fmt.Println(res, err)
}
}

View File

@@ -1,6 +1,5 @@
/*
Copyright © 2024 Tianyu Liu
*/
package cmd
@@ -10,18 +9,10 @@ import (
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "poo_recorder_helper",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "Poo recorder helper executables.",
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
@@ -45,7 +36,4 @@ func init() {
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}