internal/localTracking.go
245 lines · 5930 bytes
1package internal
2
3import (
4 "encoding/csv"
5 "fmt"
6 "os"
7 "path/filepath"
8 "strconv"
9)
10
11// Function to add an anime entry
12func LocalAddAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, watchingTime int, animeName string) {
13 file, err := os.OpenFile(databaseFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
14 if err != nil {
15 fmt.Println("Error opening file:", err)
16 return
17 }
18 defer file.Close()
19
20 writer := csv.NewWriter(file)
21 defer writer.Flush()
22
23 err = writer.Write([]string{
24 strconv.Itoa(anilistID),
25 allanimeID,
26 strconv.Itoa(watchingEpisode),
27 strconv.Itoa(watchingTime),
28 animeName,
29 })
30 if err != nil {
31 fmt.Println("Error writing to file:", err)
32 } else {
33 fmt.Println("Written to file")
34 }
35}
36
37// Function to delete an anime entry by Anilist ID and Allanime ID
38func LocalDeleteAnime(databaseFile string, anilistID int, allanimeID string) {
39 animeList := [][]string{}
40 file, err := os.Open(databaseFile)
41 if err != nil {
42 fmt.Println("Error opening file:", err)
43 return
44 }
45 defer file.Close()
46
47 reader := csv.NewReader(file)
48 records, err := reader.ReadAll()
49 if err != nil {
50 fmt.Println("Error reading file:", err)
51 return
52 }
53
54 // Filter out the anime entry
55 for _, row := range records {
56 aid, _ := strconv.Atoi(row[0]) // Anilist ID
57 if aid != anilistID || row[1] != allanimeID {
58 animeList = append(animeList, row)
59 }
60 }
61
62 // Write the filtered list back to the file
63 fileWrite, err := os.OpenFile(databaseFile, os.O_WRONLY|os.O_TRUNC, 0644)
64 if err != nil {
65 fmt.Println("Error opening file for writing:", err)
66 return
67 }
68 defer fileWrite.Close()
69
70 writer := csv.NewWriter(fileWrite)
71 defer writer.Flush()
72
73 err = writer.WriteAll(animeList)
74 if err != nil {
75 fmt.Println("Error writing to file:", err)
76 }
77}
78
79// Function to get all anime entries from the database
80func LocalGetAllAnime(databaseFile string) []Anime {
81 animeList := []Anime{}
82
83 // Ensure the directory exists
84 dir := filepath.Dir(databaseFile)
85 if err := os.MkdirAll(dir, 0755); err != nil {
86 fmt.Println("Error creating directory:", err)
87 return animeList
88 }
89
90 // Open the file, create if it doesn't exist
91 file, err := os.OpenFile(databaseFile, os.O_RDONLY|os.O_CREATE, 0644)
92 if err != nil {
93 fmt.Println("Error opening or creating file:", err)
94 return animeList
95 }
96 defer file.Close()
97
98 // If the file was just created, it will be empty, so return an empty list
99 fileInfo, err := file.Stat()
100 if err != nil {
101 fmt.Println("Error getting file info:", err)
102 return animeList
103 }
104 if fileInfo.Size() == 0 {
105 return animeList
106 }
107
108 reader := csv.NewReader(file)
109 records, err := reader.ReadAll()
110 if err != nil {
111 fmt.Println("Error reading file:", err)
112 return animeList
113 }
114
115 for _, row := range records {
116 anime := parseAnimeRow(row)
117 if anime != nil {
118 animeList = append(animeList, *anime)
119 }
120 }
121
122 return animeList
123}
124
125// Function to parse a single row of anime data
126func parseAnimeRow(row []string) *Anime {
127 if len(row) < 5 {
128 fmt.Printf("Invalid row format: %v\n", row)
129 return nil
130 }
131
132 anilistID, _ := strconv.Atoi(row[0])
133 watchingEpisode, _ := strconv.Atoi(row[2])
134 playbackTime, _ := strconv.Atoi(row[3])
135
136 anime := &Anime{
137 AnilistId: anilistID,
138 AllanimeId: row[1],
139 Ep: Episode{
140 Number: watchingEpisode,
141 Player: playingVideo{
142 PlaybackTime: playbackTime,
143 },
144 },
145 Title: AnimeTitle{
146 English: row[4],
147 Romaji: row[4],
148 },
149 }
150
151 // Check if there's an extra field for total episodes
152 if len(row) == 6 {
153 totalEpisodes, _ := strconv.Atoi(row[5])
154 if watchingEpisode == totalEpisodes {
155 // If watching episode equals total episodes, don't include the total episodes field
156 return anime
157 }
158 // If not equal, you might want to store this information somewhere
159 // For now, we'll just print it
160 // fmt.Printf("Anime %s has %d total episodes\n", anime.Title.English, totalEpisodes)
161 }
162
163 return anime
164}
165
166// Function to get the anime name (English or Romaji) from an Anime struct
167func GetAnimeName(anime Anime) string {
168 if anime.Title.English != "" {
169 return anime.Title.English
170 }
171 return anime.Title.Romaji
172}
173
174// Function to update or add a new anime entry
175func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, playbackTime int, animeName string) error {
176 // Read existing entries
177 animeList := LocalGetAllAnime(databaseFile)
178
179 // Find and update existing entry or add new one
180 updated := false
181 for i, anime := range animeList {
182 if anime.AnilistId == anilistID && anime.AllanimeId == allanimeID {
183 animeList[i].Ep.Number = watchingEpisode
184 animeList[i].Ep.Player.PlaybackTime = playbackTime
185 animeList[i].Title.English = animeName
186 animeList[i].Title.Romaji = animeName
187 updated = true
188 break
189 }
190 }
191
192 if !updated {
193 newAnime := Anime{
194 AnilistId: anilistID,
195 AllanimeId: allanimeID,
196 Ep: Episode{
197 Number: watchingEpisode,
198 Player: playingVideo{
199 PlaybackTime: playbackTime,
200 },
201 },
202 Title: AnimeTitle{
203 English: animeName,
204 Romaji: animeName,
205 },
206 }
207 animeList = append(animeList, newAnime)
208 }
209
210 // Write updated list back to file
211 file, err := os.Create(databaseFile)
212 if err != nil {
213 fmt.Println("Error creating file:", err)
214 return err
215 }
216 defer file.Close()
217
218 writer := csv.NewWriter(file)
219 defer writer.Flush()
220
221 for _, anime := range animeList {
222 record := []string{
223 strconv.Itoa(anime.AnilistId),
224 anime.AllanimeId,
225 strconv.Itoa(anime.Ep.Number),
226 strconv.Itoa(anime.Ep.Player.PlaybackTime),
227 GetAnimeName(anime),
228 }
229 if err := writer.Write(record); err != nil {
230 fmt.Println("Error writing record:", err)
231 }
232 }
233
234 return nil
235}
236
237// Function to find an anime by either Anilist ID or Allanime ID
238func LocalFindAnime(animeList []Anime, anilistID int, allanimeID string) *Anime {
239 for _, anime := range animeList {
240 if anime.AnilistId == anilistID || anime.AllanimeId == allanimeID {
241 return &anime
242 }
243 }
244 return nil
245}