Commit 748f9a9885
748f9a98852cafb4db0204f6a537a6f5401d92c5
parent: b0f3a9f184
Unsigned
Wraient <wraient.911@gmail.com> · 2024-10-27 12:39 UTC
Change token from cli or rofi
Layout: unified · split
cmd/curd/main.go
+8 −11
| @@ -62,6 +62,7 @@ func main() { |
| 62 | 62 | addNewAnime := flag.Bool("new", false, "Add new anime") |
| 63 | 63 | rofiSelection := flag.Bool("rofi", false, "Open selection in rofi") |
| 64 | 64 | noRofi := flag.Bool("no-rofi", false, "No rofi") |
| 65 | changeToken := flag.Bool("change-token", false, "Change token") |
| 65 | 66 | updateScript := flag.Bool("u", false, "Update the script") |
| 66 | 67 | editConfig := flag.Bool("e", false, "Edit config") |
| 67 | 68 | subFlag := flag.Bool("sub", false, "Watch sub version") |
| @@ -69,6 +70,7 @@ func main() { |
| 69 | 70 | |
| 70 | 71 | // Custom help/usage function |
| 71 | 72 | flag.Usage = func() { |
| 73 | internal.RestoreScreen() |
| 72 | 74 | fmt.Fprintf(os.Stderr, "Curd is a CLI tool to manage anime playback with advanced features like skipping intro, outro, filler, recap, tracking progress, and integrating with Discord.\n") |
| 73 | 75 | fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) |
| 74 | 76 | flag.PrintDefaults() // This prints the default flag information |
| @@ -91,6 +93,11 @@ func main() { |
| 91 | 93 | } |
| 92 | 94 | } |
| 93 | 95 | |
| 96 | if *changeToken { |
| 97 | internal.ChangeToken(&userCurdConfig, &user) |
| 98 | return |
| 99 | } |
| 100 | |
| 94 | 101 | if *rofiSelection { |
| 95 | 102 | userCurdConfig.RofiSelection = true |
| 96 | 103 | } |
| @@ -117,17 +124,7 @@ func main() { |
| 117 | 124 | internal.Log("Error reading token", logFile) |
| 118 | 125 | } |
| 119 | 126 | if user.Token == "" { |
| 120 | | if userCurdConfig.RofiSelection { |
| 121 | | user.Token, err = internal.GetTokenFromRofi() |
| 122 | | } else { |
| 123 | | fmt.Println("No token found, please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") |
| 124 | | fmt.Scanln(&user.Token) |
| 125 | | } |
| 126 | | if err != nil { |
| 127 | | internal.Log("Error getting user input: "+err.Error(), logFile) |
| 128 | | internal.ExitCurd(err) |
| 129 | | } |
| 130 | | internal.WriteTokenToFile(user.Token, filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "token")) |
| 127 | internal.ChangeToken(&userCurdConfig, &user) |
| 131 | 128 | } |
| 132 | 129 | |
| 133 | 130 | // Load animes in database |
internal/config.go
+15
| @@ -131,6 +131,21 @@ func createDefaultConfig(path string) error { |
| 131 | 131 | return nil |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | func ChangeToken(config *CurdConfig, user *User) { |
| 135 | var err error |
| 136 | if config.RofiSelection { |
| 137 | user.Token, err = GetTokenFromRofi() |
| 138 | } else { |
| 139 | fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") |
| 140 | fmt.Scanln(&user.Token) |
| 141 | } |
| 142 | if err != nil { |
| 143 | Log("Error getting user input: "+err.Error(), logFile) |
| 144 | ExitCurd(err) |
| 145 | } |
| 146 | WriteTokenToFile(user.Token, filepath.Join(os.ExpandEnv(config.StoragePath), "token")) |
| 147 | } |
| 148 | |
| 134 | 149 | // Load config file from disk into a map (key=value format) |
| 135 | 150 | func loadConfigFromFile(path string) (map[string]string, error) { |
| 136 | 151 | file, err := os.Open(path) |
internal/curd.go
+4 −4
| @@ -256,13 +256,13 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA |
| 256 | 256 | user.Id, user.Username, err = GetAnilistUserID(user.Token) |
| 257 | 257 | if err != nil { |
| 258 | 258 | Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile) |
| 259 | | ExitCurd(fmt.Errorf("Failed to get user ID")) |
| 259 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 260 | 260 | } |
| 261 | 261 | } |
| 262 | 262 | anilistUserData, err := GetUserData(user.Token, user.Id) |
| 263 | 263 | if err != nil { |
| 264 | 264 | Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) |
| 265 | | ExitCurd(fmt.Errorf("Failed to get user data")) |
| 265 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 266 | 266 | } |
| 267 | 267 | user.AnimeList = ParseAnimeList(anilistUserData) |
| 268 | 268 | } |
| @@ -274,12 +274,12 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 274 | 274 | user.Id, user.Username, err = GetAnilistUserID(user.Token) |
| 275 | 275 | if err != nil { |
| 276 | 276 | Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile) |
| 277 | | ExitCurd(fmt.Errorf("Failed to get user ID")) |
| 277 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 278 | 278 | } |
| 279 | 279 | anilistUserData, err := GetUserData(user.Token, user.Id) |
| 280 | 280 | if err != nil { |
| 281 | 281 | Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) |
| 282 | | ExitCurd(fmt.Errorf("Failed to get user data")) |
| 282 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 283 | 283 | } |
| 284 | 284 | user.AnimeList = ParseAnimeList(anilistUserData) |
| 285 | 285 | animeListMap := GetAnimeMap(user.AnimeList) |