Skip to content

Commit f96c678

Browse files
Created "notion" package
1 parent 90f258e commit f96c678

File tree

5 files changed

+171
-103
lines changed

5 files changed

+171
-103
lines changed

cmd/updater/main.go

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package main
22

33
import (
4-
"context"
54
"fmt"
65
"log"
76
"net/http"
87
"notion-igdb-autocomplete/config"
98
"notion-igdb-autocomplete/igdb"
9+
"notion-igdb-autocomplete/notion"
1010
"sort"
11-
"time"
1211

1312
"github.com/agnivade/levenshtein"
1413
"github.com/gin-gonic/gin"
15-
"github.com/jomei/notionapi"
1614
)
1715

1816
type body struct {
@@ -33,7 +31,7 @@ func main() {
3331
}
3432
log.Println("Successfully created IGDB client!")
3533

36-
notionClient := notionapi.NewClient(notionapi.Token(config.NotionAPISecret))
34+
notionClient := notion.NewClient(config.NotionAPISecret)
3735
log.Println("Successfully created Notion client!")
3836

3937
server := gin.Default()
@@ -52,7 +50,7 @@ func main() {
5250
return
5351
}
5452

55-
updatedPage, err := updateNotionPage(payload.PageID, game, notionClient)
53+
updatedPage, err := notionClient.Page(payload.PageID).Update(game)
5654
if err != nil {
5755
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"message": err.Error()})
5856
return
@@ -81,65 +79,6 @@ func searchIgdbGame(gameName string, client *igdb.Client) (*igdb.Game, error) {
8179
return findBestGame(gameName, results), nil
8280
}
8381

