Skip to main content

An up to date implementation of the PiShock and OpenShock APIs in Python. Includes functionality as a library and as a command line program.

Project description

PyShock ⚡

PyPI - Version PyPI - License PyPI - Python Version build

A synchronous Python client and CLI for controlling PiShock and OpenShock devices, based on the new unified PiShock API and the OpenShock API.

Control a shocker from the terminal, or add control to your program!

Install

For the command line tool:

uv tool install 'pyshock[cli]'

Use it as a library in your python program:

uv add pyshock

Usage

Set up credentials:

pyshock init

Control a device:

pyshock shock --duration 2 --intensity 75 --shocker-id 00000

# or, if you only have one shocker associated with your account:
pyshock shock 2 75

Run pyshock info 00000 for device details. Run pyshock devices to list devices and capabilities.

Note: Due to a limitation in the Pishock API, you will need to share your own shocker with yourself in order to use it via the API or CLI.

Authentication

Pass credentials on the command line or cache them with pyshock init.

With a flag:

pyshock --key KEY shock --duration 2 --intensity 15 --shocker-id 00000

Interactively:

pyshock auth

Enter your API key: _____

Or in one step:

pyshock auth --key KEY

PyShock stores credentials in your user configuration folder. Refresh with pyshock devices.

Be advised, your API key will be stored in plain text.

API

Each API client caches the result of list_shockers() for its lifetime. Call list_shockers(refresh=True) to fetch current data.

PiShockAPI

from pyshock import PiShockAPI, ShockerOperation

with PiShockAPI(api_key="key") as api:
    for shocker in api.list_shockers():
        print(shocker.name, shocker.shocker_id)

    shared_shocker = api.get_shocker_by_share_code("ABC123456")

    api.operate_shocker(
        shocker=shared_shocker,
        operation=ShockerOperation.SHOCK,
        duration=2000,  # 2 seconds
        intensity=50,
    )

    api.delete_share(share_id=12345)

Operations:

ShockerOperation.SHOCK, VIBRATE, BEEP. Duration in milliseconds (16-15000), intensity 0-100.

[!NOTE] Shockers may appear in both owned and shared provider responses.

PyShock returns one record per shocker ID, which combine 'owned device' metadata with permissions and limits attached to shared devices.

OpenShockAPI

from pyshock import OpenShockAPI, ShockerOperation

# Token authentication (limited endpoints)
with OpenShockAPI(api_token="token") as api:
    for shocker in api.list_shockers():
        print(shocker.name, shocker.shocker_id)
        api.operate_shocker(
            shocker=shocker,
            operation=ShockerOperation.SHOCK,
            duration=2000,
            intensity=50,
        )

# Cookie authentication (account and supported sharing endpoints)
from http.cookiejar import MozillaCookieJar

jar = MozillaCookieJar("cookies.txt")
jar.load(ignore_discard=True, ignore_expires=True)
cookie = jar["openShockSession"].value

with OpenShockAPI(session_cookie=cookie) as api:
    account = api.get_account()
    print(account.username)

    api.link_share_code("01234567-89ab-cdef-0123-456789abcdef")

Operations:

ShockerOperation.SHOCK, VIBRATE, BEEP. Duration in milliseconds (300-65535), intensity 0-100.


[!NOTE] PyShock validates the contents of provider responses.

Malformed or unexpected responses will raise APIError rather than being treated as empty results or discarded.

Requirements

Python 3.10+.

Niquests for the library.

The [cli] extra adds cyclopts, platformdirs, and rich for the terminal interface.

License

Your choice of AGPLv3-or-later or commercial. See LICENSE for more information.

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

pyshock-0.2.0.tar.gz (42.5 kB view details)

Uploaded Source

Built Distribution

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

pyshock-0.2.0-py3-none-any.whl (52.4 kB view details)

Uploaded Python 3

File details

Details for the file pyshock-0.2.0.tar.gz.

File metadata

  • Download URL: pyshock-0.2.0.tar.gz
  • Upload date:
  • Size: 42.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyshock-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e28f1b5ec3eb9f9f0e06f4be3edd9f2e3c3253bf6aea434514bbc11a920aa2ef
MD5 52a63599d08c51ffa78b4c4b164eea6b
BLAKE2b-256 143e12cb100f1d7709d9ea2894bd0459ba42ec3c3a4c48bb214f32b6235eea63

See more details on using hashes here.

File details

Details for the file pyshock-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pyshock-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 52.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyshock-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 778cb13e7e70df020233076fccfadd425aef2e44c77ea0435332df4f57ab2f7f
MD5 6627b0e8b836f332294836b9e141a0fb
BLAKE2b-256 37df73d9ad11dc62f76f7c0dc7b121ac39922b8e7154dc6e89691941b4dc36ef

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