Shell
Shell callbacks and structured event fields for scripts.
Requirements
POSIX sh · OpenSSL · awk · gzip · coreutils.
Installation
bpkg install PirateTok/live-sh
Alternatively, clone the source and load the library directly:
git clone https://github.com/PirateTok/live-sh.git
Save the quickstart alongside the live-sh directory as reader.sh, then run sh reader.sh.
Quickstart
Replace username_here with the username of an account that is currently live.
#!/bin/sh
PT_LIB_DIR="$(pwd)/live-sh/lib"
export PT_LIB_DIR
. "$PT_LIB_DIR/piratetok.sh"
on_chat() { printf '%s: %s\n' "$1" "$2"; }
on_error() { printf '%s\n' "$1" >&2; }
trap 'pt_disconnect' INT TERM
pt_connect "username_here"
Connection lifecycle
pt_connect runs a blocking loop and dispatches shell callbacks. pt_disconnect stops the connection and retries. Keep event handlers short because they run in the event-processing path.
Events and configuration
Events
on_event NAME METHOD fires for every decoded event, with the fields in PT_EV_FIELDS (path<TAB>value lines, e.g. user.nickname, ranks_list.0.score, gift_details.diamond_count). Read them with pt_field PATH or pt_get PATH... (→ $_g1..$_g9). Names match live-rs: Chat, Gift, Like, Member, Social, RoomUserSeq, Control, the sub-routed Follow / Share / Join / LiveEnded (both the raw and the convenience event fire), all Tier A/B types (Envelope, LinkMicBattle, SubNotify, …), and Unknown (method + raw payload hex). 64-bit identifiers retain their exact values.
Convenience handlers: on_chat USER CONTENT, on_gift USER NAME REPEAT DIAMONDS, on_like USER TOTAL, on_join USER, on_follow USER, on_share USER, on_viewers COUNT, on_top_viewer RANK SCORE NICK UNIQUE_ID USER_ID (the top-viewers box, sorted by rank), on_ended, on_unknown METHOD HEX, on_connected ROOM, on_reconnecting ATTEMPT MAX DELAY, on_disconnected, on_status MSG, on_error MSG.
API calls (no WebSocket)
| Call | Result |
|---|---|
pt_check_online USER |
prints LIVE:<room_id>:<anchor_id>, OFF, 404, APIERROR:<code>, BLOCKED or ERROR. pt_resolve_room USER sets PT_ONLINE, PT_ROOM_ID, PT_ANCHOR_ID, PT_ERROR |
pt_fetch_room_info ROOM [COOKIES] |
PT_INFO_TITLE/VIEWERS/LIKES/TOTAL/OWNER_ID, PT_INFO_FLV_ORIGIN/HD/SD/LD/AO; 18+ rooms without cookies → AGE_RESTRICTED |
pt_fetch_room_audience ROOM ANCHOR|"" COOKIES |
on_audience_viewer RANK SCORE USER_ID USERNAME NICK FOLLOWERS VERIFIED IS_FOLLOWER IS_FOLLOWING IS_SUB SEC_UID AVATAR per named viewer; PT_AUD_TOTAL, PT_AUD_ANON. Login-gated: without session cookies → SESSION_REQUIRED |
pt_fetch_profile USER |
PT_PROFILE_* (HD avatars, counts, bio link…). Cached in PT_CACHE_DIR for PT_PROFILE_TTL s, private/not-found negatively cached |
Errors are PT_ERROR="KIND: detail": USER_NOT_FOUND, HOST_NOT_ONLINE, API_ERROR, TIKTOK_BLOCKED (HTTP 403/429, empty or non-JSON body), AGE_RESTRICTED, SESSION_REQUIRED, INVALID_RESPONSE, HTTP, PROFILE_*.
Helpers
pt_gift_is_combo, pt_gift_is_streak_over, pt_gift_diamond_total, pt_gift_streak (GiftStreakTracker → PT_STREAK_*), pt_like_accumulate (LikeAccumulator → PT_LIKE_TOTAL monotonic, PT_LIKE_ACCUMULATED, PT_LIKE_BACKWARDS), pt_gifter_level, pt_member_level, pt_is_moderator, pt_is_top_gifter.
Configuration (env or variables, before calling pt_*)
| Variable | Default | |
|---|---|---|
PT_CDN / PT_WS_PORT |
webcast-ws.tiktok.com / 443 |
EU webcast-ws.eu.tiktok.com, US webcast-ws.us.tiktok.com |
PT_HEARTBEAT_SEC |
10 | also sent as heartbeat_duration |
PT_STALE_SEC |
60 | no data for this long → reconnect |
PT_MAX_RETRIES |
5 | consecutive failures; a 30 s healthy session resets it; 0 = no reconnect |
PT_UA |
random pool | fixed User-Agent |
PT_COOKIES |
session cookies appended after ttwid on the WebSocket | |
PT_PROXY |
http://[user:pass@]host:port (HTTP CONNECT) for every HTTP call and the WebSocket |
|
PT_LANGUAGE / PT_REGION |
from LANG / en, US |
|
PT_COMPRESS |
1 | gzip WebSocket payloads |
PT_CAFILE |
system trust store | extra CA bundle; TLS peers are always verified (hostname / IP checked) |
PT_WEB_HOST, PT_WEBCAST_HOST (+ _PORT) |
TikTok | override endpoints (local fakes, mirrors) |
Reconnects: a cookie-less ttwid response is retried 8× (750 ms apart) before it counts as a failed attempt. The ttwid + UA pair is reused across reconnects and rotated on DEVICE_BLOCKED (retry in 2 s) or after a connection that died within 30 s. Backoff is 2 → 4 → 8 → 16 → 30 s.
Limits
- No SOCKS proxies.
openssl s_clientonly tunnels through HTTP CONNECT, sosocks5://is rejected with an explicit error. - Throughput: every frame goes through
od/awk/gzipprocesses. It's fine for normal rooms, but it's a shell. Use live-rs/live-c for heavy rooms. PT_TTWID_DELAYuses fractionalsleep(GNU/busybox). On shells without it, the delay falls back to 1 s.
Protocol reference
The basic LIVE connection uses an anonymous ttwid cookie. Session cookies may be needed for restricted room metadata or the full audience roster. These are separate calls from the event stream.
- WebSocket contract
- Room metadata
- Audience endpoint
- Authentication and connection lifecycle
- Troubleshooting
Source and license
Public source and examples · 0BSD license