Skip to content

Instantly share code, notes, and snippets.

@Tatsh
Created January 29, 2013 09:51
Show Gist options
  • Save Tatsh/4663112 to your computer and use it in GitHub Desktop.
Save Tatsh/4663112 to your computer and use it in GitHub Desktop.
Import iTunes ratings from an iPhone database into Clementine's database. Get the database file from your iPhone (including non-jailbroken) by mounting your iPhone via ifuse, and grabbing a copy of `$MOUNTPOINT/iTunes_Control/iTunes/MediaLibrary.sqlitedb` (note how I said *copy*; I will not be held responsible if you have to restore your iDevice…
#!/usr/bin/env python
import sqlite3
import os
import subprocess as sp
import signal
from time import sleep
home = os.environ['HOME']
conn = sqlite3.connect(home + '/dev/MediaLibrary.sqlitedb',
detect_types=sqlite3.PARSE_COLNAMES)
c = conn.cursor()
query = '''select a.item_artist,
l.album,
e.title,
s.user_rating AS "rating [integer]"
from item_stats s
left join item i on i.item_pid = s.item_pid
left join item_artist a on a.item_artist_pid = i.item_artist_pid
left join item_extra e on e.item_pid = i.item_pid
left join album l on l.album_pid = i.album_pid
where i.item_pid is not null'''
c.execute(query)
data = []
for row in c:
artist = row[0]
album = row[1]
title = row[2]
rating = row[3]
data.append({
'artist': artist,
'album': album,
'title': title,
'rating': rating / 100
})
conn.close()
# Kill any clementine instance
try:
pid = int(sp.check_output('pidof clementine', shell=True).strip())
if pid:
os.kill(pid, signal.SIGINT)
sleep(2) # let the clementine sub-processes quit
except sp.CalledProcessError:
pass
clementine_db = home + '/.config/Clementine/clementine.db'
conn = sqlite3.connect(clementine_db)
c = conn.cursor()
for item in data:
# title, album, artist, rating (float out of 1)
t = info = (item['artist'], item['title'], item['album'])
query = '''select title,album,artist
from songs where artist=?
and title=? and album=?'''
c.execute(query, t)
row = c.fetchone()
if row is None:
print('No song found for %s - %s - %s' % (info[0], info[1], info[2]))
continue
query = 'update songs set rating=? where artist=? and title=? and album=?'
t = (item['rating'], item['artist'], item['title'], item['album'])
c.execute(query, t)
conn.commit()
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment