|
1 | 1 | package howlongtobeat |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
| 4 | + "context" |
5 | 5 | "fmt" |
6 | | - "io/ioutil" |
7 | | - "net/http" |
8 | | - "net/url" |
9 | | - "strings" |
10 | | - "time" |
11 | 6 |
|
12 | | - "github.com/botlabs-gg/yagpdb/v2/commands" |
13 | | - "github.com/botlabs-gg/yagpdb/v2/lib/jarowinkler" |
| 7 | + "github.com/forbiddencoding/howlongtobeat" |
14 | 8 | ) |
15 | 9 |
|
16 | 10 | func getGameData(searchTitle string) ([]hltb, error) { |
17 | | - reqData := &hltbRequest{ |
18 | | - SearchType: "games", |
19 | | - SearchTerms: []string{searchTitle}, |
20 | | - } |
21 | | - u := &url.URL{ |
22 | | - Scheme: hltbScheme, |
23 | | - Host: hltbHost, |
24 | | - Path: hltbHostPath, |
25 | | - } |
26 | | - |
27 | | - urlStr := u.String() |
28 | | - body, err := json.Marshal(reqData) |
29 | | - if err != nil { |
30 | | - return nil, commands.NewPublicError("Failed Parsing Query") |
31 | | - } |
32 | | - client := &http.Client{} |
33 | | - r, _ := http.NewRequest("POST", urlStr, strings.NewReader(string(body))) |
34 | | - r.Header.Add("Content-Type", "application/json") |
35 | | - r.Header.Add("Accept", "*/*") |
36 | | - r.Header.Add("User-Agent", "YAGPDB.xyz (https://github.com/botlabs-gg/yagpdb)") |
37 | | - r.Header.Add("origin", "https://howlongtobeat.com") |
38 | | - r.Header.Add("referer", "https://howlongtobeat.com/") |
39 | | - |
40 | | - resp, err := client.Do(r) |
| 11 | + hltb, err := howlongtobeat.New() |
41 | 12 | if err != nil { |
42 | 13 | return nil, err |
43 | 14 | } |
44 | | - |
45 | | - if resp.StatusCode != 200 { |
46 | | - return nil, commands.NewPublicError("Unable to fetch data from howlongtobeat.com") |
47 | | - } |
48 | | - r.Body.Close() |
49 | | - |
50 | | - bytes, err := ioutil.ReadAll(resp.Body) |
| 15 | + searchResults, err := hltb.SearchSimple(context.TODO(), searchTitle, howlongtobeat.SearchModifierNone) |
51 | 16 | if err != nil { |
52 | 17 | return nil, err |
53 | 18 | } |
54 | 19 |
|
55 | | - var hltbData struct { |
56 | | - Games []hltb `json:"data"` |
57 | | - } |
58 | | - err = json.Unmarshal(bytes, &hltbData) |
59 | | - if err != nil { |
60 | | - return nil, commands.NewPublicError("Error parsing response from howlongtobeat.com") |
61 | | - } |
62 | | - |
63 | | - return hltbData.Games, nil |
| 20 | + return parseGameData(searchResults), nil |
64 | 21 | } |
65 | 22 |
|
66 | | -func parseGameData(gameName string, games []hltb) []hltb { |
| 23 | +func parseGameData(games []*howlongtobeat.SearchGameSimple) []hltb { |
67 | 24 | var parsedGames []hltb |
68 | 25 | var parsingGame hltb |
69 | 26 | for _, game := range games { |
70 | | - parsingGame = game |
71 | | - parsingGame.MainStoryTimeHours = fmt.Sprintf("%.2f Hours ", (time.Second * time.Duration(game.MainStoryTime)).Hours()) |
72 | | - parsingGame.MainStoryExtraTimeHours = fmt.Sprintf("%.2f Hours ", (time.Second * time.Duration(game.MainExtraTime)).Hours()) |
73 | | - parsingGame.CompletionistTimeHours = fmt.Sprintf("%.2f Hours ", (time.Second * time.Duration(game.CompletionistTime)).Hours()) |
74 | | - parsingGame.JaroWinklerSimilarity = jarowinkler.Similarity([]rune(gameName), []rune(game.GameTitle)) |
75 | | - parsingGame.GameURL = fmt.Sprintf("%s://%s/game/%d", hltbScheme, hltbHost, games[0].GameID) |
76 | | - parsingGame.ImageURL = fmt.Sprintf("%s://%s/games/%s", hltbScheme, hltbHost, games[0].ImagePath) |
| 27 | + parsingGame = hltb{ |
| 28 | + GameTitle: game.GameName, |
| 29 | + GameID: int64(game.GameID), |
| 30 | + ImagePath: game.GameImage, |
| 31 | + MainStoryTime: int64(game.CompMain), |
| 32 | + MainStoryTimeHours: fmt.Sprintf("%.2f Hours ", game.CompMain), |
| 33 | + MainExtraTime: int64(game.CompPlus), |
| 34 | + MainStoryExtraTimeHours: fmt.Sprintf("%.2f Hours ", game.CompPlus), |
| 35 | + CompletionistTime: int64(game.CompAll), |
| 36 | + CompletionistTimeHours: fmt.Sprintf("%.2f Hours ", game.CompAll), |
| 37 | + Similarity: game.Similarity, |
| 38 | + GameURL: fmt.Sprintf("%s://%s/game/%d", hltbScheme, hltbHost, game.GameID), |
| 39 | + ImageURL: fmt.Sprintf("%s://%s/games/%s", hltbScheme, hltbHost, game.GameImage), |
| 40 | + } |
77 | 41 | parsedGames = append(parsedGames, parsingGame) |
78 | 42 | } |
79 | 43 | return parsedGames |
|
0 commit comments