psn

package module
v0.0.0-...-23733be Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2024 License: MIT Imports: 10 Imported by: 0

README

Go Reference build status build status

go-psn-api

A Playstation Network API wrapper written in Go.

Read first

Corresponding to my research how PSN works you need npsso to interact with Sony servers. Instructions how to get it below.

How to get npsso

Fully described here - https://tusticles.com/psn-php/first_login.html

If link above doesn't work

Copy this js code:

(function(open) {
    XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {

        this.addEventListener("readystatechange", function() {
            if (this.readyState == XMLHttpRequest.DONE) {
                let response = JSON.parse(this.responseText);

                if (response && "npsso" in response) {
                    console.log('found npsso', response.npsso);
                }
            }
        }, false);

        open.call(this, method, url, async, user, pass);
    };

    window.onbeforeunload = function(){
        return 'Are you sure you want to leave?';
    };

})(XMLHttpRequest.prototype.open);
  • Navigate to https://account.sonyentertainmentnetwork.com/ in your browser and open your browser’s developer console
  • Paste the above Javascript into the console and then login.
  • 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.
Functionality
  • You can get user profile info
  • You can get trophy titles
  • You can get trophy groups
  • You can get trophies
Example
package main

import (
  "fmt"
  "github.com/sizovilya/go-psn-api"
)

func main() {
  ctx := context.Background()
  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
  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
  npsso := "<your npsso>"
  psnApi, err := psn.NewPsnApi(
    lang,
    region,
  )
  if err != nil {
    panic(err)
  }

  // This request will get access and refresh tokens from Sony's servers
  err = psnApi.AuthWithNPSSO(ctx, npsso)
  if err != nil {
    panic(err)
  }

  // If you obtain refresh token you may use it for next logins.
  // Next logins should be like this:
  // refreshToken, _ := psnApi.GetRefreshToken() // store refresh token somewhere for future logins by psnApi.AuthWithRefreshToken method
  err = psnApi.AuthWithRefreshToken(ctx, "<your token>") // will get new access token, feel free to manage tokens by yourself
  if err != nil {
    panic(err)
  }

  // How to get user's profile info
  profile, err := psnApi.GetProfileRequest(ctx, "geeek_52rus")
  if err != nil {
    panic(err)
  }
  fmt.Print(profile)

  // How to get trophy titles
  trophyTitles, err := psnApi.GetTrophyTitles(ctx, "geeek_52rus", 50, 0)
  if err != nil {
    panic(err)
  }
  fmt.Print(trophyTitles)

  // How to get trophy group by trophy title
  trophyTitleId := trophyTitles.TrophyTitles[0].NpCommunicationID // get first of them
  trophyGroups, err := psnApi.GetTrophyGroups(ctx, trophyTitleId, "geeek_52rus")
  if err != nil {
    panic(err)
  }
  fmt.Println(trophyGroups)

  // How to get trophies in certain trophy title and trophy group
  trophies, err := psnApi.GetTrophies(
  	ctx,
	"NPWR13348_00", // The Last of Us 2
	"001",         // trophy group with id = 001
	"geeek_52rus",
  )
  if err != nil {
      panic(err)
  }
  fmt.Println(trophies)
}

This project highly inspired by https://github.com/Tustin/psn-php. Some useful things like auth headers and params found in Tustin/psn-php.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNPSSOEmpty  = errors.New("npsso is empty")
	ErrNPSSOLength = errors.New("npsso must be exactly 64 characters")
)

Functions

This section is empty.

Types

type Api

type Api struct {
	*Authenticator
	// contains filtered or unexported fields
}

func (*Api) GetProfileRequest

func (api *Api) GetProfileRequest(ctx context.Context, username string) (profile *Profile, err error)

Method retrieves user profile info by PSN id

func (*Api) GetTrophies

func (api *Api) GetTrophies(ctx context.Context, titleId, trophyGroupId, username string) ([]Trophy, error)

Method retrieves user's trophies

func (*Api) GetTrophyGroups

func (api *Api) GetTrophyGroups(ctx context.Context, trophyTitleId, username string) (TrophyTitle, error)

Method retrieves user's trophy groups

func (*Api) GetTrophyTitles

func (api *Api) GetTrophyTitles(ctx context.Context, username string, limit, offset int) ([]TrophyTitleFull, error)

