Exports all statistics data to a JSON file

Creates a complete export of all listening statistics and saves it as a JSON file in the user's downloads folder. This function enables users to backup their listening data, transfer it between devices, or use it for external analysis.

The export process includes:

  • Retrieving the complete statistics dataset
  • Converting complex data structures for proper serialization
  • Generating a timestamped filename for uniqueness
  • Saving the formatted data to the user's downloads directory

The exported file contains all metrics, patterns, and listening history in a well-structured JSON format.

// Export statistics and show result to user
const exported = await exportStatistics();
if (exported) {
showSuccess("Statistics exported to Downloads folder");
} else {
showError("Failed to export statistics");
}
export const exportStatistics = async (): Promise<boolean> => {
try {
const statistics = await getStatistics();
const userDownloadsFolder = app.getPath("downloads");
const timestamp = new Date().toISOString().replace(/:/g, "-").split(".")[0];
const exportFilePath = join(
userDownloadsFolder,
`spotify-statistics-${timestamp}.json`,
);

// Prepare statistics for export
const exportData = prepareStatisticsForSave(statistics);

// Write to file
writeJsonSync(exportFilePath, exportData, { spaces: 2 });

return true;
} catch (error) {
console.error("Error exporting statistics:", error);
return false;
}
};