internal/localTracking.go

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