Skip to content

Instantly share code, notes, and snippets.

@Meriipu
Last active June 12, 2017 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Meriipu/944e648b1e16e9804ea09d6f6725e515 to your computer and use it in GitHub Desktop.
Save Meriipu/944e648b1e16e9804ea09d6f6725e515 to your computer and use it in GitHub Desktop.
#Import the script from quodlibet python console and run the fix function iirc
#might need some minor modifications as I have not tested this version, but it
#is essentially what I did to import stuff from banshees SQL DB to quodlibet
#I suggest selecting one tack in quodlibet and running python console and trying to
#set field values (the selected tracks are found in the col.songs attribute in the QL
#python console) to see whether it works as expected before modifying a bunch of tracks
from ast import literal_eval
import urllib,urlparse
json_track_listing = '/home/fsjal/Documents/bansheedb/dbexport.txt'
#export from banshee DB
#include other fields in export for other stuff
'''[
{ "Uri" : """file:///etc/etc/Music/a.mp3""", "PlayCount" : """8""" },
{ "Uri" : """file:///etc/etc/Music/b.mp3""", "PlayCount" : """5""" },
...
]'''
def uri2path(uri):
return urllib.url2pathname(urlparse.urlparse(uri).path)
def _load_db():
"""load database from exported textfile as a python dict
Not sure why I did not import as json, maybe the format was not really json.
is a mapping from file paths to the fields that are to be imported
"""
with open(json_track_listing) as f:
s = f.read()
L = literal_eval(s)
d = {}
for track in L:
path = uri2path(track['Uri'])
d[path] = (track['PlayCount'],) #alternatively several fields
return d
def fix(col):
d = _load_db()
S = set()
#col.songs is the collection of songs that were selected
#in quod libet before running the python console plugin
for song in sorted(col.songs):
filename = song['~filename']
if filename not in d:
#set the rating field of songs not in the banshee database to 0.05 so
#they can be found later
song['~#rating'] = 0.05
else:
#banshee_rating = int(d[filename][1]) #field
#I believe ratings are 0.0, 0.2, ..., 1.0 rather than 0-5
#song['~#rating'] = {0:0.0, 1:0.2, 2:0.4, 3:0.6, 4:0.8, 5:1.0}[banshee_rating]
#playcount:
banshee_playcount = int(d[filename][0]) #field
song['~#playcount'] = banshee_playcount
#repeat as appropriately for other fields
#...
#add to set to detect songs that exist in banshee but not in QL
S.add(filename)
print('---')
#these songs exist in banshee but not QL
#this stuff can be commented out since it is just for debugging whether
#the shitty script works or not
for f in sorted(d):
if f not in S:
print(f)
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment