Commit 9054037964
Unsigned
Layout: unified · split
internal/localTracking.go +45 −22
| @@ -93,33 +93,54 @@ func LocalGetAllAnime(databaseFile string) []Anime { | ||
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | for _, row := range records { |
| 96 | if len(row) != 5 { | |
| 97 | fmt.Printf("Invalid row format: %v\n", row) | |
| 98 | continue | |
| 96 | anime := parseAnimeRow(row) | |
| 97 | if anime != nil { | |
| 98 | animeList = append(animeList, *anime) | |
| 99 | 99 | } |
| 100 | } | |
| 100 | 101 | |
| 101 | anilistID, _ := strconv.Atoi(row[0]) | |
| 102 | watchingEpisode, _ := strconv.Atoi(row[2]) | |
| 103 | playbackTime, _ := strconv.Atoi(row[3]) | |
| 102 | return animeList | |
| 103 | } | |
| 104 | 104 | |
| 105 | anime := Anime{ | |
| 106 | AnilistId: anilistID, | |
| 107 | AllanimeId: row[1], | |
| 108 | Ep: Episode{ | |
| 109 | Number: watchingEpisode, | |
| 110 | Player: playingVideo{ | |
| 111 | PlaybackTime: playbackTime, | |
| 112 | }, | |
| 113 | }, | |
| 114 | Title: AnimeTitle{ | |
| 115 | English: row[4], | |
| 116 | Romaji: row[4], | |
| 105 | // Function to parse a single row of anime data | |
| 106 | func parseAnimeRow(row []string) *Anime { | |
| 107 | if len(row) < 5 { | |
| 108 | fmt.Printf("Invalid row format: %v\n", row) | |
| 109 | return nil | |
| 110 | } | |
| 111 | ||
| 112 | anilistID, _ := strconv.Atoi(row[0]) | |
| 113 | watchingEpisode, _ := strconv.Atoi(row[2]) | |
| 114 | playbackTime, _ := strconv.Atoi(row[3]) | |
| 115 | ||
| 116 | anime := &Anime{ | |
| 117 | AnilistId: anilistID, | |
| 118 | AllanimeId: row[1], | |
| 119 | Ep: Episode{ | |
| 120 | Number: watchingEpisode, | |
| 121 | Player: playingVideo{ | |
| 122 | PlaybackTime: playbackTime, | |
| 117 | 123 | }, |
| 124 | }, | |
| 125 | Title: AnimeTitle{ | |
| 126 | English: row[4], | |
| 127 | Romaji: row[4], | |
| 128 | }, | |
| 129 | } | |
| 130 | ||
| 131 | // Check if there's an extra field for total episodes | |
| 132 | if len(row) == 6 { | |
| 133 | totalEpisodes, _ := strconv.Atoi(row[5]) | |
| 134 | if watchingEpisode == totalEpisodes { | |
| 135 | // If watching episode equals total episodes, don't include the total episodes field | |
| 136 | return anime | |
| 118 | 137 | } |
| 119 | animeList = append(animeList, anime) | |
| 138 | // If not equal, you might want to store this information somewhere | |
| 139 | // For now, we'll just print it | |
| 140 | // fmt.Printf("Anime %s has %d total episodes\n", anime.Title.English, totalEpisodes) | |
| 120 | 141 | } |
| 121 | 142 | |
| 122 | return animeList | |
| 143 | return anime | |
| 123 | 144 | } |
| 124 | 145 | |
| 125 | 146 | // Function to get the anime name (English or Romaji) from an Anime struct |
| @@ -131,7 +152,7 @@ func getAnimeName(anime Anime) string { | ||
| 131 | 152 | } |
| 132 | 153 | |
| 133 | 154 | // Function to update or add a new anime entry |
| 134 | func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, playbackTime int, animeName string) { | |
| 155 | func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, playbackTime int, animeName string) error { | |
| 135 | 156 | // Read existing entries |
| 136 | 157 | animeList := LocalGetAllAnime(databaseFile) |
| 137 | 158 | |
| @@ -170,7 +191,7 @@ func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, wat | ||
| 170 | 191 | file, err := os.Create(databaseFile) |
| 171 | 192 | if err != nil { |
| 172 | 193 | fmt.Println("Error creating file:", err) |
| 173 | return | |
| 194 | return err | |
| 174 | 195 | } |
| 175 | 196 | defer file.Close() |
| 176 | 197 | |
| @@ -189,6 +210,8 @@ func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, wat | ||
| 189 | 210 | fmt.Println("Error writing record:", err) |
| 190 | 211 | } |
| 191 | 212 | } |
| 213 | ||
| 214 | return nil | |
| 192 | 215 | } |
| 193 | 216 | |
| 194 | 217 | // Function to find an anime by either Anilist ID or Allanime ID |