Commit 533a5bfb3a
Unsigned
Layout: unified · split
internal/localTracking.go added +186
| @@ -0,0 +1,186 @@ | ||
| 1 | package internal | |
| 2 | ||
| 3 | import ( | |
| 4 | "encoding/csv" | |
| 5 | "fmt" | |
| 6 | "os" | |
| 7 | "strconv" | |
| 8 | ) | |
| 9 | ||
| 10 | // Function to add an anime entry | |
| 11 | func 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 | |
| 37 | func 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 retrieve all anime entries | |
| 79 | func 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 | anilistID, _ := strconv.Atoi(row[0]) | |
| 97 | watchingEpisode, _ := strconv.Atoi(row[2]) | |
| 98 | watchingTime, _ := strconv.Atoi(row[3]) | |
| 99 | ||
| 100 | anime := Anime{ | |
| 101 | AnilistId: anilistID, | |
| 102 | AllanimeId: row[1], | |
| 103 | Ep: Episode{ | |
| 104 | Number: watchingEpisode, | |
| 105 | Player: playingVideo{ | |
| 106 | PlaybackTime: watchingTime, | |
| 107 | }, | |
| 108 | }, | |
| 109 | Title: AnimeTitle{ | |
| 110 | Romaji: row[4], | |
| 111 | }, | |
| 112 | } | |
| 113 | animeList = append(animeList, anime) | |
| 114 | } | |
| 115 | ||
| 116 | return animeList | |
| 117 | } | |
| 118 | ||
| 119 | // Function to update or add a new anime entry | |
| 120 | func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, watchingTime int, animeName string) { | |
| 121 | animeList := LocalGetAllAnime(databaseFile) | |
| 122 | updated := false | |
| 123 | updatedAnimeList := [][]string{} | |
| 124 | ||
| 125 | // Check if the anime entry exists and update it | |
| 126 | for _, existingAnime := range animeList { | |
| 127 | if existingAnime.AnilistId == anilistID && existingAnime.AllanimeId == allanimeID { | |
| 128 | updatedAnimeList = append(updatedAnimeList, []string{ | |
| 129 | strconv.Itoa(anilistID), | |
| 130 | allanimeID, | |
| 131 | strconv.Itoa(watchingEpisode), | |
| 132 | strconv.Itoa(watchingTime), | |
| 133 | animeName, | |
| 134 | }) | |
| 135 | updated = true | |
| 136 | } else { | |
| 137 | updatedAnimeList = append(updatedAnimeList, []string{ | |
| 138 | strconv.Itoa(existingAnime.AnilistId), | |
| 139 | existingAnime.AllanimeId, | |
| 140 | strconv.Itoa(existingAnime.Ep.Number), | |
| 141 | strconv.Itoa(watchingTime), // Assuming you want to update the watching time too | |
| 142 | existingAnime.Title.Romaji, | |
| 143 | }) | |
| 144 | } | |
| 145 | } | |
| 146 | ||
| 147 | // If no entry was updated, add a new entry | |
| 148 | if !updated { | |
| 149 | updatedAnimeList = append(updatedAnimeList, []string{ | |
| 150 | strconv.Itoa(anilistID), | |
| 151 | allanimeID, | |
| 152 | strconv.Itoa(watchingEpisode), | |
| 153 | strconv.Itoa(watchingTime), | |
| 154 | animeName, | |
| 155 | }) | |
| 156 | } | |
| 157 | ||
| 158 | // Write all entries back to the file | |
| 159 | file, err := os.OpenFile(databaseFile, os.O_WRONLY|os.O_TRUNC, 0644) | |
| 160 | if err != nil { | |
| 161 | fmt.Println("Error opening file for writing:", err) | |
| 162 | return | |
| 163 | } | |
| 164 | defer file.Close() | |
| 165 | ||
| 166 | writer := csv.NewWriter(file) | |
| 167 | defer writer.Flush() | |
| 168 | ||
| 169 | for _, updatedAnime := range updatedAnimeList { | |
| 170 | err = writer.Write(updatedAnime) | |
| 171 | if err != nil { | |
| 172 | fmt.Println("Error writing to file:", err) | |
| 173 | return | |
| 174 | } | |
| 175 | } | |
| 176 | } | |
| 177 | ||
| 178 | // Function to find an anime by either Anilist ID or Allanime ID | |
| 179 | func LocalFindAnime(animeList []Anime, anilistID int, allanimeID string) *Anime { | |
| 180 | for _, anime := range animeList { | |
| 181 | if anime.AnilistId == anilistID || anime.AllanimeId == allanimeID { | |
| 182 | return &anime | |
| 183 | } | |
| 184 | } | |
| 185 | return nil | |
| 186 | } | |