-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_infos.go
60 lines (53 loc) · 1.17 KB
/
image_infos.go
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
package fanarttv
import (
"encoding/json"
"sort"
"strconv"
)
// ImageInfo represent an image with its associated infos
type ImageInfo struct {
ID string `json:"id"`
URL string `json:"url"`
Lang string `json:"lang"`
Likes int `json:"likes"`
// Only for the shows
Season string `json:"season,omitempty"`
}
// UnmarshalJSON is a custom unmarshal function to handle likes as ints
func (i *ImageInfo) UnmarshalJSON(data []byte) error {
aux := struct {
ID *string `json:"id"`
URL *string `json:"url"`
Lang *string `json:"lang"`
Season *string `json:"season"`
Likes string `json:"likes"`
}{
ID: &i.ID,
URL: &i.URL,
Lang: &i.Lang,
Season: &i.Season,
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
// Convert the likes into int
likes, err := strconv.Atoi(aux.Likes)
if err != nil {
return err
}
i.Likes = likes
return nil
}
// Best returns the best image based on the likes
func Best(imgs []*ImageInfo) *ImageInfo {
size := len(imgs)
switch size {
case 0:
return nil
case 1:
return imgs[0]
default:
sort.Slice(imgs, func(i, j int) bool { return imgs[i].Likes > imgs[j].Likes })
return imgs[0]
}
}