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() {
7474 os.Exit(0)
7575
7676 // 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"))
7878 if err != nil {
7979 internal.Log("Error reading token", logFile)
8080 }
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 }
8186
8287 // Load anime in database
8388 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 {
4646
4747 return animeMap
4848}
49
4950// SearchAnimeAnilist sends the query to AniList and returns a map of title to ID
5051func SearchAnimeAnilist(query, token string) (map[string]string, error) {
5152 url := "https://graphql.anilist.co"
internal/curd.go +19
@@ -170,6 +170,25 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
170170 }
171171}
172172
173// CreateOrWriteTokenFile creates the token file if it doesn't exist and writes the token to it
174func 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
173192func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string {
174193
175194 // Get episode link