GetTrophyTitles retrieves a user's trophy titles

func (*Api) RefreashAccessToken

func (api *Api) RefreashAccessToken(ctx context.Context) error

type Authenticator

type Authenticator struct {
	// contains filtered or unexported fields
}

func NewApi

func NewApi(opts ...Options) *Authenticator

New API creates a new API caller Default langage and region are the first in SupportedLanguages and SupportedRegions resp.

func (*Authenticator) Authenticate

func (auth *Authenticator) Authenticate(ctx context.Context, npsso string) (*Api, error)

type AvatarUrls

type AvatarUrls struct {
	Size      string `json:"size"`
	AvatarURL string `json:"avatarUrl"`
}

type ConsoleAvailability

type ConsoleAvailability struct {
	AvailabilityStatus string `json:"availabilityStatus"`
}

type Language

type Language string

TODO: candidate for code generation

var (
	LangJA   Language = "ja"
	LangEN   Language = "en"
	LangENGB Language = "en-GB"
	LangENUS Language = "en-US"
	LangFR   Language = "fr"
	LangES   Language = "es"
	LangESMX Language = "es-MX"
	LangDE   Language = "de"
	LangIT   Language = "it"
	LangNL   Language = "nl"
	LangPT   Language = "pt"
	LangPTBR Language = "pt-BR"
	LangRU   Language = "ru"
	LangPL   Language = "pl"
	LangFI   Language = "fi"
	LangDA   Language = "da"
	LangNO   Language = "no"
	LangSV   Language = "sv"
	LangTR   Language = "tr"
	LangKO   Language = "ko"
	LangZHCN Language = "zh-CN"
	LangZHTW Language = "zh-TW"
)

func SupportedLanguages

func SupportedLanguages() []Language

type Options

type Options func(c *config)

func WithClient

func WithClient(client *http.Client) (Options, error)

func WithLanguage

func WithLanguage(lang Language) (Options, error)

func WithRegion

func WithRegion(region Region) (Options, error)

type PersonalDetail

type PersonalDetail struct {
	FirstName string `json:"firstName"`
	LastName  string `json:"lastName"`
}

type Presences

type Presences struct {
}

type Profile

type Profile struct {
	User
	NpID                  string              `json:"npId"`
	AvatarUrls            []AvatarUrls        `json:"avatarUrls"`
	Plus                  int                 `json:"plus"`
	AboutMe               string              `json:"aboutMe"`
	LanguagesUsed         []string            `json:"languagesUsed"`
	TrophySummary         TrophySummary       `json:"trophySummary"`
	IsOfficiallyVerified  bool                `json:"isOfficiallyVerified"`
	PersonalDetail        PersonalDetail      `json:"personalDetail"`
	PersonalDetailSharing string              `json:"personalDetailSharing"`
	PrimaryOnlineStatus   string              `json:"primaryOnlineStatus"`
	Presences             []Presences         `json:"presences"`
	FriendRelation        string              `json:"friendRelation"`
	Blocking              bool                `json:"blocking"`
	MutualFriendsCount    int                 `json:"mutualFriendsCount"`
	Following             bool                `json:"following"`
	FollowerCount         int                 `json:"followerCount"`
	ConsoleAvailability   ConsoleAvailability `json:"consoleAvailability"`
}

type Region

type Region string

TODO: candidate for code generation

