Skip to main content

Python API Wrapper for PlayStation Network API with Home Assistant Support

Project description

PlayStation Logo PlayStation Network API Wrapper Python (PSNAWP)

Retrieve User Information, Trophies, Game and Store data from the PlayStation Network.

PyPI version Downloads python-logo Checked with pyright Documentation Status Poetry pre-commit pytest codecov Ruff License: MIT

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/psnawp_api
   init.py20100% 
   psnawp.py330100% 
src/psnawp_api/core
   init.py40100% 
   authenticator.py1311717 87%
   psnawp_exceptions.py110100% 
   request_builder.py7277 90%
src/psnawp_api/models
   init.py70100% 
   client.py800100% 
   game_title.py230100% 
   title_stats.py7111 99%
   user.py870100% 
src/psnawp_api/models/group
   init.py20100% 
   group.py560100% 
   group_datatypes.py424242 0%
src/psnawp_api/models/listing
   init.py20100% 
   pagination_iterator.py5222 96%
src/psnawp_api/models/search
   init.py20100% 
   search_datatypes.py740100% 
   universal_search.py622626 58%
src/psnawp_api/models/trophies
   init.py60100% 
   trophy.py10477 93%
   trophy_constants.py330100% 
   trophy_group.py650100% 
   trophy_summary.py200100% 
   trophy_titles.py700100% 
   trophy_utils.py60100% 
src/psnawp_api/utils
   init.py30100% 
   endpoints.py30100% 
   misc.py190100% 
TOTAL114210291% 

How to install

From PyPI

pip install PSNAWP

Important Links

PyPI: https://pypi.org/project/PSNAWP/

Read the Docs: https://psnawp.readthedocs.io/en/latest/

Getting Started

[!CAUTION] This library is an unofficial and reverse-engineered API wrapper for the PlayStation Network (PSN). It has been developed based on the reverse engineering of the PSN Android app. The API wrapper (>= v2.1.0) will self rate limit at 300 requests per 15 minutes. However, it is still important that you don't send bulk requests using this API. Excessive use of API may lead to your PSN account being temporarily or permanently banned.

You can also create a dedicated account to use this library so that in the worst-case scenario you do not lose access to your primary account, along with your video games and progress.

To get started you need to obtain npsso <64 character code>. You need to follow the following steps

  1. Login into your My PlayStation account.
  2. In another tab, go to https://ca.account.sony.com/api/v1/ssocookie
  3. If you are logged in you should see a text similar to this
{"npsso":"<64 character npsso code>"}

This npsso code will be used in the api for authentication purposes. The refresh token that is generated from npsso lasts about 2 months. After that you have to get a new npsso token. The bot will print a warning if there are less than 3 days left in refresh token expiration.

Following is the quick example on how to use this library

from psnawp_api import PSNAWP
from psnawp_api.models import SearchDomain
from psnawp_api.models.trophies import PlatformType

psnawp = PSNAWP("<64 character npsso code>")

# Your Personal Account Info
client = psnawp.me()
print(f"Online ID: {client.online_id}")
print(f"Account ID: {client.account_id}")
print(f"Region: {client.get_region()}")
print(f"Profile: {client.get_profile_legacy()} \n")

# Your Registered Devices
devices = client.get_account_devices()
for device in devices:
    print(f"Device: {device} \n")

# Your Friends List
friends_list = client.friends_list()
for friend in friends_list:
    print(f"Friend: {friend} \n")

# Your Players Blocked List
blocked_list = client.blocked_list()
for blocked_user in blocked_list:
    print(f"Blocked User: {blocked_user} \n")

# Your Friends in "Notify when available" List
available_to_play = client.available_to_play()
for user in available_to_play:
    print(f"Available to Play: {user} \n")

# Your trophies (PS4)
for trophy in client.trophies("NPWR22810_00", PlatformType.PS4):
    print(trophy)

# Your Chat Groups
groups = client.get_groups()
first_group_id = None  # This will be used later to test group methods
for id, group in enumerate(groups):
    if id == 0:  # Get the first group ID
        first_group_id = group.group_id

    group_info = group.get_group_information()
    print(f"Group {id}: {group_info} \n")

# Your Playing time (PS4, PS5 above only)
titles_with_stats = client.title_stats()
for title in titles_with_stats:
    print(
        f" \
        Game: {title.name} - \
        Play Count: {title.play_count} - \
        Play Duration: {title.play_duration} \n"
    )


