Commit de4e4c93ec

de4e4c93ec2e6c632062679d6a6cb22ff073861b

parent: 69973d45b9

Unsigned

Wraient <wraient.911@gmail.com> · 2024-10-21 18:40 UTC

Fix: Local update function fixed

Layout: unified · split

internal/localTracking.go +55 −47
@@ -74,8 +74,6 @@ func LocalDeleteAnime(databaseFile string, anilistID int, allanimeID string) {
7474 fmt.Println("Error writing to file:", err)
7575 }
7676}
77
78// Function to retrieve all anime entries
7977func LocalGetAllAnime(databaseFile string) []Anime {
8078 animeList := []Anime{}
8179 file, err := os.Open(databaseFile)
@@ -93,9 +91,14 @@ func LocalGetAllAnime(databaseFile string) []Anime {
9391 }
9492
9593 for _, row := range records {
94 if len(row) != 5 {
95 fmt.Printf("Invalid row format: %v\n", row)
96 continue
97 }
98
9699 anilistID, _ := strconv.Atoi(row[0])
97100 watchingEpisode, _ := strconv.Atoi(row[2])
98 watchingTime, _ := strconv.Atoi(row[3])
101 playbackTime, _ := strconv.Atoi(row[3])
99102
100103 anime := Anime{
101104 AnilistId: anilistID,
@@ -103,11 +106,12 @@ func LocalGetAllAnime(databaseFile string) []Anime {
103106 Ep: Episode{
104107 Number: watchingEpisode,
105108 Player: playingVideo{
106 PlaybackTime: watchingTime,
109 PlaybackTime: playbackTime,
107110 },
108111 },
109112 Title: AnimeTitle{
110 Romaji: row[4],
113 English: row[4],
114 Romaji: row[4],
111115 },
112116 }
113117 animeList = append(animeList, anime)
@@ -116,54 +120,53 @@ func LocalGetAllAnime(databaseFile string) []Anime {
116120 return animeList
117121}
118122
123// Function to get the anime name (English or Romaji) from an Anime struct
124func getAnimeName(anime Anime) string {
125 if anime.Title.English != "" {
126 return anime.Title.English
127 }
128 return anime.Title.Romaji
129}
119130// Function to update or add a new anime entry
120func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, watchingTime int, animeName string) {
131func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, playbackTime int, animeName string) {
132 // Read existing entries
121133 animeList := LocalGetAllAnime(databaseFile)
134
135 // Find and update existing entry or add new one
122136 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
135143 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
149145 }
150146 }
151147
152 // If no entry was updated, add a new entry
153148 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)
161164 }
162165
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)
165168 if err != nil {
166 fmt.Println("Error opening file for writing:", err)
169 fmt.Println("Error creating file:", err)
167170 return
168171 }
169172 defer file.Close()
@@ -171,11 +174,16 @@ func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, wat
171174 writer := csv.NewWriter(file)
172175 defer writer.Flush()
173176
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)
179187 }
180188 }
181189}