internal/localTracking.go
239 lines · 5595 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// Function to parse a single row of anime data
125func parseAnimeRow(row []string) *Anime {
126 if len(row) < 5 {
127 fmt.Printf("Invalid row format: %v\n", row)
128 return nil
129 }
130
131 anilistID, _ := strconv.Atoi(row[0])
132 watchingEpisode, _ := strconv.Atoi(row[2])
133 playbackTime, _ := strconv.Atoi(row[3])
134
135 anime := &Anime{
136 AnilistId: anilistID,
137 AllanimeId: row[1],
138 Ep: Episode{
139 Number: watchingEpisode,
140 Player: playingVideo{
141 PlaybackTime: playbackTime,
142 },
143 },
144 }
145
146 if len(row) == 6 {
147 anime.Title = AnimeTitle{
148 English: row[5],
149 Romaji: row[5],
150 }
151 } else if len(row) == 5 {
152 anime.Title = AnimeTitle{
153 English: row[4],
154 Romaji: row[4],
155 }
156 }
157
158 return anime
159}
160// Function to get the anime name (English or Romaji) from an Anime struct
161func GetAnimeName(anime Anime) string {
162 if anime.Title.English != "" {
163 return anime.Title.English
164 }
165 return anime.Title.Romaji
166}
167
168// Function to update or add a new anime entry
169func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, playbackTime int, animeName string) error {
170 // Read existing entries
171 animeList := LocalGetAllAnime(databaseFile)
172
173 // Find and update existing entry or add new one
174 updated := false
175 for i, anime := range animeList {
176 if anime.AnilistId == anilistID && anime.AllanimeId == allanimeID {
177 animeList[i].Ep.Number = watchingEpisode
178 animeList[i].Ep.Player.PlaybackTime = playbackTime
179 animeList[i].Title.English = animeName
180 animeList[i].Title.Romaji = animeName
181 updated = true
182 break
183 }
184 }
185
186 if !updated {
187 newAnime := Anime{
188 AnilistId: anilistID,
189 AllanimeId: allanimeID,
190 Ep: Episode{
191 Number: watchingEpisode,
192 Player: playingVideo{
193 PlaybackTime: playbackTime,
194 },
195 },
196 Title: AnimeTitle{
197 English: animeName,
198 Romaji: animeName,
199 },
200 }
201 animeList = append(animeList, newAnime)
202 }
203
204 // Write updated list back to file
205 file, err := os.Create(databaseFile)
206 if err != nil {
207 fmt.Println("Error creating file:", err)
208 return err
209 }
210 defer file.Close()
211
212 writer := csv.NewWriter(file)
213 defer writer.Flush()
214
215 for _, anime := range animeList {
216 record := []string{
217 strconv.Itoa(anime.AnilistId),
218 anime.AllanimeId,
219 strconv.Itoa(anime.Ep.Number),
220 strconv.Itoa(anime.Ep.Player.PlaybackTime),
221 GetAnimeName(anime),
222 }
223 if err := writer.Write(record); err != nil {
224 fmt.Println("Error writing record:", err)
225 }
226 }
227
228 return nil
229}
230
231// Function to find an anime by either Anilist ID or Allanime ID
232func LocalFindAnime(animeList []Anime, anilistID int, allanimeID string) *Anime {
233 for _, anime := range animeList {
234 if anime.AnilistId == anilistID || anime.AllanimeId == allanimeID {
235 return &anime
236 }
237 }
238 return nil
239}