forked from sselph/scraper
/
daphne.go
66 lines (60 loc) · 1.24 KB
/
daphne.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
package ds
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/sselph/scraper/gdb"
)
// Daphne is a data source using GDB for Daphne games.
type Daphne struct {
HM *HashMap
}
// GetID implements DS.
func (d *Daphne) getID(p string) (string, error) {
if filepath.Ext(p) != ".daphne" {
return "", ErrNotFound
}
gameID := filepath.Base(p)
switch {
case strings.HasPrefix(gameID, "lair2_"):
gameID = "lair2_*.daphne"
case strings.HasPrefix(gameID, "sdq"):
gameID = "sdq*.daphne"
case strings.HasPrefix(gameID, "tq"):
gameID = "tq*.daphne"
}
id, ok := d.HM.ID(gameID)
if !ok {
return "", ErrNotFound
}
return id, nil
}
// GetName implements DS.
func (d *Daphne) GetName(p string) string {
gameID := filepath.Base(p)
n, ok := d.HM.Name(gameID)
if !ok {
return ""
}
return n
}
// GetGame implements DS.
func (d *Daphne) GetGame(ctx context.Context, p string) (*Game, error) {
id, err := d.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
}