Commit de4e4c93ec
Unsigned
Layout: unified · split
internal/localTracking.go +55 −47
| @@ -74,8 +74,6 @@ func LocalDeleteAnime(databaseFile string, anilistID int, allanimeID string) { | ||
| 74 | 74 | fmt.Println("Error writing to file:", err) |
| 75 | 75 | } |
| 76 | 76 | } |
| 77 | ||
| 78 | // Function to retrieve all anime entries | |
| 79 | 77 | func LocalGetAllAnime(databaseFile string) []Anime { |
| 80 | 78 | animeList := []Anime{} |
| 81 | 79 | file, err := os.Open(databaseFile) |
| @@ -93,9 +91,14 @@ func LocalGetAllAnime(databaseFile string) []Anime { | ||
| 93 | 91 | } |
| 94 | 92 | |
| 95 | 93 | for _, row := range records { |
| 94 | if len(row) != 5 { | |
| 95 | fmt.Printf("Invalid row format: %v\n", row) | |
| 96 | continue | |
| 97 | } | |
| 98 | ||
| 96 | 99 | anilistID, _ := strconv.Atoi(row[0]) |
| 97 | 100 | watchingEpisode, _ := strconv.Atoi(row[2]) |
| 98 | watchingTime, _ := strconv.Atoi(row[3]) | |
| 101 | playbackTime, _ := strconv.Atoi(row[3]) | |
| 99 | 102 | |
| 100 | 103 | anime := Anime{ |
| 101 | 104 | AnilistId: anilistID, |
| @@ -103,11 +106,12 @@ func LocalGetAllAnime(databaseFile string) []Anime { | ||
| 103 | 106 | Ep: Episode{ |
| 104 | 107 | Number: watchingEpisode, |
| 105 | 108 | Player: playingVideo{ |
| 106 | PlaybackTime: watchingTime, | |
| 109 | PlaybackTime: playbackTime, | |
| 107 | 110 | }, |
| 108 | 111 | }, |
| 109 | 112 | Title: AnimeTitle{ |
| 110 | Romaji: row[4], | |
| 113 | English: row[4], | |
| 114 | Romaji: row[4], | |
| 111 | 115 | }, |
| 112 | 116 | } |
| 113 | 117 | animeList = append(animeList, anime) |
| @@ -116,54 +120,53 @@ func LocalGetAllAnime(databaseFile string) []Anime { | ||
| 116 | 120 | return animeList |
| 117 | 121 | } |
| 118 | 122 | |
| 123 | // Function to get the anime name (English or Romaji) from an Anime struct | |
| 124 | func getAnimeName(anime Anime) string { | |
| 125 | if anime.Title.English != "" { | |
| 126 | return anime.Title.English | |
| 127 | } | |
| 128 | return anime.Title.Romaji | |
| 129 | } | |
| 119 | 130 | // Function to update or add a new anime entry |
| 120 | func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, watchingTime int, animeName string) { | |
| 131 | func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, playbackTime int, animeName string) { | |
| 132 | // Read existing entries | |
| 121 | 133 | animeList := LocalGetAllAnime(databaseFile) |
| 134 | ||
| 135 | // Find and update existing entry or add new one | |
| 122 | 136 | updated := false |
| 123 | updatedAnimeList := [][]string{} | |
| 124 | ||
| 125 | // Check if the anime entry exists and update it | |
| 126 | for _, existingAnime := range animeList { | |
| 127 | if existingAnime.AnilistId == anilistID && existingAnime.AllanimeId == allanimeID { | |
| 128 | updatedAnimeList = append(updatedAnimeList, []string{ | |
| 129 | strconv.Itoa(anilistID), | |
| 130 | allanimeID, | |
| 131 | strconv.Itoa(watchingEpisode), | |
| 132 | strconv.Itoa(watchingTime), | |
| 133 | animeName, | |
| 134 | }) | |
| 137 | for i, anime := range animeList { | |
| 138 | if anime.AnilistId == anilistID && anime.AllanimeId == allanimeID { | |
| 139 | animeList[i].Ep.Number = watchingEpisode | |
| 140 | animeList[i].Ep.Player.PlaybackTime = playbackTime | |
| 141 | animeList[i].Title.English = animeName | |
| 142 | animeList[i].Title.Romaji = animeName | |
| 135 | 143 | updated = true |
| 136 | } else { | |
| 137 | updatedAnimeList = append(updatedAnimeList, []string{ | |
| 138 | strconv.Itoa(existingAnime.AnilistId), | |
| 139 | existingAnime.AllanimeId, | |
| 140 | strconv.Itoa(existingAnime.Ep.Number), | |
| 141 | strconv.Itoa(existingAnime.Ep.Player.PlaybackTime), | |
| 142 | func() string { | |
| 143 | if existingAnime.Title.English != "" { | |
| 144 | return existingAnime.Title.English | |
| 145 | } | |
| 146 | return existingAnime.Title.Romaji | |
| 147 | }(), // Keep the original anime name (Romaji or English) for other entries | |
| 148 | }) | |
| 144 | break | |
| 149 | 145 | } |
| 150 | 146 | } |
| 151 | 147 | |
| 152 | // If no entry was updated, add a new entry | |
| 153 | 148 | if !updated { |
| 154 | updatedAnimeList = append(updatedAnimeList, []string{ | |
| 155 | strconv.Itoa(anilistID), | |
| 156 | allanimeID, | |
| 157 | strconv.Itoa(watchingEpisode), | |
| 158 | strconv.Itoa(watchingTime), | |
| 159 | animeName, | |
| 160 | }) | |
| 149 | newAnime := Anime{ | |
| 150 | AnilistId: anilistID, | |
| 151 | AllanimeId: allanimeID, | |
| 152 | Ep: Episode{ | |
| 153 | Number: watchingEpisode, | |
| 154 | Player: playingVideo{ | |
| 155 | PlaybackTime: playbackTime, | |
| 156 | }, | |
| 157 | }, | |
| 158 | Title: AnimeTitle{ | |
| 159 | English: animeName, | |
| 160 | Romaji: animeName, | |
| 161 | }, | |
| 162 | } | |
| 163 | animeList = append(animeList, newAnime) | |
| 161 | 164 | } |
| 162 | 165 | |
| 163 | // Write all entries back to the file | |
| 164 | file, err := os.OpenFile(databaseFile, os.O_WRONLY|os.O_TRUNC, 0644) | |
| 166 | // Write updated list back to file | |
| 167 | file, err := os.Create(databaseFile) | |
| 165 | 168 | if err != nil { |
| 166 | fmt.Println("Error opening file for writing:", err) | |
| 169 | fmt.Println("Error creating file:", err) | |
| 167 | 170 | return |
| 168 | 171 | } |
| 169 | 172 | defer file.Close() |
| @@ -171,11 +174,16 @@ func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, wat | ||
| 171 | 174 | writer := csv.NewWriter(file) |
| 172 | 175 | defer writer.Flush() |
| 173 | 176 | |
| 174 | for _, updatedAnime := range updatedAnimeList { | |
| 175 | err = writer.Write(updatedAnime) | |
| 176 | if err != nil { | |
| 177 | fmt.Println("Error writing to file:", err) | |
| 178 | return | |
| 177 | for _, anime := range animeList { | |
| 178 | record := []string{ | |
| 179 | strconv.Itoa(anime.AnilistId), | |
| 180 | anime.AllanimeId, | |
| 181 | strconv.Itoa(anime.Ep.Number), | |
| 182 | strconv.Itoa(anime.Ep.Player.PlaybackTime), | |
| 183 | getAnimeName(anime), | |
| 184 | } | |
| 185 | if err := writer.Write(record); err != nil { | |
| 186 | fmt.Println("Error writing record:", err) | |
| 179 | 187 | } |
| 180 | 188 | } |
| 181 | 189 | } |