-
Notifications
You must be signed in to change notification settings - Fork 88
/
gdb.go
134 lines (123 loc) · 3.02 KB
/
gdb.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
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
package ds
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/sselph/scraper/gdb"
)
// GDB is a DataSource using thegamesdb.net
type GDB struct {
HM *HashMap
Hasher *Hasher
}
// getFront gets the front boxart for a Game if it exists.
func getFront(g gdb.Game) *gdb.Image {
for _, v := range g.BoxArt {
if v.Side == "front" {
return &v
}
}
return nil
}
// toXMLDate converts a gdb date to the gamelist.xml date.
func toXMLDate(d string) string {
switch len(d) {
case 10:
t, _ := time.Parse("01/02/2006", d)
return t.Format("20060102T000000")
case 4:
return fmt.Sprintf("%s0101T000000", d)
}
return ""
}
// Hash hashes a ROM.
func (g *GDB) Hash(p string) (string, error) {
return g.Hasher.Hash(p)
}
// getID gets the ID from the path.
func (g *GDB) getID(p string) (string, error) {
h, err := g.Hasher.Hash(p)
if err != nil {
return "", err
}
id, ok := g.HM.ID(h)
if !ok {
return "", ErrNotFound
}
return id, nil
}
// GetName implements DS
func (g *GDB) GetName(p string) string {
h, err := g.Hasher.Hash(p)
if err != nil {
return ""
}
name, ok := g.HM.Name(h)
if !ok {
return ""
}
return name
}
// GetGame implements DS
func (g *GDB) GetGame(ctx context.Context, p string) (*Game, error) {
id, err := g.getID(p)
if err != nil {
return nil, err
}
req := gdb.GGReq{ID: id}
resp, err := gdb.GetGame(ctx, req)
if err != nil {
return nil, err
}
if len(resp.Game) == 0 {
return nil, fmt.Errorf("game with id (%s) not found", id)
}
game := resp.Game[0]
ret := ParseGDBGame(game, resp.ImageURL)
ret.ID = id
return ret, nil
}
// ParseGDBGame parses a gdb.Game and returns a Game.
func ParseGDBGame(game gdb.Game, imgURL string) *Game {
ret := NewGame()
if len(game.Screenshot) != 0 {
ret.Images[ImgScreen] = HTTPImage{URL: imgURL + game.Screenshot[0].Original.URL}
ret.Thumbs[ImgScreen] = HTTPImage{URL: imgURL + game.Screenshot[0].Thumb}
}
front := getFront(game)
if front != nil {
ret.Images[ImgBoxart] = HTTPImage{URL: imgURL + front.URL}
ret.Thumbs[ImgBoxart] = HTTPImage{URL: imgURL + front.Thumb}
}
if len(game.FanArt) != 0 {
ret.Images[ImgFanart] = HTTPImage{URL: imgURL + game.FanArt[0].Original.URL}
ret.Thumbs[ImgFanart] = HTTPImage{URL: imgURL + game.FanArt[0].Thumb}
}
if len(game.Banner) != 0 {
ret.Images[ImgBanner] = HTTPImage{URL: imgURL + game.Banner[0].URL}
ret.Thumbs[ImgBanner] = HTTPImage{URL: imgURL + game.Banner[0].URL}
}
if len(game.ClearLogo) != 0 {
ret.Images[ImgLogo] = HTTPImage{URL: imgURL + game.ClearLogo[0].URL}
ret.Thumbs[ImgLogo] = HTTPImage{URL: imgURL + game.ClearLogo[0].URL}
}
var genre string
if len(game.Genres) >= 1 {
genre = game.Genres[0]
}
ret.GameTitle = game.GameTitle
ret.Overview = game.Overview
ret.Rating = game.Rating / 10.0
ret.ReleaseDate = toXMLDate(game.ReleaseDate)
ret.Developer = game.Developer
ret.Publisher = game.Publisher
ret.Genre = genre
ret.Source = "theGamesDB.net"
p, err := strconv.ParseInt(strings.TrimRight(game.Players, "+"), 10, 32)
if err == nil {
ret.Players = p
}
return ret
}