{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import internetarchive\n",
    "import time\n",
    "\n",
    "def get_flac_files_with_metadata(query, max_items=None, sleep_time=1):\n",
    "    \"\"\"\n",
    "    Search the Internet Archive and build a dataset of FLAC files with their metadata.\n",
    "    \n",
    "    Parameters:\n",
    "        query (str): The search query\n",
    "        max_items (int, optional): Maximum number of items to retrieve\n",
    "        sleep_time (float): Seconds to sleep between page requests\n",
    "    \n",
    "    Returns:\n",
    "        list: List of dictionaries containing FLAC file URLs and their metadata\n",
    "    \"\"\"\n",
    "    all_flac_files = []\n",
    "    search = internetarchive.search_items(query)\n",
    "    \n",
    "    # Get total number of results\n",
    "    total_results = search.num_found\n",
    "    print(f\"Found {total_results} total results\")\n",
    "    \n",
    "    # Process items as we find them\n",
    "    for i, result in enumerate(search):\n",
    "        if max_items and i >= max_items:\n",
    "            break\n",
    "            \n",
    "        identifier = result['identifier']\n",
    "        print(f\"Processing item {i+1}: {identifier}\")\n",
    "        \n",
    "        # Get the item and extract FLAC files with metadata\n",
    "        item = internetarchive.get_item(identifier)\n",
    "        item_metadata = item.metadata  # Get item-level metadata\n",
    "        flac_count = 0\n",
    "        \n",
    "        for file_dict in item.files:\n",
    "            if file_dict['name'].lower().endswith('.flac'):\n",
    "                download_url = f\"https://archive.org/download/{identifier}/{file_dict['name']}\"\n",
    "                \n",
    "                # Create a dictionary with URL and all metadata\n",
    "                flac_data = {\n",
    "                    'download_url': download_url,\n",
    "                    'identifier': identifier,\n",
    "                    'filename': file_dict['name'],\n",
    "                    'file_metadata': file_dict,  # All file-specific metadata\n",
    "                    'item_metadata': item_metadata  # Item-level metadata\n",
    "                }\n",
    "                \n",
    "                all_flac_files.append(flac_data)\n",
    "                flac_count += 1\n",
    "        \n",
    "        if flac_count > 0:\n",
    "            print(f\"  Found {flac_count} FLAC files\")\n",
    "        else:\n",
    "            print(\"  No FLAC files found\")\n",
    "            \n",
    "        # Pause periodically to be nice to the servers\n",
    "        if (i + 1) % 10 == 0:\n",
    "            print(f\"Processed {i + 1} items, found {len(all_flac_files)} FLAC files total\")\n",
    "            time.sleep(sleep_time)\n",
    "    \n",
    "    print(f\"\\nCompleted search: Found {len(all_flac_files)} FLAC files across {i+1} items\")\n",
    "    \n",
    "    return all_flac_files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Usage example\n",
    "query = 'format:(FLAC) AND \"16 bit\"'\n",
    "all_flac_files = get_flac_files_with_metadata(query, max_items=1)  # Limit to 50 items for testing\n",
    "\n",
    "for file in all_flac_files:\n",
    "    print(file)\n",
    "    break\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "suno_env",
   "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
