internal/localTracking.go

202 lines · 4760 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		if len(row) != 5 {
 97			fmt.Printf("Invalid row format: %v\n", row)
 98			continue
 99		}
100
101		anilistID, _ := strconv.Atoi(row[0])
102		watchingEpisode, _ := strconv.Atoi(row[2])
103		playbackTime, _ := strconv.Atoi(row[3])
104
105		anime := Anime{
106			AnilistId:  anilistID,
107			AllanimeId: row[1],
108			Ep: Episode{
109				Number: watchingEpisode,
110				Player: playingVideo{
111					PlaybackTime: playbackTime,
112				},
113			},
114			Title: AnimeTitle{
115				English: row[4],
116				Romaji:  row[4],
117			},
118		}
119		animeList = append(animeList, anime)
120	}
121
122	return animeList
123}
124
125// Function to get the anime name (English or Romaji) from an Anime struct
126func getAnimeName(anime Anime) string {
127	if anime.Title.English != "" {
128		return anime.Title.English
129	}
130	return anime.Title.Romaji
131}
132
133// Function to update or add a new anime entry
134func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, playbackTime int, animeName string) {
135	// Read existing entries
136	animeList := LocalGetAllAnime(databaseFile)
137
138	// Find and update existing entry or add new one
139	updated := false
140	for i, anime := range animeList {
141		if anime.AnilistId == anilistID && anime.AllanimeId == allanimeID {
142			animeList[i].Ep.Number = watchingEpisode
143			animeList[i].Ep.Player.PlaybackTime = playbackTime
144			animeList[i].Title.English = animeName
145			animeList[i].Title.Romaji = animeName
146			updated = true
147			break
148		}
149	}
150
151	if !updated {
152		newAnime := Anime{
153			AnilistId:  anilistID,
154			AllanimeId: allanimeID,
155			Ep: Episode{
156				Number: watchingEpisode,
157				Player: playingVideo{
158					PlaybackTime: playbackTime,
159				},
160			},
161			Title: AnimeTitle{
162				English: animeName,
163				Romaji:  animeName,
164			},
165		}
166		animeList = append(animeList, newAnime)
167	}
168
169	// Write updated list back to file
170	file, err := os.Create(databaseFile)
171	if err != nil {
172		fmt.Println("Error creating file:", err)
173		return
174	}
175	defer file.Close()
176
177	writer := csv.NewWriter(file)
178	defer writer.Flush()
179
180	for _, anime := range animeList {
181		record := []string{
182			strconv.Itoa(anime.AnilistId),
183			anime.AllanimeId,
184			strconv.Itoa(anime.Ep.Number),
185			strconv.Itoa(anime.Ep.Player.PlaybackTime),
186			getAnimeName(anime),
187		}
188		if err := writer.Write(record); err != nil {
189			fmt.Println("Error writing record:", err)
190		}
191	}
192}
193
194// Function to find an anime by either Anilist ID or Allanime ID
195func LocalFindAnime(animeList []Anime, anilistID int, allanimeID string) *Anime {
196	for _, anime := range animeList {
197		if anime.AnilistId == anilistID || anime.AllanimeId == allanimeID {
198			return &anime
199		}
200	}
201	return nil
202}