var (
	RegionUS   Region = "us"
	RegionCA   Region = "ca"
	RegionMX   Region = "mx"
	RegionCL   Region = "cl"
	RegionPE   Region = "pe"
	RegionAR   Region = "ar"
	RegionCO   Region = "co"
	RegionBR   Region = "br"
	RegionGB   Region = "gb"
	RegionIE   Region = "ie"
	RegionBE   Region = "be"
	RegionLU   Region = "lu"
	RegionNL   Region = "nl"
	RegionFR   Region = "fr"
	RegionDE   Region = "de"
	RegionAT   Region = "at"
	RegionCH   Region = "ch"
	RegionIT   Region = "it"
	RegionPT   Region = "pt"
	RegionDK   Region = "dk"
	RegionFI   Region = "fi"
	RegionNO   Region = "no"
	RegionSE   Region = "se"
	RegionAU   Region = "au"
	RegionNZ   Region = "nz"
	RegionES   Region = "es"
	RegionRU   Region = "ru"
	RegionAE   Region = "ae"
	RegionZA   Region = "za"
	RegionPL   Region = "pl"
	RegionGR   Region = "gr"
	RegionSA   Region = "sa"
	RegionCZ   Region = "cz"
	RegionBG   Region = "bg"
	RegionHR   Region = "hr"
	RegionRO   Region = "ro"
	RegionSI   Region = "si"
	RegionHU   Region = "hu"
	RegionSK   Region = "sk"
	RegionTR   Region = "tr"
	RegionBH   Region = "bh"
	RegionKW   Region = "kw"
	RegionLB   Region = "lb"
	RegionOM   Region = "om"
	RegionQA   Region = "qa"
	RegionIL   Region = "il"
	RegionMT   Region = "mt"
	RegionIS   Region = "is"
	RegionCY   Region = "cy"
	RegionIN   Region = "in"
	RegionUA   Region = "ua"
	RegionHK   Region = "hk"
	RegionTW   Region = "tw"
	RegionSG   Region = "sg"
	RegionMY   Region = "my"
	RegionID   Region = "id"
	RegionTH   Region = "th"
	RegionJP   Region = "jp"
	RegionKR   Region = "kr"
	RegionENUS Region = "en-US"
)

func SupportedRegions

func SupportedRegions() []Region

type Trophy

type Trophy struct {
	ID           int        `json:"trophyId"`
	Hidden       bool       `json:"trophyHidden"`
	Type         string     `json:"trophyType"`
	Name         string     `json:"trophyName"`
	Detail       string     `json:"trophyDetail"`
	IconURL      string     `json:"trophyIconUrl"`
	SmallIconURL string     `json:"trophySmallIconUrl"`
	Rare         int        `json:"trophyRare"`
	EarnedRate   string     `json:"trophyEarnedRate"`
	FromUser     UserEarned `json:"fromUser"`
}

type TrophyCount

type TrophyCount struct {
	Platinum int `json:"platinum"`
	Gold     int `json:"gold"`
	Silver   int `json:"silver"`
	Bronze   int `json:"bronze"`
}

type TrophyGroup

type TrophyGroup struct {
	ID              string       `json:"trophyGroupId"`
	Name            string       `json:"trophyGroupName"`
	Detail          string       `json:"trophyGroupDetail"`
	IconURL         string       `json:"trophyGroupIconUrl"`
	SmallIconURL    string       `json:"trophyGroupSmallIconUrl"`
	DefinedTrophies TrophyCount  `json:"definedTrophies"`
	ComparedUser    UserProgress `json:"comparedUser"`
}

type TrophySummary

type TrophySummary struct {
	Level          int         `json:"level"`
	Progress       int         `json:"progress"`
	EarnedTrophies TrophyCount `json:"earnedTrophies"`
}

type TrophyTitle

type TrophyTitle struct {
	Name            string        `json:"trophyTitleName"`
	Detail          string        `json:"trophyTitleDetail"`
	IconURL         string        `json:"trophyTitleIconUrl"`
	Platfrom        string        `json:"trophyTitlePlatfrom"` // typo in Sony's response
	DefinedTrophies TrophyCount   `json:"definedTrophies"`
	TrophyGroups    []TrophyGroup `json:"trophyGroups"`
}

type TrophyTitleFull

type TrophyTitleFull struct {
	TrophyTitle
	NpCommunicationID       string       `json:"npCommunicationId"`
	TrophyTitleSmallIconURL string       `json:"trophyTitleSmallIconUrl"`
	HasTrophyGroups         bool         `json:"hasTrophyGroups"`
	ComparedUser            UserProgress `json:"comparedUser"`
	FromUser                UserProgress `json:"fromUser"`
}

type User

type User struct {
	OnlineID string `json:"onlineId"`
}

type UserEarned

type UserEarned struct {
	User
	Earned bool `json:"earned"`
}

type UserProgress

type UserProgress struct {
	User
	Progress       int         `json:"progress"`
	EarnedTrophies TrophyCount `json:"earnedTrophies"`
	HiddenFlag     bool        `json:"hiddenFlag"`
	LastUpdateDate time.Time   `json:"lastUpdateDate"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL