# Jackett Setup for Movie Streaming App

## Installation
```bash
cd /tmp
wget -q "https://github.com/Jackett/Jackett/releases/latest/download/Jackett.Binaries.LinuxAMDx64.tar.gz"
sudo tar xzf Jackett.Binaries.LinuxAMDx64.tar.gz -C /opt/
```

## Start
```bash
/opt/Jackett/jackett --NoRestart --NoUpdates &
# Waits ~10s to initialize
sleep 10 && ss -tlnp | grep 9117
```

## Get API Key
```bash
cat ~/.config/Jackett/ServerConfig.json | python3 -c "import json,sys; print(json.load(sys.stdin).get('APIKey',''))"
```

## Open Firewall
```bash
sudo ufw allow 9117/tcp comment 'Jackett'
```

## Add Indexers
1. Open http://192.168.1.50:9117 in a browser
2. Click "Add Indexer"
3. Search for and add public trackers:
   - 1337x
   - The Pirate Bay
   - TorrentGalaxy
   - LimeTorrents
   - EZTV (TV shows)
4. Configure each (most public trackers work with defaults)

## Test Search
```bash
API_KEY="your-api-key-here"
curl -s "http://127.0.0.1:9117/api/v2.0/indexers/all/results/torznab/api?apikey=${API_KEY}&t=movie&q=The+Matrix" | head -c 500
```

## Torznab Response Format
```xml
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:torznab="http://torznab.com/schemas/2015/feed">
  <channel>
    <item>
      <title>Movie.Title.2024.1080p.WEB-DL</title>
      <guid>https://.../btih:abcdef1234567890abcdef1234567890abcdef12&...</guid>
      <torznab:attr name="infohash" value="abcdef1234567890abcdef1234567890abcdef12"/>
      <torznab:attr name="size" value="2147483648"/>
      <torznab:attr name="seeders" value="42"/>
      <torznab:attr name="indexer" value="1337x"/>
    </item>
  </channel>
</rss>
```

## Integration in Flask
```python
import xml.etree.ElementTree as ET
import re

def search_jackett(title, media_type='movie', season='', episode=''):
    q = urllib.parse.quote(title)
    if media_type == 'tv' and season and episode:
        url = f'{JACKETT_HOST}:{JACKETT_PORT}/api/v2.0/indexers/all/results/torznab/api?apikey={JACKETT_API}&t=tvsearch&q={q}&season={season}&ep={episode}'
    else:
        url = f'{JACKETT_HOST}:{JACKETT_PORT}/api/v2.0/indexers/all/results/torznab/api?apikey={JACKETT_API}&t=movie&q={q}'
    
    r = urllib.request.urlopen(url, timeout=15)
    xml = r.read().decode()
    results = []
    NS = {'t': 'http://torznab.com/schemas/2015/feed'}
    root = ET.fromstring(xml)
    for item in root.findall('.//item'):
        ih = ''
        for attr in item.findall('.//t:attr', NS):
            if attr.get('name') == 'infohash':
                ih = attr.get('value', '')
        if not ih:
            guid = item.findtext('guid', '')
            m = re.search(r'btih:([a-fA-F0-9]{40})', guid)
            if m: ih = m.group(1).lower()
        if not ih: continue
        # ... build result dict
    return results[:20]
```
