Skip to main content

PNW subscription utility

Project description

Politics and War Subscriptions

A Python package for subscribing to real-time Politics and War game events via the PnW API.

Features

  • 🎨 Nation Color Changes - Track when nations change their color bloc
  • 🌍 Nations - Monitor nation creation, updates, and deletions
  • 📊 Trades - Monitor trade offers in real-time
  • ⚔️ Wars - Track war declarations and outcomes
  • 💥 Attacks - Monitor individual war attacks as they happen
  • 🤝 Alliances - Watch alliance changes
  • 🏙️ Cities - Track city creation and improvements
  • 💰 Bank Records - Monitor alliance bank transactions
  • 📜 Treaties - Track treaty creation and cancellation
  • 🎯 Bounties - Monitor bounty creation and claims
  • Baseball Games - Track baseball game results
  • 💎 Treasure Trades - Monitor treasure trading
  • 🚫 Embargoes - Track embargo creation and removal
  • 🐍 Pythonic API - Simple, clean interface
  • Real-time - Powered by Pusher websockets
  • 🔄 Automatic Reconnection - Handles connection issues gracefully

Prerequisites

  • Python 3.7+
  • Node.js 14+ (with npm)
  • A Politics and War API key

Installation

  1. Install the Python package:
pip install -e .
  1. Install Node.js dependencies:
cd pnw_subscriptions/scripts
npm install pusher-js node-fetch

Quick Start

Monitor Nation Color Changes

from pnw_subscriptions import Subscription

def handle_event(event):
    print(f"{event['nation_name']} changed from {event['old_color']} to {event['new_color']}")

sub = Subscription(
    api_key="your_api_key_here",
    subscription_type="nation_colors"
)

sub.start(callback=handle_event)

Monitor Trade Market

from pnw_subscriptions import Subscription

def handle_trade(event):
    if event['type'] == 'TRADE_CREATED':
        print(f"New {event['buy_or_sell']} offer: {event['amount']} {event['offer_resource']} @ ${event['price']}")

sub = Subscription(
    api_key="your_api_key_here",
    subscription_type="trades"
)

sub.start(callback=handle_trade)

Monitor Multiple Subscriptions Simultaneously

from pnw_subscriptions import MultiSubscription

def handle_colors(event):
    print(f"Color change: {event['nation_name']}")

def handle_trades(event):
    print(f"Trade: {event['offer_resource']}")

# Create multi-subscription manager
multi = MultiSubscription(api_key="your_api_key_here")

# Add multiple subscriptions
multi.add_subscription("nation_colors", handle_colors)
multi.add_subscription("trades", handle_trades)

# Start all subscriptions (blocks until Ctrl+C)
multi.start_all()

Event Types

Nation Color Changes

{
    "type": "COLOR_CHANGE",
    "nation_id": "123456",
    "nation_name": "Example Nation",
    "alliance_name": "Example Alliance",
    "old_color": "aqua",
    "new_color": "red",
    "beige_turns_left": 0,
    "score": 1500.0
}

Trade Events

{
    "type": "TRADE_CREATED",  # or TRADE_UPDATED, TRADE_DELETED
    "trade_id": "789012",
    "offer_resource": "food",
    "buy_or_sell": "sell",
    "price": 50.0,
    "amount": 1000,
    "nation_id": "123456",
    "nation_name": "Example Nation",
    "date_accepted": null,
    "original_trade_id": null
}

Advanced Usage

Multiple Subscriptions with Context Manager

from pnw_subscriptions import MultiSubscription

with MultiSubscription(api_key="your_key") as multi:
    multi.add_subscription("nation_colors", handle_colors)
    multi.add_subscription("trades", handle_trades)
    multi.start_all()
# Automatically cleaned up

Non-Blocking Subscriptions

# Run subscription in background thread
sub = Subscription(api_key="your_key", subscription_type="nation_colors")
sub.start(callback=handle_event, blocking=False)

# Do other work while subscription runs in background
do_other_work()

# Stop when done
sub.stop()

Context Manager

with Subscription(api_key="your_key", subscription_type="nation_colors") as sub:
    sub.start(callback=handle_event)
# Automatically cleaned up

Filtering Events

def handle_beige_exits(event):
    if event['new_color'] != 'beige' and event['old_color'] == 'beige':
        print(f"{event['nation_name']} exited beige!")

sub = Subscription(api_key="your_key", subscription_type="nation_colors")
sub.start(callback=handle_beige_exits)

Error Handling

def safe_handler(event):
    try:
        # Your processing logic
        process_event(event)
    except Exception as e:
        print(f"Error processing event: {e}")

sub.start(callback=safe_handler)

Subscription Types

Available subscription types:

  • "nation_colors" - Nation color bloc changes (tracked with full history)
  • "nations" - Nation creation, updates, and deletions
  • "trades" - Trade market activity (create, update, delete)
  • "wars" - War declarations and outcomes
  • "attacks" - Individual war attacks
  • "alliances" - Alliance changes
  • "cities" - City creation, updates, and improvements
  • "banks" - Alliance bank transactions (bankrecs)
  • "treaties" - Treaty creation, updates, and cancellation
  • "bounties" - Bounty creation, updates, and claims
  • "baseball_games" - Baseball game results
  • "treasure_trades" - Treasure trading activity
  • "embargoes" - Embargo creation and removal

How It Works

This package wraps Node.js scripts that connect to the Politics and War Pusher websocket API. When events occur, the Node.js process outputs JSON to stdout, which the Python wrapper parses and passes to your callback function.

Troubleshooting

Node.js not found:

RuntimeError: Node.js is not installed or not in PATH

Install Node.js from https://nodejs.org/

Missing dependencies:

cd pnw_subscriptions/scripts
npm install pusher-js node-fetch

API Key issues: Make sure your API key is valid and has the necessary permissions.

License

MIT

Contributing

Contributions welcome! Please open an issue or PR.

Credits

Uses the Politics and War API

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

pnw_python_subscriptions-1.1.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

pnw_python_subscriptions-1.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file pnw_python_subscriptions-1.1.0.tar.gz.

File metadata

File hashes

Hashes for pnw_python_subscriptions-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f876a66720b806f556d74da84d0a89da3ab76fc82ab57613f81df91ecb84689b
MD5 42737ca851939dc3ef704b2fa2239018
BLAKE2b-256 fae2fb19af15f43bc9436821e0fe8c33193ffa3fe8a41774b3946d915b6ac33a

See more details on using hashes here.

File details

Details for the file pnw_python_subscriptions-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pnw_python_subscriptions-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b67f04056ae0a3d081a85970146c0f4b0d44dbaf86979ea529deb247bff2c2be
MD5 31c2a2f09d14c48b4a3a4b6c4610a927
BLAKE2b-256 229b903fda67edbdf5459d65602b1e86913bafa47d836d19d132fdd2fff5c96a

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