2025-11-07-codemcp-feat-add-json-merge-script-for-playlists-and-histo.md 2.0 KB

User Prompt

given the format of the json export of playlists and history, write a small script (in jq and/or bash) that will accept an arbitrary number of these exported files, and combine them into one (returning the result to standard out is fine). pay close attention to reconciling differences (between playlist versions and between history versions), and ensure no loss of data. try to preserve playlist order (even if a later version had added more videos, for example), and use filename date (later versions break ties, if necessary). if videos have been removed in subsequent versions, keep them in the combined version.

Solution

Created merge-exports.sh - a bash script using jq to merge multiple playlist export JSON files.

Key Features

  1. Playlist Merging:

    • Preserves video order based on earliest appearance (by file date and position)
    • Keeps all videos even if removed in later versions
    • Uses latest video metadata (title, status, tags) from newest file
  2. History Merging:

    • Combines all history events per video
    • Deduplicates events by timestamp
    • Keeps latest title and URL per video
    • Merges tags from all versions
  3. Open Tabs:

    • Deduplicates by URL
    • Keeps latest title per unique URL
  4. Date Handling:

    • Extracts date prefix from filename (YYYY-MM-DD format)
    • Uses date for sorting and tie-breaking
    • Defaults to "0000-00-00" if no date found

Usage

./merge-exports.sh export1.json export2.json export3.json > merged.json

Algorithm Details

For playlists:

  1. Groups videos by playlist name across all files
  2. Creates union of all videos (by URL) from all versions
  3. For each video, finds earliest appearance (date + index)
  4. Sorts videos by earliest appearance to preserve order
  5. Uses latest video data for metadata

For history:

  • Groups by video URL
  • Merges all event arrays
  • Deduplicates by timestamp
  • Keeps chronological order

Bug Fix

Fixed "Cannot index array with string" error by properly accessing the files array with $files[0] instead of just $files.