|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "notion-igdb-autocomplete/config" |
| 9 | + "notion-igdb-autocomplete/igdb" |
| 10 | + "sort" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/agnivade/levenshtein" |
| 14 | + "github.com/gin-gonic/gin" |
| 15 | + "github.com/jomei/notionapi" |
| 16 | +) |
| 17 | + |
| 18 | +type body struct { |
| 19 | + PageID string `json:"page_id,required"` |
| 20 | + Search string `json:"search,required"` |
| 21 | +} |
| 22 | + |
| 23 | +func main() { |
| 24 | + config, err := config.Load() |
| 25 | + if err != nil { |
| 26 | + log.Fatalf("Unable to load config: %s\n", err) |
| 27 | + } |
| 28 | + log.Println("Successfully loaded config!") |
| 29 | + |
| 30 | + igdbClient, err := igdb.NewClient(config.IGDBClientID, config.IGDBSecret) |
| 31 | + if err != nil { |
| 32 | + log.Fatalf("Unable to create IGDB client: %s\n", err) |
| 33 | + } |
| 34 | + log.Println("Successfully created IGDB client!") |
| 35 | + |
| 36 | + notionClient := notionapi.NewClient(notionapi.Token(config.NotionAPISecret)) |
| 37 | + log.Println("Successfully created Notion client!") |
| 38 | + |
| 39 | + server := gin.Default() |
| 40 | + server.PUT("/", func(ctx *gin.Context) { |
| 41 | + var payload body |
| 42 | + |
| 43 | + err := ctx.ShouldBindJSON(&payload) |
| 44 | + if err != nil { |
| 45 | + ctx.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + game, err := searchIgdbGame(payload.Search, &igdbClient) |
| 50 | + if err != nil { |
| 51 | + ctx.JSON(http.StatusNotFound, gin.H{"message": err.Error()}) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + updatedPage, err := updateNotionPage(payload.PageID, game, notionClient) |
| 56 | + if err != nil { |
| 57 | + ctx.JSON(http.StatusUnprocessableEntity, gin.H{"message": err.Error()}) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + ctx.JSON(http.StatusOK, gin.H{"data": fmt.Sprintf("Updated page %s with game %s infos", updatedPage.ID, game.Name)}) |
| 62 | + }) |
| 63 | + |
| 64 | + err = server.Run(fmt.Sprintf("0.0.0.0:%d", config.UpdaterPort)) |
| 65 | + if err != nil { |
| 66 | + log.Fatalf("Unable to start server: %s\n", err) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func searchIgdbGame(gameName string, client *igdb.Client) (*igdb.Game, error) { |
| 71 | + query := igdb.NewSearchQuery(gameName, []string{"name", "platforms.name", "first_release_date", "franchises.name", "genres.name", "cover.image_id"}) |
| 72 | + results, err := client.SearchGame(query) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + if len(results) <= 0 { |
| 78 | + return nil, fmt.Errorf("cannot find game '%s'", gameName) |
| 79 | + } |
| 80 | + |
| 81 | + return findBestGame(gameName, results), nil |
| 82 | +} |
| 83 | + |
| 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: ¬ionapi.Image{ |
| 92 | + Type: "external", |
| 93 | + External: ¬ionapi.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: ¬ionapi.Text{Content: game.Name}}, |
| 102 | + }, |
| 103 | + }, |
| 104 | + "Release date": notionapi.DateProperty{ |
| 105 | + Type: notionapi.PropertyTypeDate, |
| 106 | + |
| 107 | + Date: ¬ionapi.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 | + |
| 143 | +type ComparedGames []ComparedGame |
| 144 | +type ComparedGame struct { |
| 145 | + Game igdb.Game |
| 146 | + Index int |
| 147 | +} |
| 148 | + |
| 149 | +// Implements interface sort.Interface |
| 150 | +func (cg ComparedGames) Len() int { |
| 151 | + return len(cg) |
| 152 | +} |
| 153 | + |
| 154 | +// Implements interface sort.Interface |
| 155 | +func (cg ComparedGames) Swap(i, j int) { |
| 156 | + cg[i], cg[j] = cg[j], cg[i] |
| 157 | +} |
| 158 | + |
| 159 | +// Implements interface sort.Interface |
| 160 | +func (cg ComparedGames) Less(i, j int) bool { |
| 161 | + return cg[i].Index < cg[j].Index |
| 162 | +} |
| 163 | + |
| 164 | +func findBestGame(search string, games igdb.Games) *igdb.Game { |
| 165 | + var comparisons ComparedGames |
| 166 | + |
| 167 | + for _, game := range games { |
| 168 | + comparisons = append(comparisons, ComparedGame{ |
| 169 | + Game: game, |
| 170 | + Index: levenshtein.ComputeDistance(search, game.Name), |
| 171 | + }) |
| 172 | + } |
| 173 | + |
| 174 | + sort.Sort(comparisons) |
| 175 | + |
| 176 | + return &comparisons[0].Game |
| 177 | +} |
0 commit comments