diff options
| author | 2026-08-01 19:58:57 +0700 | |
|---|---|---|
| committer | 2026-08-01 19:58:57 +0700 | |
| commit | 684d0b6a57d2eb79730ade63baccdc6e59bebc29 (patch) | |
| tree | 956c89c81965b8c974c8497e3719d0b16d332512 /internal/storage/index.go | |
| parent | 129e48683f6fc0615b606a737f2afcd50538bb2e (diff) | |
| download | articlator-main.tar.gz articlator-main.zip | |
feat: repurpose metadata parser as article transcoder for saya.uz andmain
telegram
Diffstat (limited to 'internal/storage/index.go')
| -rw-r--r-- | internal/storage/index.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/internal/storage/index.go b/internal/storage/index.go new file mode 100644 index 0000000..bfeafbe --- /dev/null +++ b/internal/storage/index.go @@ -0,0 +1,92 @@ +package storage + +import ( + "encoding/json" + "fmt" + "time" +) + +const IndexFileName = "index.json" +const MedleysIndexFileName = "medleys.json" + +const IndexSchemaVersion = 2 + +type IndexEntry struct { + Link string `json:"link"` + ModifiedTime time.Time `json:"modifiedTime"` + Title string `json:"title"` + ShortDescription string `json:"shortDescription"` + ActionDate string `json:"actionDate"` + PublishedTime time.Time `json:"publishedTime"` + Thumbnail string `json:"thumbnail"` + Tags []string `json:"tags"` + Geolocation string `json:"geolocation"` + Medley string `json:"medley,omitempty"` + MedleyPart int `json:"medleyPart,omitempty"` +} + +type IndexV2Category struct { + GeneratedAt time.Time `json:"generatedAt"` + Pages map[string]IndexEntry `json:"pages"` +} + +type IndexV1Category struct { + GeneratedAt time.Time `json:"generatedAt"` + Pages []IndexEntry `json:"pages"` +} + +type Index struct { + SchemaVersion int `json:"schemaVersion"` + GeneratedAt time.Time `json:"generatedAt"` + Categories any `json:"categories"` +} + +func (idx *Index) UnmarshalJSON(data []byte) error { + var tmp struct { + SchemaVersion int `json:"schemaVersion"` + GeneratedAt time.Time `json:"generatedAt"` + Categories json.RawMessage `json:"categories"` + } + + if err := json.Unmarshal(data, &tmp); err != nil { + return err + } + + idx.SchemaVersion = tmp.SchemaVersion + idx.GeneratedAt = tmp.GeneratedAt + + switch tmp.SchemaVersion { + case 1: + var categories map[string]*IndexV1Category + if err := json.Unmarshal(tmp.Categories, &categories); err != nil { + return fmt.Errorf("unmarshal map[string]*IndexV1Category: %w", err) + } + idx.Categories = &categories + case 2: + var categories map[string]*IndexV2Category + if err := json.Unmarshal(tmp.Categories, &categories); err != nil { + return fmt.Errorf("unmarshal map[string]*IndexV2Category: %w", err) + } + idx.Categories = &categories + default: + return fmt.Errorf("unsupported index version: %d", tmp.SchemaVersion) + } + + return nil +} + +type IndexV1 struct { + SchemaVersion int `json:"schemaVersion"` + GeneratedAt time.Time `json:"generatedAt"` + Categories map[string]*IndexV1Category `json:"categories"` +} + +type MedleyEntry struct { + Codename string `json:"codename"` + Content []string `json:"content"` +} + +type MedleyPageEntry struct { + Codename string `json:"codename"` + Position int `json:"position"` +} |