-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffline_database.go
More file actions
383 lines (320 loc) · 10.1 KB
/
offline_database.go
File metadata and controls
383 lines (320 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
const (
aodGitHubAPIURL = "https://api.github.com/repos/manami-project/anime-offline-database/releases/latest"
aodAssetName = "anime-offline-database-minified.json"
aodMetadataFile = "version.txt"
aodDatabaseFile = "anime-offline-database.json"
)
// AODEntry represents a single anime entry from the offline database.
type AODEntry struct {
Sources []string `json:"sources"`
Title string `json:"title"`
Type string `json:"type"`
}
// OfflineDatabase provides ID mapping between AniList and MyAnimeList
// using the anime-offline-database project.
type OfflineDatabase struct {
malToAniList map[int]int
anilistToMAL map[int]int
lastUpdate string
entries int
}
// GetAniListID returns the AniList ID for a given MAL ID.
func (db *OfflineDatabase) GetAniListID(malID int) (int, bool) {
id, ok := db.malToAniList[malID]
return id, ok
}
// GetMALID returns the MAL ID for a given AniList ID.
func (db *OfflineDatabase) GetMALID(anilistID int) (int, bool) {
id, ok := db.anilistToMAL[anilistID]
return id, ok
}
// LoadOfflineDatabase loads the offline database, downloading or updating as needed.
func LoadOfflineDatabase(ctx context.Context, cfg OfflineDatabaseConfig) (*OfflineDatabase, error) {
if cfg.CacheDir == "" {
cfg.CacheDir = getDefaultCacheDir()
}
dbPath := filepath.Join(cfg.CacheDir, aodDatabaseFile)
metaPath := filepath.Join(cfg.CacheDir, aodMetadataFile)
exists := fileExists(dbPath)
err := ensureDatabase(ctx, cfg, exists, dbPath, metaPath)
if err != nil {
return nil, err
}
return parseAODFile(dbPath)
}
func ensureDatabase(ctx context.Context, cfg OfflineDatabaseConfig, exists bool, dbPath, metaPath string) error {
needsDownload := cfg.ForceRefresh || !exists
if needsDownload {
return handleDownload(ctx, cfg.CacheDir, dbPath, metaPath, exists)
}
if cfg.AutoUpdate {
updateIfNeeded(ctx, dbPath, metaPath)
}
return nil
}
func handleDownload(ctx context.Context, cacheDir, dbPath, metaPath string, exists bool) error {
err := downloadAndCache(ctx, cacheDir, dbPath, metaPath)
if err != nil {
if !exists {
return fmt.Errorf("download offline database: %w", err)
}
LogWarn(ctx, "Failed to download offline database: %v (using cached version)", err)
}
return nil
}
func downloadAndCache(ctx context.Context, cacheDir, dbPath, metaPath string) error {
LogStage(ctx, "Downloading offline database...")
downloadURL, tag, err := getLatestReleaseInfo(ctx)
if err != nil {
return fmt.Errorf("get latest release: %w", err)
}
// #nosec G301 - Cache directory for non-sensitive data
if err := os.MkdirAll(cacheDir, 0o750); err != nil {
return fmt.Errorf("create cache directory: %w", err)
}
if err := downloadAODFile(ctx, downloadURL, dbPath); err != nil {
return fmt.Errorf("download file: %w", err)
}
// #nosec G306 - Version metadata is non-sensitive
if err := os.WriteFile(metaPath, []byte(tag), 0o600); err != nil {
LogWarn(ctx, "Failed to save version metadata: %v", err)
}
LogInfoSuccess(ctx, "Downloaded offline database (version %s)", tag)
return nil
}
func updateIfNeeded(ctx context.Context, dbPath, metaPath string) {
cachedVersion, _ := getCachedVersion(metaPath)
latestURL, latestTag, err := getLatestReleaseInfo(ctx)
if err != nil {
LogDebug(ctx, "Cannot check for offline database updates: %v (using cached version)", err)
return
}
if cachedVersion == latestTag {
LogDebug(ctx, "Offline database is up to date (version %s)", cachedVersion)
return
}
LogInfo(ctx, "Updating offline database: %s → %s", cachedVersion, latestTag)
if err := downloadAODFile(ctx, latestURL, dbPath); err != nil {
LogWarn(ctx, "Failed to update offline database: %v (using cached version)", err)
return
}
if err := os.WriteFile(metaPath, []byte(latestTag), 0o600); err != nil {
LogWarn(ctx, "Failed to save version metadata: %v", err)
}
}
// downloadAODFile downloads the file to a temp location and atomically renames it.
func downloadAODFile(ctx context.Context, url, destPath string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("create request: %w", err)
}
client := NewRetryableClient(&http.Client{Timeout: 5 * time.Minute}, 3)
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("download: %w", err)
}
defer resp.Body.Close() //nolint:errcheck // best effort close
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status: %s", resp.Status)
}
tmpFile, err := os.CreateTemp(filepath.Dir(destPath), "aod-*.tmp")
if err != nil {
return fmt.Errorf("create temp file: %w", err)
}
tmpPath := tmpFile.Name()
if _, err := io.Copy(tmpFile, resp.Body); err != nil {
_ = tmpFile.Close()
_ = os.Remove(tmpPath)
return fmt.Errorf("write file: %w", err)
}
_ = tmpFile.Close()
if err := os.Rename(tmpPath, destPath); err != nil {
_ = os.Remove(tmpPath)
return fmt.Errorf("rename file: %w", err)
}
return nil
}
// parseAODFile parses the offline database JSON file using streaming decoder
// and builds ID mapping indices.
func parseAODFile(filePath string) (*OfflineDatabase, error) {
// #nosec G304 - File path comes from controlled cache directory
f, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("open file: %w", err)
}
defer f.Close() //nolint:errcheck // read-only file
db := &OfflineDatabase{
malToAniList: make(map[int]int),
anilistToMAL: make(map[int]int),
}
decoder := json.NewDecoder(f)
// Read opening brace
if _, err := decoder.Token(); err != nil {
return nil, fmt.Errorf("read opening token: %w", err)
}
for decoder.More() {
// Read field name
tok, err := decoder.Token()
if err != nil {
return nil, fmt.Errorf("read field token: %w", err)
}
key, ok := tok.(string)
if !ok {
continue
}
switch key {
case "data":
err := parseDataArray(decoder, db)
if err != nil {
return nil, err
}
case "lastUpdate":
var lastUpdate string
err := decoder.Decode(&lastUpdate)
if err != nil {
return nil, fmt.Errorf("decode lastUpdate: %w", err)
}
db.lastUpdate = lastUpdate
default:
// Skip unknown fields
var raw json.RawMessage
err := decoder.Decode(&raw)
if err != nil {
return nil, fmt.Errorf("skip field %s: %w", key, err)
}
}
}
return db, nil
}
func parseDataArray(decoder *json.Decoder, db *OfflineDatabase) error {
// Read opening bracket of data array
if _, err := decoder.Token(); err != nil {
return fmt.Errorf("read data array start: %w", err)
}
for decoder.More() {
var entry AODEntry
err := decoder.Decode(&entry)
if err != nil {
return fmt.Errorf("decode entry: %w", err)
}
indexEntry(entry, db)
}
// Read closing bracket
if _, err := decoder.Token(); err != nil {
return fmt.Errorf("read data array end: %w", err)
}
return nil
}
func indexEntry(entry AODEntry, db *OfflineDatabase) {
var malID, anilistID int
for _, src := range entry.Sources {
if id, ok := extractIDFromURL(src, "https://myanimelist.net/anime/"); ok {
malID = id
}
if id, ok := extractIDFromURL(src, "https://anilist.co/anime/"); ok {
anilistID = id
}
}
if malID > 0 && anilistID > 0 {
db.malToAniList[malID] = anilistID
db.anilistToMAL[anilistID] = malID
db.entries++
}
}
// extractIDFromURL extracts a numeric ID from a URL with the given prefix.
// Example: extractIDFromURL("https://myanimelist.net/anime/1535", "https://myanimelist.net/anime/") returns (1535, true).
func extractIDFromURL(url, prefix string) (int, bool) {
if !strings.HasPrefix(url, prefix) {
return 0, false
}
idStr := strings.TrimPrefix(url, prefix)
// Handle URLs with trailing path segments (e.g., "/anime/1535/some-title")
if idx := strings.Index(idStr, "/"); idx != -1 {
idStr = idStr[:idx]
}
id, err := strconv.Atoi(idStr)
if err != nil || id <= 0 {
return 0, false
}
return id, true
}
// GitHub release API response (minimal fields).
type githubRelease struct {
TagName string `json:"tag_name"`
Assets []githubAsset `json:"assets"`
}
type githubAsset struct {
Name string `json:"name"`
BrowserDownloadURL string `json:"browser_download_url"`
}
// getLatestReleaseInfo queries the GitHub API for the latest release download URL and tag.
func getLatestReleaseInfo(ctx context.Context) (downloadURL, tag string, err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, aodGitHubAPIURL, nil)
if err != nil {
return "", "", fmt.Errorf("create request: %w", err)
}
req.Header.Set("Accept", "application/vnd.github.v3+json")
client := NewRetryableClient(&http.Client{Timeout: 30 * time.Second}, 3)
resp, err := client.Do(req)
if err != nil {
return "", "", fmt.Errorf("request: %w", err)
}
defer resp.Body.Close() //nolint:errcheck // best effort close
if resp.StatusCode != http.StatusOK {
return "", "", fmt.Errorf("github API status: %s", resp.Status)
}
var release githubRelease
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", "", fmt.Errorf("decode response: %w", err)
}
for _, asset := range release.Assets {
if asset.Name == aodAssetName {
return asset.BrowserDownloadURL, release.TagName, nil
}
}
return "", "", fmt.Errorf("asset %s not found in release %s", aodAssetName, release.TagName)
}
// getCachedVersion reads the cached version tag from the metadata file.
func getCachedVersion(metadataPath string) (string, error) {
// #nosec G304 - File path comes from controlled cache directory
data, err := os.ReadFile(metadataPath)
if err != nil {
return "", err
}
return strings.TrimSpace(string(data)), nil
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func getDefaultCacheDir() string {
configDir, err := os.UserConfigDir()
if err != nil {
return ""
}
return filepath.Join(configDir, "anilist-mal-sync", "aod-cache")
}
// BuildFromEntries builds an OfflineDatabase from a slice of AODEntry (used in tests).
func BuildFromEntries(entries []AODEntry) *OfflineDatabase {
db := &OfflineDatabase{
malToAniList: make(map[int]int),
anilistToMAL: make(map[int]int),
}
for _, entry := range entries {
indexEntry(entry, db)
}
return db
}