#!/bin/bash
# W90DM Web Scraper Script

IP="41.119.36.159"
USER="admin"
PASS="0penV70penV7"
COOKIE_FILE="/tmp/w90dm_cookie.txt"

echo "Scraping W90DM at $IP..."

# Try to login
curl -k -s -c "$COOKIE_FILE" -b "$COOKIE_FILE" \
  -X POST \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "username=$USER&password=$PASS" \
  "https://$IP/login.htm" > /dev/null 2>&1

# Try API endpoints
for endpoint in "/cgi-bin/api/get_handset_list" "/cgi-bin/get_handsets.cgi"; do
    echo "Trying: $endpoint"
    RESULT=$(curl -k -s -c "$COOKIE_FILE" -b "$COOKIE_FILE" \
      "https://$IP$endpoint" 2>&1)
    
    if echo "$RESULT" | grep -q '"ret":"ok"'; then
        echo "SUCCESS! Raw response:"
        echo "$RESULT" | head -20
        
        # Parse and update database
        # This is a simplified version - would need jq for proper JSON parsing
        break
    fi
done

# Cleanup
rm -f "$COOKIE_FILE"
