internal/localTracking.go
225 lines · 5391 bytes
1package internal
2
3import (
4 "encoding/csv"
5 "fmt"
6 "os"
7 "strconv"
8)
9
10// Function to add an anime entry
11func LocalAddAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, watchingTime int, animeName string) {
12 file, err := os.OpenFile(databaseFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
13 if err != nil {
14 fmt.Println("Error opening file:", err)
15 return
16 }
17 defer file.Close()
18
19 writer := csv.NewWriter(file)
20 defer writer.Flush()
21
22 err = writer.Write([]string{
23 strconv.Itoa(anilistID),
24 allanimeID,
25 strconv.Itoa(watchingEpisode),
26 strconv.Itoa(watchingTime),
27 animeName,
28 })
29 if err != nil {
30 fmt.Println("Error writing to file:", err)
31 } else {
32 fmt.Println("Written to file")
33 }
34}
35
36// Function to delete an anime entry by Anilist ID and Allanime ID
37func LocalDeleteAnime(databaseFile string, anilistID int, allanimeID string) {
38 animeList := [][]string{}
39 file, err := os.Open(databaseFile)
40 if err != nil {
41 fmt.Println("Error opening file:", err)
42 return
43 }
44 defer file.Close()
45
46 reader := csv.NewReader(file)
47 records, err := reader.ReadAll()
48 if err != nil {
49 fmt.Println("Error reading file:", err)
50 return
51 }
52
53 // Filter out the anime entry
54 for _, row := range records {
55 aid, _ := strconv.Atoi(row[0]) // Anilist ID
56 if aid != anilistID || row[1] != allanimeID {
57 animeList = append(animeList, row)
58 }
59 }
60
61 // Write the filtered list back to the file
62 fileWrite, err := os.OpenFile(databaseFile, os.O_WRONLY|os.O_TRUNC, 0644)
63 if err != nil {
64 fmt.Println("Error opening file for writing:", err)
65 return
66 }
67 defer fileWrite.Close()
68
69 writer := csv.NewWriter(fileWrite)
70 defer writer.Flush()
71
72 err = writer.WriteAll(animeList)
73 if err != nil {
74 fmt.Println("Error writing to file:", err)
75 }
76}
77
78// Function to get all anime entries from the database
79func LocalGetAllAnime(databaseFile string) []Anime {
80 animeList := []Anime{}
81 file, err := os.Open(databaseFile)
82 if err != nil {
83 fmt.Println("Error opening file:", err)
84 return animeList
85 }
86 defer file.Close()
87
88 reader := csv.NewReader(file)
89 records, err := reader.ReadAll()
90 if err != nil {
91 fmt.Println("Error reading file:", err)
92 return animeList
93 }
94
95 for _, row := range records {
96 anime := parseAnimeRow(row)
97 if anime != nil {
98 animeList = append(animeList, *anime)
99 }
100 }
101
102 return animeList
103}
104
105// Function to parse a single row of anime data
106func 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,
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
137 }
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)
141 }
142
143 return anime
144}
145
146// Function to get the anime name (English or Romaji) from an Anime struct
147func GetAnimeName(anime Anime) string {
148 if anime.Title.English != "" {
149 return anime.Title.English
150 }
151 return anime.Title.Romaji
152}
153
154// Function to update or add a new anime entry
155func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, playbackTime int, animeName string) error {
156 // Read existing entries
157 animeList := LocalGetAllAnime(databaseFile)
158
159 // Find and update existing entry or add new one
160 updated := false
161 for i, anime := range animeList {
162 if anime.AnilistId == anilistID && anime.AllanimeId == allanimeID {
163 animeList[i].Ep.Number = watchingEpisode
164 animeList[i].Ep.Player.PlaybackTime = playbackTime
165 animeList[i].Title.English = animeName
166 animeList[i].Title.Romaji = animeName
167 updated = true
168 break
169 }
170 }
171
172 if !updated {
173 newAnime := Anime{
174 AnilistId: anilistID,
175 AllanimeId: allanimeID,
176 Ep: Episode{
177 Number: watchingEpisode,
178 Player: playingVideo{
179 PlaybackTime: playbackTime,
180 },
181 },
182 Title: AnimeTitle{
183 English: animeName,
184 Romaji: animeName,
185 },
186 }
187 animeList = append(animeList, newAnime)
188 }
189
190 // Write updated list back to file
191 file, err := os.Create(databaseFile)
192 if err != nil {
193 fmt.Println("Error creating file:", err)
194 return err
195 }
196 defer file.Close()
197
198 writer := csv.NewWriter(file)
199 defer writer.Flush()
200
201 for _, anime := range animeList {
202 record := []string{
203 strconv.Itoa(anime.AnilistId),
204 anime.AllanimeId,
205 strconv.Itoa(anime.Ep.Number),
206 strconv.Itoa(anime.Ep.Player.PlaybackTime),
207 GetAnimeName(anime),
208 }
209 if err := writer.Write(record); err != nil {
210 fmt.Println("Error writing record:", err)
211 }
212 }
213
214 return nil
215}
216
217// Function to find an anime by either Anilist ID or Allanime ID
218func LocalFindAnime(animeList []Anime, anilistID int, allanimeID string) *Anime {
219 for _, anime := range animeList {
220 if anime.AnilistId == anilistID || anime.AllanimeId == allanimeID {
221 return &anime
222 }
223 }
224 return nil
225}