# AskGamblers Casino Bonus Scraper

This script scrapes casino bonus data from AskGamblers.com and generates a CSV file with all the extracted information.

## Features

- ✅ Scrapes multiple pages from AskGamblers bonus listings
- ✅ Extracts casino name, bonus text, and bonus type
- ✅ Downloads casino logo images
- ✅ Follows redirects to get final casino URLs
- ✅ Generates CSV file compatible with the Laravel import system
- ✅ Respectful scraping with delays between requests

## What Gets Extracted

From each casino bonus card, the script extracts:

1. **Casino Name** - Extracted from title (text before "Casino")
2. **Bonus Text** - The welcome bonus description (e.g., "100% up to €300 + 100 Free Spins")
3. **Bonus Type** - Type of bonus (e.g., "Welcome Bonus")
4. **Image URL** - Original image URL from AskGamblers
5. **Local Image Path** - Path to downloaded image (saved in `scraped_data/images/`)
6. **Casino URL** - Final casino URL after following redirects
7. **Visit URL** - Original AskGamblers visit/bonus URL

## Installation

The script uses Guzzle HTTP client which is already included in your Laravel project. No additional dependencies needed.

## Usage

### Basic Usage (scrape 5 pages)

```bash
php scrape_askgamblers.php
```

### Scrape Specific Number of Pages

```bash
php scrape_askgamblers.php 10
```

This will scrape 10 pages (pages 1-10).

### Scrape All Pages

Edit the script and set `$maxPages = null` in the constructor, or modify the scrape method.

## Output

The script creates:

1. **CSV File**: `scraped_data/askgamblers_casinos_YYYY-MM-DD_HHMMSS.csv`
   - Contains all extracted casino data
   - Ready to import into your Laravel system

2. **Images Directory**: `scraped_data/images/`
   - All casino logos are downloaded here
   - Filenames: `{casino_name}_{timestamp}.{extension}`

## CSV Format

The generated CSV includes these columns:

```csv
casino_name,bonus_text,bonus_type,image_url,local_image_path,casino_url,visit_url
```

Example row:
```csv
AllStarz Casino,"100% up to €300 + 100 Free Spins","Welcome Bonus","https://www.askgamblers.com/uploads/...","images/allstarz_casino_1234567890.png","https://allstarz.com","https://www.askgamblers.com/visit/bonus/..."
```

## How It Works

1. **Page Fetching**: Uses Guzzle HTTP client to fetch pages
2. **HTML Parsing**: Uses PHP's DOMDocument to parse HTML and extract data
3. **Data Extraction**: 
   - Finds all `<div class="card-bonus">` elements
   - Extracts casino name from title (before "Casino")
   - Extracts bonus text (after "Bonus:")
   - Gets image URL from `data-srcset` or `srcset` attributes
   - Gets visit URL from "Get bonus" button
4. **Image Download**: Downloads images and saves them locally
5. **Redirect Following**: Follows the visit URL redirect to get final casino URL
6. **CSV Generation**: Creates CSV file with all extracted data

## Configuration

You can modify these settings in the `AskGamblersScraper` class:

- `$maxPages` - Maximum number of pages to scrape (default: 10)
- `$delayBetweenRequests` - Seconds to wait between page requests (default: 2)
- `$startUrl` - Starting URL (default: welcome bonuses page)

## Error Handling

The script includes error handling for:
- Network errors
- Missing HTML elements
- Image download failures
- Redirect following failures

Errors are logged to console but don't stop the scraping process.

## Notes

- The script includes a 2-second delay between page requests to be respectful
- Images are saved with sanitized filenames
- Casino URLs are extracted from redirects (base domain only)
- The script handles pagination automatically (page 1, 2, 3, etc.)

## Integration with Laravel Import

The generated CSV can be imported using the existing CSV import functionality in `app/Filament/Pages/GlobalSettings.php`. However, you may need to map the columns to match your Post model fields:

- `casino_name` → `title` (you may need to add "Casino" suffix)
- `bonus_text` → `welcome_bonus` (JSON field)
- `image_url` → `thumbnail`
- `casino_url` → `casino_url`

## Troubleshooting

**No casinos found:**
- Check if AskGamblers has changed their HTML structure
- Verify the page URL is still accessible
- Check network connectivity

**Images not downloading:**
- Check write permissions on `scraped_data/images/` directory
- Verify image URLs are accessible
- Check disk space

**Redirects not working:**
- Some casinos may have complex redirect chains
- The script extracts the base domain from the final redirect
- Check if the visit URL is accessible
