dumpResponseToFile function

void dumpResponseToFile({
  1. required String html,
  2. required bool debug,
})

Saves HTML content to dump file for debugging purposes.

This function is used to save HTML content to timestamped files in a dump directory. It's useful for debugging scraping issues by preserving the HTML content that was processed.

The function:

  1. Creates a dump directory if it doesn't exist
  2. Saves the current dump.html content to a timestamped file
  3. Updates dump.html with the new HTML content

Parameters:

  • html: The HTML content to save
  • debug: Whether debug mode is enabled (only saves if true)

Example:

dumpResponse('<html>...</html>', debug);

Implementation

void dumpResponseToFile({required String html, required bool debug}) {
  if (!debug) return;

  try {
    // Generate timestamp for unique filename
    final timestamp = DateTime.now().millisecondsSinceEpoch;

    final currentDir = Directory.current;

    // Ensure dump folder exists
    final dumpDir = Directory(path.join(currentDir.path, 'dump'));
    if (!dumpDir.existsSync()) {
      dumpDir.createSync(recursive: true);
      print('Created dump folder at: ${dumpDir.path}');
    }

    // The main dump.html file inside /dump
    final dumpLocation = File(path.join(dumpDir.path, 'dump.html'));

    String oldFileContent = '';
    if (dumpLocation.existsSync()) {
      // Read current dump.html
      oldFileContent = dumpLocation.readAsStringSync();

      // Save old dump content to a timestamped file inside /dump
      final oldDump = File(path.join(dumpDir.path, '$timestamp.html'));
      oldDump.writeAsStringSync(oldFileContent);
    }

    // Update or create dump.html with new content
    dumpLocation.writeAsStringSync(html, mode: FileMode.write);

    print('Dump saved at: ${dumpLocation.path}');
  } catch (e) {
    print('Unable to save file, error: $e');
  }
}