This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Import ratings from iTunes Library - call for suggestions

Tags: None
(comma "," separated)
User avatar
egork
Registered Member
Posts
6
Karma
0
I have just written a bash script to import all song ratings from iTunes Library XML file to amarok via DCOP.
Now the script is not elegant at all, but it worked for me. As iPod has 70% of the MP3 players market, there must be other people interested in importing their song ratings once they migrate to Amarok.

Does anybody want to help me develop this script into a decent Amarok import script?

What I do not like at all now, is the way I have translated special characters from XML file. There must be a better way, also dealing with other language encodings.

#!/bin/bash
TUNES_DIR="/mnt/smb_drive/iTunes_Library" # Path to your music files, the name may not contain "%"
TUNES_LIB_DIR="/mnt/smb_drive/iTunes" # Path to the XML file, you may copy it to a home directory instead
#csplit -s -f iTunesLib.  $TUNES_LIB_DIR/iTunes\ Music\ Library.xml /Playlist/ \
#&& rm iTunesLib.01

# "Rating" string always preceeds "Location" string if present at all,
# so concatenate them into one string
egrep "Rating|Location" iTunesLib.00 \
|sed -n -e '
/Rating/{
N
s/\n//
p
}
' |

# Get the values out of xml tags.
sed -e 's%.*\([0-9]\{1,2\}\)[0-9].*\(.*\).*%"\2" \1%g' |

# Replace the path to the library as it is now mounted sowhere else
sed -e 's%file://.*iTunes_Library/%'$TUNES_DIR'/%g' |

# Replace encoded characters with ASCII
perl -e 'while(<>){s/(%..)/chr(hex(substr($1,1)))/eg; print}'|
sed -e 's/\&\;/\&/g' |

# tee iTunesOutput.txt |

xargs -L 1 dcop amarok player setRatingByPath 

rm iTunesLib.00
echo "Rating done"
exit 0
Aaron Cooper
Registered Member
Posts
104
Karma
0
Very nice!  How about importing Play Counts, Date first played, and Date last played?
User avatar
egork
Registered Member
Posts
6
Karma
0
Aaron Cooper wrote:Very nice!  How about importing Play Counts, Date first played, and Date last played?
Thanks for your appreciation!

To get this info from the xml file one just need to change the grep regexp to
# so concatenate them into one string
egrep "Play Count|Location" iTunesLib.00 \

and the following one to
# Get the values out of xml tags.
sed -e 's%.*\([0-9]\{1,\}\).*\(.*\).*%"\2" \1%g' |

BTW, I forgot to mention in the previous post, that I committed a small hack in converting double-digit ratings to one-digit by just omitting last digit in the source XML tag.

But with writing play counts into Amarok, there is a complication, because I do not see a DCOP call that could do that. If you will find it yourself you could of course then change the script. I am really an Amarok newbie.

Anyways, before adding new "features", I would like to get away from the dirty way with grep sed etc. to read XML file and use some XML tool instead, also convert URLs into filenames with some programm like recode or iconv. If anybody knows how to do it, I would appreciate a tip.
yesac142
Registered Member
Posts
7
Karma
0
Yes!  An xml import tool would be devine.  I'm trying to go from Winamp to Amarok, but should be the same.  Unforturnately, I'm a linux newbie and don't know squat about programming, so I can't really help with anymore development.  I'll try the script you have tonight (first I'll read up on how to even run script).  Anyway, I can tell you that your effort is very much appreciated!
User avatar
egork
Registered Member
Posts
6
Karma
0
yesac142 wrote: I'm trying to go from Winamp to Amarok, but should be the same.

Does Winamp store ratings in an XML file as well? If you wish you can send me an example file and I will try to write an import script for it.
headlessbob
Registered Member
Posts
4
Karma
0
Thanks for the awesome script, it worked perfectly to import the ratings. I just found this old topic though, that seems to have a perl script to deal with everything (which I assume accesses the amarok sqlite database directly rather than through a dcop call to amarok)

http://amarok.kde.org/forum/index.php/topic,11902.0.html
User avatar
egork
Registered Member
Posts
6
Karma
0
headlessbob wrote:Thanks for the awesome script, it worked perfectly to import the ratings. I just found this old topic though, that seems to have a perl script to deal with everything (which I assume accesses the amarok sqlite database directly rather than through a dcop call to amarok)

http://amarok.kde.org/forum/index.php/topic,11902.0.html


Thanks for pointing that out! It could have saved me a lot of time, however would have taken away the Spass of hacking the itunes.xml :-)

Now when there is a rather integrated solution to the import, would anybody step up and develop a plugin for Amarok?
headlessbob
Registered Member
Posts
4
Karma
0
I actually just finished a somewhat more complete set of scripts to do this (still not a plugin... but it worked for me so maybe others will be interested). I've posted the scripts at http://www.jobeus.net/~brian/itunesimport.html
Ya it would be nice somewhere down the road to have a plugin in Amarok to do this from iTunes as well as other media managers.
Cheers!
muro
Registered Member
Posts
3
Karma
0
I made an amarok-script (so you can install it in amarok and use it from there) for itunes ratings and last dale played, you can download it here:
http://www.slovenskoo.com/itunesratings .

I will try to add play counts and playlists (not smart playlists). I did't do much ruby programming yet, so the code might be ugly - if you want to change something - but it should work. Any comments or bug reports are appreciated - see the page for contact info.

Why create something, that has already been done? Well here are my reasons:
  • I didn't know other solutions when I started
  • This one is an amarok script, not bash or perl, so it should be easier to install and run
  • It is nicer  :cool: A file dialog prompts you for the library file and progress is shown

Last edited by muro on Thu Aug 31, 2006 7:16 am, edited 1 time in total.
e235
Registered Member
Posts
15
Karma
0
Headlessbob's script needs modifactions to import last played and first added dates.
iTunes appears to have slightly changed the lines storing date values in the XML file.

Specifically lines like:
    Date Added2003-12-28T14:52:18Z
    Play Date UTC2005-08-23T15:23:02Z

Tthe "Z" at the end of the date string is throwing off Date::Manip used in the script. So the script is trying to submit empty date values.

You need to change 2 lines in the script, adding the "Z":
elsif ($line =~ m#Date Added(.+?)Z#) {
and
elsif ($line =~ m#Play Date UTC(.+?)Z#) {

----
edit: I'm still having a problem getting amarok to recognize the new values. I think maybe the script needs to be modified to write the date added to the createdate field in the both the statistics and tags tables.

Last edited by e235 on Mon Nov 20, 2006 6:19 am, edited 1 time in total.


Bookmarks



Who is online

Registered users: abc72656, Bing [Bot], daret, Google [Bot], Sogou [Bot], Yahoo [Bot]