-
Notifications
You must be signed in to change notification settings - Fork 137
/
box.go
146 lines (121 loc) · 3.48 KB
/
box.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
135
136
137
138
139
140
141
142
143
144
145
146
package steambox
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"math"
"os"
"sort"
"strings"
"unicode/utf8"
steam "github.com/YouEclipse/steam-go/pkg"
"github.com/google/go-github/github"
)
// Box defines the steam box.
type Box struct {
steam *steam.Client
github *github.Client
}
// NewBox creates a new Box with the given API key.
func NewBox(apikey string, ghUsername, ghToken string) *Box {
box := &Box{}
box.steam = steam.NewClient(apikey, nil)
tp := github.BasicAuthTransport{
Username: strings.TrimSpace(ghUsername),
Password: strings.TrimSpace(ghToken),
}
box.github = github.NewClient(tp.Client())
return box
}
// GetGist gets the gist from github.com.
func (b *Box) GetGist(ctx context.Context, id string) (*github.Gist, error) {
gist, _, err := b.github.Gists.Get(ctx, id)
if err != nil {
return nil, err
}
return gist, nil
}
// UpdateGist updates the gist.
func (b *Box) UpdateGist(ctx context.Context, id string, gist *github.Gist) error {
_, _, err := b.github.Gists.Edit(ctx, id, gist)
return err
}
// GetPlayTime gets the paytime form steam web API.
func (b *Box) GetPlayTime(ctx context.Context, steamID uint64, appID ...uint32) ([]string, error) {
params := &steam.GetOwnedGamesParams{
SteamID: steamID,
IncludeAppInfo: true,
IncludePlayedFreeGames: true,
}
if len(appID) > 0 {
params.AppIDsFilter = appID
}
gameRet, err := b.steam.IPlayerService.GetOwnedGames(ctx, params)
if err != nil {
return nil, err
}
var lines []string
var max = 0
sort.Slice(gameRet.Games, func(i, j int) bool {
return gameRet.Games[i].PlaytimeForever > gameRet.Games[j].PlaytimeForever
})
for _, game := range gameRet.Games {
if max >= 5 {
break
}
hours := int(math.Floor(float64(game.PlaytimeForever / 60)))
mins := int(math.Floor(float64(game.PlaytimeForever % 60)))
line := pad(getNameEmoji(game.Appid, game.Name), " ", 35) + " " +
pad(fmt.Sprintf("🕘 %d hrs %d mins", hours, mins), "", 16)
lines = append(lines, line)
max++
}
return lines, nil
}
func (b *Box) UpdateMarkdown(ctx context.Context, title, filename string, content []byte) error {
md, err := ioutil.ReadFile(filename)
if err != nil {
return fmt.Errorf("steambox.UpdateMarkdown: Error reade a file: %w", err)
}
start := []byte("<!-- steam-box start -->")
before := md[:bytes.Index(md, start)+len(start)]
end := []byte("<!-- steam-box end -->")
after := md[bytes.Index(md, end):]
newMd := bytes.NewBuffer(nil)
newMd.Write(before)
newMd.WriteString("\n" + title + "\n")
newMd.WriteString("```text\n")
newMd.Write(content)
newMd.WriteString("\n")
newMd.WriteString("```\n")
newMd.WriteString("<!-- Powered by https://github.com/YouEclipse/steam-box . -->\n")
newMd.Write(after)
err = ioutil.WriteFile(filename, newMd.Bytes(), os.ModeAppend)
if err != nil {
return fmt.Errorf("steambox.UpdateMarkdown: Error write a file: %w", err)
}
return nil
}
func pad(s, pad string, targetLength int) string {
padding := targetLength - utf8.RuneCountInString(s)
if padding <= 0 {
return s
}
return s + strings.Repeat(pad, padding)
}
func getNameEmoji(id int, name string) string {
// hard code some game's emoji
var nameEmojiMap = map[int]string{
730: "🔫 ", // CS:GO
271590: "🚓 ", // GTA 5
578080: "🍳 ", // PUBG
431960: "💻 ", // Wallpaper Engine
8930: "🌏 ", // Sid Meier's Civilization V
359550: "🔫 ", // Tom Clancy's Rainbow Six Siege
}
if emoji, ok := nameEmojiMap[id]; ok {
return emoji + name
}
return "🎮 " + name
}