Tool not using own notion lib anymore
This commit is contained in:
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
@@ -17,7 +17,9 @@
|
|||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mode": "auto",
|
"mode": "auto",
|
||||||
"program": "${workspaceFolder}/src/helper/poo_recorder_helper/main.go",
|
"program": "${workspaceFolder}/src/helper/poo_recorder_helper/main.go",
|
||||||
"args": ["reverse"]
|
"args": [
|
||||||
|
"reverse"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -8,4 +8,7 @@ require (
|
|||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/spf13/cobra v1.8.1 // indirect
|
github.com/spf13/cobra v1.8.1 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // 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
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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/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 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
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/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=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -6,11 +6,13 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jomei/notionapi"
|
"github.com/jomei/notionapi"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/t-liu93/home-automation-backend/util/notion"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
var notionToken string
|
var notionToken string
|
||||||
@@ -20,31 +22,70 @@ var notionTableId string
|
|||||||
var reverseCmd = &cobra.Command{
|
var reverseCmd = &cobra.Command{
|
||||||
Use: "reverse",
|
Use: "reverse",
|
||||||
Short: "Reverse given poo recording table",
|
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{}
|
rows := []notionapi.Block{}
|
||||||
fmt.Println("Reverse table ID: ", notionTableId)
|
fmt.Println("Reverse table ID: ", notionTableId)
|
||||||
notion.Init(notionToken)
|
block, err := client.Block.Get(context.Background(), notionapi.BlockID(notionTableId))
|
||||||
headerBlock := <-notion.AGetBlockChildren(notionTableId, "", 100)
|
if err != nil {
|
||||||
headerId := headerBlock.Children[0].GetID()
|
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), ¬ionapi.Pagination{
|
||||||
|
StartCursor: "",
|
||||||
|
PageSize: 100,
|
||||||
|
})
|
||||||
|
headerId := headerBlock.Results[0].GetID()
|
||||||
nextCursor := headerId.String()
|
nextCursor := headerId.String()
|
||||||
hasMore := true
|
hasMore := true
|
||||||
for hasMore {
|
for hasMore {
|
||||||
blockChildren := <-notion.AGetBlockChildren(notionTableId, nextCursor, 100)
|
blockChildren, _ := client.Block.GetChildren(context.Background(), notionapi.BlockID(notionTableId), ¬ionapi.Pagination{
|
||||||
rows = append(rows, blockChildren.Children...)
|
StartCursor: notionapi.Cursor(nextCursor),
|
||||||
|
PageSize: 100,
|
||||||
|
})
|
||||||
|
rows = append(rows, blockChildren.Results...)
|
||||||
hasMore = blockChildren.HasMore
|
hasMore = blockChildren.HasMore
|
||||||
nextCursor = blockChildren.NextCursor
|
nextCursor = blockChildren.NextCursor
|
||||||
}
|
}
|
||||||
rows = rows[1:]
|
rows = rows[1:]
|
||||||
rowsR := reverseTable(rows)
|
rowsR := reverseTable(rows)
|
||||||
for _, row := range rowsR {
|
nrRowsToDelete := len(rowsR)
|
||||||
notion.GetClient().Block.Delete(context.Background(), row.GetID())
|
for index, row := range rowsR {
|
||||||
fmt.Println("Deleted row: ", row.GetID())
|
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)
|
time.Sleep(400 * time.Millisecond)
|
||||||
}
|
}
|
||||||
after := headerId
|
after := headerId
|
||||||
|
fmt.Println("Writing rows back to table")
|
||||||
for len(rowsR) > 0 {
|
for len(rowsR) > 0 {
|
||||||
var rowsToWrite []notionapi.Block
|
var rowsToWrite []notionapi.Block
|
||||||
if len(rowsR) > 100 {
|
if len(rowsR) > 100 {
|
||||||
@@ -52,13 +93,12 @@ func reverseRun(cmd *cobra.Command, args []string) {
|
|||||||
} else {
|
} else {
|
||||||
rowsToWrite = rowsR
|
rowsToWrite = rowsR
|
||||||
}
|
}
|
||||||
res, err := notion.GetClient().Block.AppendChildren(context.Background(), notionapi.BlockID(notionTableId), ¬ionapi.AppendBlockChildrenRequest{
|
client.Block.AppendChildren(context.Background(), notionapi.BlockID(notionTableId), ¬ionapi.AppendBlockChildrenRequest{
|
||||||
After: after,
|
After: after,
|
||||||
Children: rowsToWrite,
|
Children: rowsToWrite,
|
||||||
})
|
})
|
||||||
after = rowsToWrite[len(rowsToWrite)-1].GetID()
|
after = rowsToWrite[len(rowsToWrite)-1].GetID()
|
||||||
rowsR = rowsR[len(rowsToWrite):]
|
rowsR = rowsR[len(rowsToWrite):]
|
||||||
fmt.Println(res, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright © 2024 Tianyu Liu
|
Copyright © 2024 Tianyu Liu
|
||||||
|
|
||||||
*/
|
*/
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
@@ -10,18 +9,10 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// rootCmd represents the base command when called without any subcommands
|
// rootCmd represents the base command when called without any subcommands
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "poo_recorder_helper",
|
Use: "poo_recorder_helper",
|
||||||
Short: "A brief description of your application",
|
Short: "Poo recorder helper executables.",
|
||||||
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.`,
|
|
||||||
// Uncomment the following line if your bare application
|
// Uncomment the following line if your bare application
|
||||||
// has an action associated with it:
|
// has an action associated with it:
|
||||||
// Run: func(cmd *cobra.Command, args []string) { },
|
// Run: func(cmd *cobra.Command, args []string) { },
|
||||||
@@ -45,7 +36,4 @@ func init() {
|
|||||||
|
|
||||||
// Cobra also supports local flags, which will only run
|
// Cobra also supports local flags, which will only run
|
||||||
// when this action is called directly.
|
// when this action is called directly.
|
||||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user