{
 "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/sample.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": [
    "def parse_timings(timings_str):\n",
    "    timestamp_pattern = r'(\\d+):(\\d+)(?::(\\d+))?'\n",
    "    matches = re.findall(timestamp_pattern, timings_str)\n",
    "    \n",
    "    # Convert timestamps to seconds\n",
    "    timestamps = []\n",
    "    for match in matches:\n",
    "        hours_or_minutes = int(match[0])\n",
    "        seconds_or_minutes = int(match[1])\n",
    "        seconds = match[2]\n",
    "        \n",
    "        if seconds:  # Format is H:MM:SS\n",
    "            total_seconds = hours_or_minutes * 3600 + seconds_or_minutes * 60 + int(seconds)\n",
    "        else:  # Format is MM:SS or M:SS\n",
    "            total_seconds = hours_or_minutes * 60 + seconds_or_minutes\n",
    "        \n",
    "        timestamps.append(float(total_seconds))\n",
    "    \n",
    "    # Check if \"throughout\" is present (case-insensitive)\n",
    "    throughout = 'throughout' in timings_str.lower()\n",
    "    \n",
    "    return timestamps, throughout\n",
    "\n",
    "def is_valid_sample(metadata):\n",
    "    timings = metadata['timings']\n",
    "    timestamps, is_throughout = parse_timings(timings)\n",
    "    return len(timestamps) >= 4 or is_throughout\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "metadata_samples = []\n",
    "\n",
    "for sample in data:\n",
    "    output = sample[0]\n",
    "    source = sample[1]\n",
    "    if output['youtube'] is None:\n",
    "        continue\n",
    "    new_metadata =  dict(\n",
    "        output_id=output['youtube'],\n",
    "        source_ids=[source['youtube']],\n",
    "        is_valid_sample=is_valid_sample(output),\n",
    "        votes=output['votes']\n",
    "    )\n",
    "    metadata_samples.append(new_metadata)\n",
    "\n",
    "print(len(metadata_samples))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "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_valid_sample=False,\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/sample_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/sample_metadata.jsonl\")"
   ]
  }
 ],
 "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
}
