{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from suno_utils.utils.text import read_jsonl, write_jsonl\n",
    "import re\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read the data\n",
    "data = read_jsonl(\"/home/sara/who_sampled_victor/who_sampled/data/cover.jsonl\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dictionary of unique IDs to metadata (using youtube ID)\n",
    "id_to_metadata = {}\n",
    "\n",
    "for pair in data:\n",
    "    remix, source = pair[0], pair[1]\n",
    "    \n",
    "    for song in [remix, source]:\n",
    "        youtube_id = song['youtube']\n",
    "        if youtube_id and youtube_id not in id_to_metadata:\n",
    "            id_to_metadata[youtube_id] = {\n",
    "                'artist': song['artist'],\n",
    "                'song': song['song'],\n",
    "                'duration': song['duration'],\n",
    "                'release': song['release'],\n",
    "                'youtube': song['youtube'],\n",
    "                'date': song['date'],\n",
    "                'producer': song['producer'],\n",
    "                'who_sampled_url': song['who_sampled_url']\n",
    "            }\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dictionary of source songs to lists of their remixes\n",
    "source_to_remixes = {}\n",
    "\n",
    "for pair in data:\n",
    "    remix, source = pair[0], pair[1]\n",
    "    source_id = source['youtube']\n",
    "    remix_id = remix['youtube']\n",
    "    votes = remix['votes']\n",
    "    \n",
    "    if source_id:\n",
    "        if source_id not in source_to_remixes:\n",
    "            source_to_remixes[source_id] = []\n",
    "        \n",
    "        source_to_remixes[source_id].append({'id': remix_id, 'votes': votes})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dictionary of remixes to source songs\n",
    "remix_to_sources = {}\n",
    "\n",
    "for pair in data:\n",
    "    remix, source = pair[0], pair[1]\n",
    "    source_id = source['youtube']\n",
    "    remix_id = remix['youtube']\n",
    "    votes = remix['votes']\n",
    "    \n",
    "    if remix_id:\n",
    "        if remix_id not in remix_to_sources:\n",
    "            remix_to_sources[remix_id] = []\n",
    "        \n",
    "        remix_to_sources[remix_id].append({'id': source_id, 'votes': votes})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Check results\n",
    "print(f\"Total unique songs: {len(id_to_metadata)}\")\n",
    "print(f\"Total source songs: {len(source_to_remixes)}\")\n",
    "print(f\"Total remixes: {len(remix_to_sources)}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "metadata_samples = []\n",
    "\n",
    "for remix_id, source_ids in remix_to_sources.items():\n",
    "    if remix_id is None or len(source_ids) < 2:\n",
    "        continue\n",
    "\n",
    "    new_metadata = dict(\n",
    "        output_id=remix_id,\n",
    "        source_ids=source_ids,\n",
    "        is_mashup=True,\n",
    "        votes=None\n",
    "    )\n",
    "    metadata_samples.append(new_metadata)\n",
    "\n",
    "print(len(metadata_samples))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "write_jsonl(metadata_samples, \"/home/sara/who_sampled_victor/who_sampled/data/cover_processed.jsonl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sampled_metadata = []\n",
    "for id, metadata in id_to_metadata.items():\n",
    "    if id is not None:\n",
    "        sampled_metadata.append(metadata)\n",
    "\n",
    "print(len(sampled_metadata))\n",
    "write_jsonl(sampled_metadata, \"/home/sara/who_sampled_victor/who_sampled/data/cover_metadata.jsonl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "suno_clean",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
