Commit 5c915e8410
5c915e84101803bb6712521fcdad2dbdf56c2203
parent: 41ccef0a32
Unsigned
Wraient <wraient.911@gmail.com> · 2024-10-22 16:40 UTC
Add: Token asking
Layout: unified · split
cmd/curd/main.go
+6 −1
| @@ -74,10 +74,15 @@ func main() { |
| 74 | 74 | os.Exit(0) |
| 75 | 75 | |
| 76 | 76 | // Get the token from the token file |
| 77 | | user.Token, err = internal.GetTokenFromFile("/home/wraient/.local/share/curd/token") |
| 77 | user.Token, err = internal.GetTokenFromFile(os.ExpandEnv("$HOME/.local/share/curd/token")) |
| 78 | 78 | if err != nil { |
| 79 | 79 | internal.Log("Error reading token", logFile) |
| 80 | 80 | } |
| 81 | if user.Token == "" { |
| 82 | fmt.Println("No token found, please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") |
| 83 | fmt.Scanln(&user.Token) |
| 84 | internal.WriteTokenToFile(user.Token, os.ExpandEnv("$HOME/.local/share/curd/token")) |
| 85 | } |
| 81 | 86 | |
| 82 | 87 | // Load anime in database |
| 83 | 88 | databaseFile := os.ExpandEnv("$HOME/Projects/curd/.config/curd/curd_history.txt") |
internal/anilist.go
+1
| @@ -46,6 +46,7 @@ func GetAnimeMap(animeList AnimeList) map[string]string { |
| 46 | 46 | |
| 47 | 47 | return animeMap |
| 48 | 48 | } |
| 49 | |
| 49 | 50 | // SearchAnimeAnilist sends the query to AniList and returns a map of title to ID |
| 50 | 51 | func SearchAnimeAnilist(query, token string) (map[string]string, error) { |
| 51 | 52 | url := "https://graphql.anilist.co" |
internal/curd.go
+19
| @@ -170,6 +170,25 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 170 | 170 | } |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | // CreateOrWriteTokenFile creates the token file if it doesn't exist and writes the token to it |
| 174 | func WriteTokenToFile(token string, filePath string) error { |
| 175 | // Extract the directory path |
| 176 | dir := filepath.Dir(filePath) |
| 177 | |
| 178 | // Create all necessary parent directories |
| 179 | if err := os.MkdirAll(dir, 0755); err != nil { |
| 180 | return fmt.Errorf("failed to create directories: %v", err) |
| 181 | } |
| 182 | |
| 183 | // Write the token to the file, creating it if it doesn't exist |
| 184 | err := os.WriteFile(filePath, []byte(token), 0644) |
| 185 | if err != nil { |
| 186 | return fmt.Errorf("failed to write token to file: %v", err) |
| 187 | } |
| 188 | |
| 189 | return nil |
| 190 | } |
| 191 | |
| 173 | 192 | func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string { |
| 174 | 193 | |
| 175 | 194 | // Get episode link |