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