psn

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2021 License: MIT Imports: 7 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

This section is empty.

Functions

func NewPsnApi

func NewPsnApi(lang, region string) (*psn, error)

Creates new psn api

Types

type AvatarUrls

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

type ConsoleAvailability

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

type EarnedTrophies

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

type PersonalDetail

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

type Presences

type Presences struct {
}

type Profile

type Profile struct {
	OnlineID              string              `json:"onlineId"`
	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 ProfileResponse

type ProfileResponse struct {
	Profile Profile `json:"profile"`
}

type TrophiesResponse added in v1.0.0

type TrophiesResponse struct {
	Trophies []struct {
		TrophyID           int    `json:"trophyId"`
		TrophyHidden       bool   `json:"trophyHidden"`
		TrophyType         string `json:"trophyType"`
		TrophyName         string `json:"trophyName"`
		TrophyDetail       string `json:"trophyDetail"`
		TrophyIconURL      string `json:"trophyIconUrl"`
		TrophySmallIconURL string `json:"trophySmallIconUrl"`
		TrophyRare         int    `json:"trophyRare"`
		TrophyEarnedRate   string `json:"trophyEarnedRate"`
		FromUser           struct {
			OnlineID string `json:"onlineId"`
			Earned   bool   `json:"earned"`
		} `json:"fromUser,omitempty"`
	} `json:"trophies"`
}

type TrophyGroupResponse added in v0.0.4

type TrophyGroupResponse struct {
	TrophyTitleName     string `json:"trophyTitleName"`
	TrophyTitleDetail   string `json:"trophyTitleDetail"`
	TrophyTitleIconURL  string `json:"trophyTitleIconUrl"`
	TrophyTitlePlatfrom string `json:"trophyTitlePlatfrom"`
	DefinedTrophies     struct {
		Bronze   int `json:"bronze"`
		Silver   int `json:"silver"`
		Gold     int `json:"gold"`
		Platinum int `json:"platinum"`
	} `json:"definedTrophies"`
	TrophyGroups []struct {
		TrophyGroupID           string `json:"trophyGroupId"`
		TrophyGroupName         string `json:"trophyGroupName"`
		TrophyGroupDetail       string `json:"trophyGroupDetail"`
		TrophyGroupIconURL      string `json:"trophyGroupIconUrl"`
		TrophyGroupSmallIconURL string `json:"trophyGroupSmallIconUrl"`
		DefinedTrophies         struct {
			Bronze   int `json:"bronze"`
			Silver   int `json:"silver"`
			Gold     int `json:"gold"`
			Platinum int `json:"platinum"`
		} `json:"definedTrophies"`
		ComparedUser struct {
			OnlineID       string `json:"onlineId"`
			Progress       int    `json:"progress"`
			EarnedTrophies struct {
				Bronze   int `json:"bronze"`
				Silver   int `json:"silver"`
				Gold     int `json:"gold"`
				Platinum int `json:"platinum"`
			} `json:"earnedTrophies"`
			LastUpdateDate time.Time `json:"lastUpdateDate"`
		} `json:"comparedUser"`
	} `json:"trophyGroups"`
}

type TrophySummary

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

type TrophyTitleResponse added in v0.0.2

type TrophyTitleResponse struct {
	TotalResults int `json:"totalResults"`
	Offset       int `json:"offset"`
	Limit        int `json:"limit"`
	TrophyTitles []struct {
		NpCommunicationID       string `json:"npCommunicationId"`
		TrophyTitleName         string `json:"trophyTitleName"`
		TrophyTitleDetail       string `json:"trophyTitleDetail"`
		TrophyTitleIconURL      string `json:"trophyTitleIconUrl"`
		TrophyTitleSmallIconURL string `json:"trophyTitleSmallIconUrl"`
		TrophyTitlePlatfrom     string `json:"trophyTitlePlatfrom"` // typo in Sony's response
		HasTrophyGroups         bool   `json:"hasTrophyGroups"`
		DefinedTrophies         struct {
			Bronze   int `json:"bronze"`
			Silver   int `json:"silver"`
			Gold     int `json:"gold"`
			Platinum int `json:"platinum"`
		} `json:"definedTrophies"`
		ComparedUser struct {
			OnlineID       string `json:"onlineId"`
			Progress       int    `json:"progress"`
			EarnedTrophies struct {
				Bronze   int `json:"bronze"`
				Silver   int `json:"silver"`
				Gold     int `json:"gold"`
				Platinum int `json:"platinum"`
			} `json:"earnedTrophies"`
			LastUpdateDate time.Time `json:"lastUpdateDate"`
		} `json:"comparedUser"`
		FromUser struct {
			OnlineID       string `json:"onlineId"`
			Progress       int    `json:"progress"`
			EarnedTrophies struct {
				Bronze   int `json:"bronze"`
				Silver   int `json:"silver"`
				Gold     int `json:"gold"`
				Platinum int `json:"platinum"`
			} `json:"earnedTrophies"`
			HiddenFlag     bool      `json:"hiddenFlag"`
			LastUpdateDate time.Time `json:"lastUpdateDate"`
		} `json:"fromUser,omitempty"`
	} `json:"trophyTitles"`
}

Jump to

Keyboard shortcuts

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