# The Pirate Bay API — Free Torrent Search Without Setup

## Endpoint
```
GET https://apibay.org/q.php?q={QUERY}&cat=0
```
No API key needed. No Cloudflare. Returns JSON directly.

## Response Format
```json
[
  {
    "id": "12345",
    "name": "The Matrix (1999) 1080p BrRip x264 - YIFY",
    "info_hash": "D7A46713EAEE18C746B3...",
    "size": "1986420736",
    "seeders": "710",
    "leechers": "89",
    "added": "1700000000",
    "status": "trusted",
    "category": "202"
  }
]
```

## Implementation (Flask) — return ALL results
```python
def search_tpb(title):
    import json
    q = urllib.parse.quote(title)
    url = f'https://apibay.org/q.php?q={q}&cat=0'
    req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
    r = urllib.request.urlopen(req, timeout=15)
    data = json.loads(r.read())
    results = []
    for d in data:  # No limit — return everything TPB has
        ih = d.get('info_hash', '').strip().lower()
        if not ih or ih == '0000000000000000000000000000000000000000':
            continue
        results.append({
            'hash': ih,
            'title': d.get('name', 'Unknown'),
            'name': 'TPB',
            'size': int(d.get('size', 0)),
            'seeders': int(d.get('seeders', 0)),
        })
    return results
```

## Fallback Chain Priority
1. Jackett (if user configured indexers with public trackers)
2. **TPB API** (always works, zero setup, 100+ results)
3. Embed sources (vidsrc.to, 2embed, etc.)

## Notes
- Returns up to 100 results per query (API max)
- Skip entries where `info_hash` is all zeros (dead/unregistered torrents)
- `cat=0` means all categories. Use `cat=201` for movies, `cat=205` for TV
- No rate limiting observed
- The TPB web frontend loads results from this API via JS (the HTML page is a skeleton)
- Many results will have 0 seeders — TorrServer cannot stream those. Display seed count in the UI so users can pick well-seeded torrents.
- Format seed count in source button label (e.g. "TPB 710s" for 710 seeders)