84-
func updateNotionPage(pageID string, game *igdb.Game, notionClient *notionapi.Client) (*notionapi.Page, error) {
85-
releaseDate := notionapi.Date(time.Unix(game.ReleaseDate, 0))
86-
platforms := game.NotionPlatforms()
87-
genres := game.NotionGenres()
88-
franchises := game.NotionFranchises()
89-
90-
updateReq := notionapi.PageUpdateRequest{
91-
Cover: &notionapi.Image{
92-
Type: "external",
93-
External: &notionapi.FileObject{
94-
URL: game.CoverURL(),
95-
},
96-
},
97-
Properties: notionapi.Properties{
98-
"Title": notionapi.TitleProperty{
99-
Type: notionapi.PropertyTypeTitle,
100-
Title: []notionapi.RichText{
101-
{Text: &notionapi.Text{Content: game.Name}},
102-
},
103-
},
104-
"Release date": notionapi.DateProperty{
105-
Type: notionapi.PropertyTypeDate,
106-
107-
Date: &notionapi.DateObject{
108-
Start: &releaseDate,
109-
},
110-
},
111-
},
112-
}
113-
114-
if len(platforms) > 0 {
115-
updateReq.Properties["Platforms"] = notionapi.MultiSelectProperty{
116-
Type: notionapi.PropertyTypeMultiSelect,
117-
MultiSelect: game.NotionPlatforms(),
118-
}
119-
}
120-
121-
if len(franchises) > 0 {
122-
updateReq.Properties["Franchises"] = notionapi.MultiSelectProperty{
123-
Type: notionapi.PropertyTypeMultiSelect,
124-
MultiSelect: franchises,
125-
}
126-
}
127-
128-
if len(genres) > 0 {
129-
updateReq.Properties["Genres"] = notionapi.MultiSelectProperty{
130-
Type: notionapi.PropertyTypeMultiSelect,
131-
MultiSelect: game.NotionGenres(),
132-
}
133-
}
134-
135-
page, err := notionClient.Page.Update(context.Background(), notionapi.PageID(pageID), &updateReq)
136-
if err != nil {
137-
return nil, err
138-
}
139-
140-
return page, nil
141-
}
142-
14382
type ComparedGames []ComparedGame
14483
type ComparedGame struct {
14584
Game igdb.Game

cmd/watcher/main.go

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package main
22

33
import (
4-
"context"
54
"encoding/json"
65
"fmt"
76
"io"
87
"log"
98
"net/http"
109
"notion-igdb-autocomplete/config"
10+
"notion-igdb-autocomplete/notion"
1111
"strings"
1212
"time"
1313

1414
"github.com/jomei/notionapi"
1515
)
1616

17+
type updateRequestBody struct {
18+
PageID string `json:"page_id,required"`
19+
Search string `json:"search,required"`
20+
}
21+
1722
func main() {
1823
config, err := config.Load()
1924
if err != nil {
@@ -22,21 +27,24 @@ func main() {
2227
log.Println("Successfully loaded config!")
2328
}
2429

30+
notionClient := notion.NewClient(config.NotionAPISecret)
31+
log.Println("Successfully created Notion client!")
32+
2533
titleCleaner := strings.NewReplacer("{{", "", "}}", "")
2634

2735
log.Println("Looking for pages to update...")
2836
for range time.Tick(time.Duration(config.WatcherTickDelay)) {
29-
pages, err := fetchPages(config.NotionAPISecret, config.NotionPageID)
37+
entries, err := notionClient.Database(config.NotionPageID).GetEntries()
3038
if err != nil {
3139
log.Fatalf("Unable to fetch pages: %s\n", err)
3240
}
3341

34-
for _, obj := range pages {
35-
id := obj.ID
36-
title := obj.Properties["Title"].(*notionapi.TitleProperty).Title[0].Text.Content
42+
for _, entry := range entries {
43+
id := entry.ID
44+
title := entry.Properties["Title"].(*notionapi.TitleProperty).Title[0].Text.Content
3745
cleanTitle := titleCleaner.Replace(title)
3846

39-
err = callUpdater(config.UpdaterURL(), updaterBody{PageID: id.String(), Search: cleanTitle})
47+
err = callUpdater(config.UpdaterURL(), updateRequestBody{PageID: id.String(), Search: cleanTitle})
4048
if err != nil {
4149
log.Printf("Unable to update page '%s': %s\n", id, err)
4250
} else {
@@ -46,39 +54,7 @@ func main() {
4654
}
4755
}
4856

49-
func fetchPages(apiSecret string, databaseID string) ([]notionapi.Page, error) {
50-
notion := notionapi.NewClient(notionapi.Token(apiSecret))
51-
query := &notionapi.DatabaseQueryRequest{
52-
Filter: &notionapi.AndCompoundFilter{
53-
notionapi.PropertyFilter{
54-
Property: "Title",
55-
RichText: &notionapi.TextFilterCondition{
56-
StartsWith: "{{",
57-
},
58-
},
59-
notionapi.PropertyFilter{
60-
Property: "Title",
61-
RichText: &notionapi.TextFilterCondition{
62-
EndsWith: "}}",
63-
},
64-
},
65-
},
66-
}
67-
68-
result, err := notion.Database.Query(context.Background(), notionapi.DatabaseID(databaseID), query)
69-
if err != nil {
70-
return nil, err
71-
}
72-
73-
return result.Results, nil
74-
}
75-
76-
type updaterBody struct {
77-
PageID string `json:"page_id,required"`
78-
Search string `json:"search,required"`
79-
}
80-
81-
func callUpdater(updaterURL string, payload updaterBody) error {
57+
func callUpdater(updaterURL string, payload updateRequestBody) error {
8258
reqBody, err := json.Marshal(payload)
8359
if err != nil {
8460
return err

notion/client.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package notion
2+
3+
import "github.com/jomei/notionapi"
4+
5+
type Client struct {
6+
APIClient notionapi.Client
7+
}
8+
9+
func NewClient(apiSecret string) Client {
10+
return Client{
11+
APIClient: *notionapi.NewClient(notionapi.Token(apiSecret)),
12+
}
13+
}
14+
15+
func (c *Client) Page(id string) *Page {
16+
return NewPage(id, &c.APIClient)
17+
}
18+
19+
func (c *Client) Database(id string) *Database {
20+
return NewDatabase(id, &c.APIClient)
21+
}

notion/database.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package notion
2+
3+
import (
4+
"context"
5+
6+
"github.com/jomei/notionapi"
7+
)
8+
9+
type Database struct {
10+
apiClient *notionapi.Client
11+
Id string
12+
}
13+
14+
func NewDatabase(id string, client *notionapi.Client) *Database {
15+
return &Database{
16+
Id: id,
17+
apiClient: client,
18+
}
19+
}
20+
21+
func (d *Database) GetEntries() ([]notionapi.Page, error) {
22+
query := &notionapi.DatabaseQueryRequest{
23+
Filter: &notionapi.AndCompoundFilter{
24+
notionapi.PropertyFilter{
25+
Property: "Title",
26+
RichText: &notionapi.TextFilterCondition{
27+
StartsWith: "{{",
28+
},
29+
},
30+
notionapi.PropertyFilter{
31+
Property: "Title",
32+
RichText: &notionapi.TextFilterCondition{
33+
EndsWith: "}}",
34+
},
35+
},
36+
},
37+
}
38+
39+
result, err := d.apiClient.Database.Query(context.Background(), notionapi.DatabaseID(d.Id), query)
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
return result.Results, nil
45+
}

notion/page.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package notion
2+
3+
import (
4+
"context"
5+
"notion-igdb-autocomplete/igdb"
6+
"time"
7+
8+
"github.com/jomei/notionapi"
9+
)
10+
11+
type Page struct {
12+
apiClient *notionapi.Client
13+
Id string
14+
}
15+
16+
func NewPage(id string, client *notionapi.Client) *Page {
17+
return &Page{
18+
Id: id,
19+
apiClient: client,
20+
}
21+
}
22+
23+
func (p *Page) Update(game *igdb.Game) (*notionapi.Page, error) {
24+
request := createUpdateRequest(game)
25+
pageID := notionapi.PageID(p.Id)
26+
27+
updatedPage, err := p.apiClient.Page.Update(context.Background(), pageID, &request)
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
return updatedPage, nil
33+
}
34+
35+
func createUpdateRequest(game *igdb.Game) notionapi.PageUpdateRequest {
36+
releaseDate := notionapi.Date(time.Unix(game.ReleaseDate, 0))
37+
platforms := game.NotionPlatforms()
38+
genres := game.NotionGenres()
39+
franchises := game.NotionFranchises()
40+
41+
request := notionapi.PageUpdateRequest{
42+
Cover: &notionapi.Image{
43+
Type: "external",
44+
External: &notionapi.FileObject{
45+
URL: game.CoverURL(),
46+
},
47+
},
48+
Properties: notionapi.Properties{
49+
"Title": notionapi.TitleProperty{
50+
Type: notionapi.PropertyTypeTitle,
51+
Title: []notionapi.RichText{
52+
{Text: &notionapi.Text{Content: game.Name}},
53+
},
54+
},
55+
"Release date": notionapi.DateProperty{
56+
Type: notionapi.PropertyTypeDate,
57+
58+
Date: &notionapi.DateObject{
59+
Start: &releaseDate,
60+
},
61+
},
62+
},
63+
}
64+
65+
if len(platforms) > 0 {
66+
request.Properties["Platforms"] = notionapi.MultiSelectProperty{
67+
Type: notionapi.PropertyTypeMultiSelect,
68+
MultiSelect: game.NotionPlatforms(),
69+
}
70+
}
71+
72+
if len(franchises) > 0 {
73+
request.Properties["Franchises"] = notionapi.MultiSelectProperty{
74+
Type: notionapi.PropertyTypeMultiSelect,
75+
MultiSelect: franchises,
76+
}
77+
}
78+
79+
if len(genres) > 0 {
80+
request.Properties["Genres"] = notionapi.MultiSelectProperty{
81+
Type: notionapi.PropertyTypeMultiSelect,
82+
MultiSelect: game.NotionGenres(),
83+
}
84+
}
85+
86+
return request
87+
}

0 commit comments

Comments
 (0)