# Other User's
example_user_1 = psnawp.user(online_id="VaultTec-Co")  # Get a PSN player by their Online ID
print(f"User 1 Online ID: {example_user_1.online_id}")
print(f"User 1 Account ID: {example_user_1.account_id}")
print(f"User 1 Region: {example_user_1.get_region()}")

print(example_user_1.profile())
print(example_user_1.prev_online_id)
print(example_user_1.get_presence())
print(example_user_1.friendship())
print(example_user_1.is_blocked())

# Example of getting a user by their account ID
user_account_id = psnawp.user(account_id="9122947611907501295")
print(f"User Account ID: {user_account_id.online_id}")


# Messaging and Groups Interaction
group = psnawp.group(group_id=first_group_id)  # This is the first group ID we got earlier - i.e. the first group in your groups list
print(group.get_group_information())
print(group.get_conversation(10))  # Get the last 10 messages in the group
print(group.send_message("Hello World"))
print(group.change_name("API Testing 3"))
# print(group.leave_group()) # Uncomment to leave the group

# Create a new group with other users - i.e. 'VaultTec-Co' and 'test'
example_user_2 = psnawp.user(online_id="test")
new_group = psnawp.group(users_list=[example_user_1, example_user_2])
print(new_group.get_group_information())
# You can use the same above methods to interact with the new group - i.e. send messages, change name, etc.

# Searching for Game Titles
search = psnawp.search(search_query="GTA 5", search_domain=SearchDomain.FULL_GAMES)
for search_result in search:
    print(search_result["result"]["invariantName"])

Note: If you want to create multiple instances of psnawp you need to get npsso code from separate PSN accounts. If you generate a new npsso with same account your previous npsso will expire immediately.

Contribution

All bug reposts and features requests are welcomed, although I am new at making python libraries, so it may take me a while to implement some features. Suggestions are welcomes if I am doing something that is an unconventional way of doing it.

Disclaimer

This project was not intended to be used for spam, abuse, or anything of the sort. Any use of this project for those purposes is not endorsed. Please keep this in mind when creating applications using this API wrapper.

Credit

This project contains code from PlayStationNetwork::API and PSN-PHP Wrapper that was translated to Python. Also, special thanks @andshrew for documenting the PlayStation Trophy endpoints. All licenses are included in this repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psnawp_ha-2.4.1.tar.gz (45.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

psnawp_ha-2.4.1-py3-none-any.whl (64.5 kB view details)

Uploaded Python 3

psnawp_ha-2.4.1-py2.py3-none-any.whl (61.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file psnawp_ha-2.4.1.tar.gz.

File metadata

  • Download URL: psnawp_ha-2.4.1.tar.gz
  • Upload date:
  • Size: 45.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.3 Darwin/24.4.0

File hashes

Hashes for psnawp_ha-2.4.1.tar.gz
Algorithm Hash digest
SHA256 0a295750f8988da541582c94b71fb8b6b2be901aa8c1a9d5713e7403c6576450
MD5 ec605e52f5a3c6115980025751a399ec
BLAKE2b-256 f39a033b10a3b250bae72f2d2fe62187483d1b83fe4d46cc409895f564aeee6a

See more details on using hashes here.

File details

Details for the file psnawp_ha-2.4.1-py3-none-any.whl.

File metadata

  • Download URL: psnawp_ha-2.4.1-py3-none-any.whl
  • Upload date:
  • Size: 64.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.3 Darwin/24.4.0

File hashes

Hashes for psnawp_ha-2.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0ddc1c82e8f84a94f517215f61cb766e426fb44c50232e9c36436cf0d17d1454
MD5 d18e0faf2190d262d466f3f161315298
BLAKE2b-256 766caa8dbb9811e6a612d0d714c1d230c752284d41d3e5bf2ec65b4d14d9aaf8

See more details on using hashes here.

File details

Details for the file psnawp_ha-2.4.1-py2.py3-none-any.whl.

File metadata

  • Download URL: psnawp_ha-2.4.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 61.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.3 Darwin/24.4.0

File hashes

Hashes for psnawp_ha-2.4.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b92714f99703b86058b736df81553615b09527a3acb1481aa54c0f6c4ae3b8d4
MD5 d2a6d19a0b6193c2394f80dc66b6caac
BLAKE2b-256 c3d7d9428a7aaa7330218d2cd999e542cc53d00063fff25e411aebbb7c4c1f9b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page