]> git.lizzy.rs Git - go-anidb.git/blob - file.go
misc: (*EpisodeList).Sub: allow subtracting arbitrary containers
[go-anidb.git] / file.go
1 package anidb
2
3 import (
4         "encoding/json"
5         "fmt"
6         "github.com/Kovensky/go-anidb/misc"
7         "image"
8         "strconv"
9         "time"
10 )
11
12 type FileVersion int
13
14 func (v FileVersion) String() string {
15         if v == 1 {
16                 return ""
17         }
18         return fmt.Sprintf("v%d", int(v))
19 }
20
21 type FileSource string
22
23 type AudioStream struct {
24         Codec    string
25         Bitrate  int
26         Language Language
27 }
28
29 type VideoInfo struct {
30         Codec      string
31         Bitrate    int
32         Resolution image.Rectangle
33         ColorDepth int
34 }
35
36 type File struct {
37         FID FID
38
39         AID AID
40         EID EID
41         GID GID
42
43         EpisodeNumber misc.EpisodeList
44
45         Incomplete bool
46
47         Deprecated bool
48         CRCMatch   bool
49         BadCRC     bool
50         Version    FileVersion
51         Uncensored bool // Meaning unclear, may not be simply !Censored
52         Censored   bool // Meaning unclear, may not be simply !Uncensored
53
54         Filesize int64
55         Ed2kHash string
56         SHA1Hash string
57         CRC32    string
58
59         Length  time.Duration
60         AirDate time.Time
61
62         AudioStreams      []AudioStream
63         SubtitleLanguages []Language
64         VideoInfo         VideoInfo
65         FileExtension     string
66
67         Source FileSource
68
69         // Map of related EIDs to percentages (range 0.0-1.0).
70         // The percentage indicates how much of the EID is covered by this file.
71         RelatedEpisodes RelatedEpisodes
72
73         Cached time.Time
74 }
75
76 type RelatedEpisodes map[EID]float32
77
78 func (er RelatedEpisodes) MarshalJSON() ([]byte, error) {
79         generic := make(map[string]float32, len(er))
80         for k, v := range er {
81                 generic[strconv.Itoa(int(k))] = v
82         }
83         return json.Marshal(generic)
84 }
85
86 func (er RelatedEpisodes) UnmarshalJSON(b []byte) error {
87         var generic map[string]float32
88         if err := json.Unmarshal(b, &generic); err != nil {
89                 return err
90         }
91         for k, v := range generic {
92                 ik, err := strconv.ParseInt(k, 10, 32)
93                 if err != nil {
94                         return err
95                 }
96
97                 er[EID(ik)] = v
98         }
99
100         return nil
101 }