Skip to content

Instantly share code, notes, and snippets.

@ehrenfeu
Created July 1, 2019 12:02
Show Gist options
  • Save ehrenfeu/e9b8b706eb9e2fe6d87729ca774b3617 to your computer and use it in GitHub Desktop.
Save ehrenfeu/e9b8b706eb9e2fe6d87729ca774b3617 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sqlite3\n",
"import pathlib\n",
"from urllib.parse import unquote"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"BANSHEE_DB_FILE = os.path.join(pathlib.Path.home(), \".config/banshee-1/banshee.db\")\n",
"print (f\"Using banshee database at: {BANSHEE_DB_FILE}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"bdb = sqlite3.connect(BANSHEE_DB_FILE)\n",
"bdb.isolation_level = None\n",
"bdb.text_factory = str\n",
"bdb.row_factory = sqlite3.Row"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"query = \"\"\"\n",
" SELECT\n",
" PlaylistID,\n",
" Name,\n",
" COUNT(1) AS nb_items\n",
" FROM\n",
" CorePlaylists\n",
" INNER JOIN CorePlaylistEntries USING (PlaylistID)\n",
" WHERE\n",
" IsTemporary = 0\n",
" GROUP BY\n",
" PlaylistID,\n",
" Name\n",
" HAVING\n",
" COUNT(1) > 0\n",
" ORDER BY\n",
" nb_items DESC;\n",
"\"\"\"\n",
"\n",
"playlists = bdb.cursor()\n",
"playlists.execute(query)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"query = \"\"\"\n",
" SELECT uri\n",
" FROM\n",
" CorePlaylistEntries\n",
" INNER JOIN CoreTracks USING (TrackID)\n",
" WHERE\n",
" PlaylistID = :playlist_id\n",
" ORDER BY\n",
" ViewOrder ASC;\n",
"\"\"\"\n",
"\n",
"entries = bdb.cursor()\n",
"\n",
"for playlist in playlists:\n",
" name = playlist['Name']\n",
" file_name = name.replace(' ', '_') + \".m3u\"\n",
" file_name = os.path.join(pathlib.Path.home(), file_name)\n",
" print (f\"Processing playlist '{name}'\")\n",
" entries.execute(query, {'playlist_id': playlist['PlaylistID']})\n",
" counter = 0\n",
" with open(file_name, 'w') as fout:\n",
" for pl_entry in entries:\n",
" uri = pl_entry[\"uri\"].replace('file://', '', 1)\n",
" uri = unquote(uri)\n",
" if not os.path.exists(uri):\n",
" print (f\"missing {uri}\")\n",
" fout.write(uri + \"\\n\")\n",
" counter += 1\n",
"\n",
" print (f\"Wrote playlist into [{file_name}] with {counter} items\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment