Skip to content

Commit ef43869

Browse files
authored
trophies (#16)
1 parent 446aa20 commit ef43869

File tree

4 files changed

+84
-18
lines changed

4 files changed

+84
-18
lines changed

README.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
![build status](https://github.com/sizovilya/go-psn-api/actions/workflows/golangci-lint.yml/badge.svg?branch=main)
44
<p align="center"><img src="assets/gopher_ps_gamer.png" width="250"></p>
55

6-
# go-psn-api(WIP)
6+
# go-psn-api
77
A Playstation Network API wrapper written in Go.
88
## Read first
99
Corresponding to my research how PSN works you need npsso to interact with Sony servers.
@@ -44,10 +44,11 @@ Copy this js code:
4444
- After the login flow is completed, you should see a new log in the developer console that looks like: found npsso <64 character code>. Copy that 64 character code.
4545
</details>
4646

47-
### Functions at this moment
47+
### Functionality
4848
- You can get user profile info
4949
- You can get trophy titles
5050
- You can get trophy groups
51+
- You can get trophies
5152

5253
### Example
5354
```go
@@ -60,9 +61,9 @@ import (
6061

6162
func main() {
6263
ctx := context.Background()
63-
lang := "ru" // known list here https://github.com/sizovilya/go-psn-api/blob/main/langs.go, some languages in list are wrong and unsupported now, feel free to investigate for your own
64-
region := "ru" // known list here https://github.com/sizovilya/go-psn-api/blob/main/regions.go, some regions in list are wrong and unsupported now, feel free to investigate for your own
65-
npsso := "your npsso"
64+
lang := "ru" // known list here https://github.com/sizovilya/go-psn-api/blob/main/langs.go, some languages in list are wrong and unsupported now, feel free to investigate for your own and add it to list
65+
region := "ru" // known list here https://github.com/sizovilya/go-psn-api/blob/main/regions.go, some regions in list are wrong and unsupported now, feel free to investigate for your own and add it to list
66+
npsso := "<your npsso>"
6667
psnApi, err := psn.NewPsnApi(
6768
lang,
6869
region,
@@ -71,7 +72,7 @@ func main() {
7172
panic(err)
7273
}
7374

74-
// This request will get access token and refresh token from Sony's servers
75+
// This request will get access and refresh tokens from Sony's servers
7576
err = psnApi.AuthWithNPSSO(ctx, npsso)
7677
if err != nil {
7778
panic(err)
@@ -80,7 +81,7 @@ func main() {
8081
// If you obtain refresh token you may use it for next logins.
8182
// Next logins should be like this:
8283
// refreshToken, _ := psnApi.GetRefreshToken() // store refresh token somewhere for future logins by psnApi.AuthWithRefreshToken method
83-
err = psnApi.AuthWithRefreshToken(ctx, "your token") // get new access token
84+
err = psnApi.AuthWithRefreshToken(ctx, "<your token>") // will get new access token, feel free to manage tokens by yourself
8485
if err != nil {
8586
panic(err)
8687
}
@@ -106,6 +107,18 @@ func main() {
106107
panic(err)
107108
}
108109
fmt.Println(trophyGroups)
110+
111+
// How to get trophies in certain trophy title and trophy group
112+
trophies, err := psnApi.GetTrophies(
113+
ctx,
114+
"NPWR13348_00", // The Last Of Us 2
115+
"001", // trophy group with id = 001
116+
"geeek_52rus",
117+
)
118+
if err != nil {
119+
panic(err)
120+
}
121+
fmt.Println(trophies)
109122
}
110123

111124
```

trophy.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package psn
2+
3+
import (
4+
"context"
5+
"fmt"
6+
)
7+
8+
const trophiesApi = "-tpy.np.community.playstation.net/trophy/v1/trophyTitles/"
9+
10+
type TrophiesResponse struct {
11+
Trophies []struct {
12+
TrophyID int `json:"trophyId"`
13+
TrophyHidden bool `json:"trophyHidden"`
14+
TrophyType string `json:"trophyType"`
15+
TrophyName string `json:"trophyName"`
16+
TrophyDetail string `json:"trophyDetail"`
17+
TrophyIconURL string `json:"trophyIconUrl"`
18+
TrophySmallIconURL string `json:"trophySmallIconUrl"`
19+
TrophyRare int `json:"trophyRare"`
20+
TrophyEarnedRate string `json:"trophyEarnedRate"`
21+
FromUser struct {
22+
OnlineID string `json:"onlineId"`
23+
Earned bool `json:"earned"`
24+
} `json:"fromUser,omitempty"`
25+
} `json:"trophies"`
26+
}
27+
28+
// Method retrieves user's trophies
29+
func (p *psn) GetTrophies(ctx context.Context, trophyTitleId, trophyGroupId, username string) (*TrophiesResponse, error) {
30+
var h = headers{}
31+
h["authorization"] = fmt.Sprintf("Bearer %s", p.accessToken)
32+
h["Accept"] = "*/*"
33+
h["Accept-Encoding"] = "gzip, deflate, br"
34+
35+
trophiesResponse := TrophiesResponse{}
36+
err := p.get(
37+
ctx,
38+
fmt.Sprintf(
39+
"https://%s%s%s/trophyGroups/%s/trophies?fields=@default,trophyRare,trophyEarnedRate,trophySmallIconUrl&visibleType=1&comparedUser=%s&npLanguage=%s",
40+
p.region,
41+
trophiesApi,
42+
trophyTitleId,
43+
trophyGroupId,
44+
username,
45+
p.lang,
46+
),
47+
h,
48+
&trophiesResponse,
49+
)
50+
if err != nil {
51+
return nil, fmt.Errorf("can't do GET request: %w", err)
52+
}
53+
return &trophiesResponse, nil
54+
}

trophy_group.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ type TrophyGroupResponse struct {
4646
}
4747

4848
// Method retrieves user's trophy groups
49-
func (p *psn) GetTrophyGroups(ctx context.Context, trophyTitleId, username string) (profile *TrophyGroupResponse, err error) {
49+
func (p *psn) GetTrophyGroups(ctx context.Context, trophyTitleId, username string) (*TrophyGroupResponse, error) {
5050
var h = headers{}
5151
h["authorization"] = fmt.Sprintf("Bearer %s", p.accessToken)
5252
h["Accept"] = "*/*"
5353
h["Accept-Encoding"] = "gzip, deflate, br"
5454

55-
trophyTitleResponse := TrophyGroupResponse{}
56-
err = p.get(
55+
response := TrophyGroupResponse{}
56+
err := p.get(
5757
ctx,
5858
fmt.Sprintf(
5959
"https://%s%s%s/trophyGroups?fields=@default,trophyGroupSmallIconUrl&comparedUser=%s&npLanguage=%s",
@@ -64,10 +64,10 @@ func (p *psn) GetTrophyGroups(ctx context.Context, trophyTitleId, username strin
6464
p.lang,
6565
),
6666
h,
67-
&trophyTitleResponse,
67+
&response,
6868
)
6969
if err != nil {
7070
return nil, fmt.Errorf("can't do GET request: %w", err)
7171
}
72-
return &trophyTitleResponse, nil
72+
return &response, nil
7373
}

trophy_title.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,14 @@ type TrophyTitleResponse struct {
5353
}
5454

5555
// Method retrieves user's trophy titles
56-
func (p *psn) GetTrophyTitles(ctx context.Context, username string, limit, offset int32) (profile *TrophyTitleResponse, err error) {
56+
func (p *psn) GetTrophyTitles(ctx context.Context, username string, limit, offset int32) (*TrophyTitleResponse, error) {
5757
var h = headers{}
5858
h["authorization"] = fmt.Sprintf("Bearer %s", p.accessToken)
59-
//h["accept-language"] = "en-US"
6059
h["Accept"] = "*/*"
6160
h["Accept-Encoding"] = "gzip, deflate, br"
6261

63-
trophyTitleResponse := TrophyTitleResponse{}
64-
err = p.get(
62+
response := TrophyTitleResponse{}
63+
err := p.get(
6564
ctx,
6665
fmt.Sprintf(
6766
"https://%s%sfields=@default,trophyTitleSmallIconUrl&platform=PS3,PS4,PSVITA&limit=%d&offset=%d&comparedUser=%s&npLanguage=%s",
@@ -73,10 +72,10 @@ func (p *psn) GetTrophyTitles(ctx context.Context, username string, limit, offse
7372
p.lang,
7473
),
7574
h,
76-
&trophyTitleResponse,
75+
&response,
7776
)
7877
if err != nil {
7978
return nil, fmt.Errorf("can't do GET request: %w", err)
8079
}
81-
return &trophyTitleResponse, nil
80+
return &response, nil
8281
}

0 commit comments

Comments
 (0)