#!/usr/bin/env python3
"""
Quick scraping utility using Scrapling.
Usage:
  python3 scripts/scrape.py "https://url"                    # Get text
  python3 scripts/scrape.py "https://url" --links            # Get all links
  python3 scripts/scrape.py "https://url" --html             # Get raw HTML
  python3 scripts/scrape.py "https://url" --css "h1,h2,p"   # CSS selector
  python3 scripts/scrape.py "https://url" --stealth          # Use StealthFetcher (Cloudflare bypass)
"""
import sys
import argparse
import logging

logging.basicConfig(level=logging.WARNING)

def main():
    parser = argparse.ArgumentParser(description='Scrape a URL with Scrapling')
    parser.add_argument('url', help='URL to scrape')
    parser.add_argument('--links', action='store_true', help='Extract all links')
    parser.add_argument('--html', action='store_true', help='Output raw HTML')
    parser.add_argument('--css', type=str, help='CSS selector to extract')
    parser.add_argument('--stealth', action='store_true', help='Use StealthFetcher for Cloudflare bypass')
    parser.add_argument('--json', action='store_true', help='Try to parse JSON response')
    args = parser.parse_args()

    if args.stealth:
        from scrapling import StealthFetcher
        f = StealthFetcher()
    else:
        from scrapling import Fetcher
        f = Fetcher()

    page = f.get(args.url)

    if args.html:
        print(page.body.decode('utf-8', errors='replace'))
    elif args.json:
        print(page.json())
    elif args.links:
        links = page.css('a')
        for l in links:
            href = l.attrib.get('href', '')
            text = l.text.strip()[:60] if l.text else ''
            if href:
                print(f'{href} — {text}')
    elif args.css:
        elements = page.css(args.css)
        for el in elements:
            text = el.text.strip() if el.text else ''
            if text:
                print(text)
    else:
        print(page.get_all_text())

if __name__ == '__main__':
    main()
