Importing iTunes ratings to Plex

Hi everyone,

I needed to import my iTunes ratings to Plex so I created a small Python script to do so.
I’m sharing it here so it may help other people who are looking to switch to Plex but don’t want to loose their iTunes ratings.

  • It uses the iTunes library xml file and connects to Plex.
  • It does not modify the Plex SQLite database directly. I could have done that for faster processing but I opted for cleaner approach with less risks.

I’ll describe below on how to install and use the script on Windows 10. Other OS platforms should be similar.

# Important notes. MUST READ!!!

  1. Using this is your own responsibility. I’m just sharing this.
    No support whatsoever in case of irreversible problems!
    Backup Plex before is always recommended!

  2. I’m not a developer. This means that the script can probably be optimized quite a bit. However, it’s ‘good enough’ for me
    Feel free to do so if you have the knowledge/time :slight_smile:

  3. I did notice that iTunes likes to put fake ratings in the xml file!
    When 1 song of an album is rated 5 stars and all other songs are unrated, then every song in that album had 5 stars in the xml file.
    What I did to solve this was bulk setting all unrated tracks to 1 star.
    I don’t have 1 star ratings because if a track has 1 star, it deserves to be deleted from my library :).
    The script will ask if you want to rate 1 star tracks.
    This is a limitation of the iTunes xml file and not of this script.

  4. The script has been optimized for subsequent runs when overwriting existing ratings is set to false
    This means that if your iTunes library has a lot of unrated items it will still try to match them all.
    I might create a second script which does the opposite and only tries to update when the ratings exists in iTunes.
    But due to the previous ‘point 3’ I don’t see any reason to do this.
    (Don’t worry: when overwriting existing ratings is enabled, it only will update different ratings to increase performance).

  5. The script loads the entire xml file and Plex music library in memory.
    In my case ± 60.000 songs resulted into 1GB-1.5GB of memory usage.
    Make sure you have enough RAM on your machine if running this on a huge library!

  6. Performance greatly depends on how many tracks are already rated and how many are not rated in iTunes.
    If you have an unrated Plex library and an almost fully rated iTunes library the inital sync will take quite some time.
    But at least it is automated and you can just let it run…
    This is due to using HTTP calls instead of modifying the DB directly.
    Whenever it needs to rate a track it has to create a new connection to PMS.

  7. The script is for Plex servers running without SSL. For SSL connections you’ll need to make the modifications yourself.

  8. The script will create a details.log file in the same directory which contains details about what it has done to each track.

  9. The script will only update exact matches (Title, Artist & Album). If your tracks have weird characters in them, they might not be matched.

  10. To cancel the script at any time, press <CTRL+C>

  11. You can repeat the script at any time, it will go much faster for the items it has already done

# Installation & usage

  1. Create a working directory
    As an example, I’m using “C:\Users\Nick\Desktop\itunes2plex”

  2. Download the script in attachment (import_ratings.py) to this directory

  3. Install Python 3
    The simplest way to do this is opening a command prompt and execute the following: python
    It should launch the Windows Store from which you can install it.
    Otherwise just go to ‘https://www.python.org/’ and follow instructions.

  4. Install pip
    Download “get-pip.py” (https://bootstrap.pypa.io/get-pip.py) to a folder on your computer.
    Open a command prompt and navigate to the folder containing get-pip.py.
    Run the following command: “python get-pip.py”

  5. Install libpytunes dependency (Python Itunes Library parser)
    Download and extract (or git clone) GitHub - liamks/libpytunes: Python Itunes Library parser to ‘C:\Users\Nick\Desktop\itunes2plex\libpytunes’
    Open command prompt and navigate to ‘C:\Users\Nick\Desktop\itunes2plex\libpytunes’
    Execute the following commands:
    pip install -r requirements.txt --user
    python setup.py install --user

  6. Install python-plexapi dependency (Python bindings for the Plex API)
    Open command prompt and execute the following command: pip install plexapi --user

  7. iTunes xml library file
    Copy your itunes xml library file to ‘C:\Users\Nick\Desktop\itunes2plex’
    the file is named ‘iTunes Library.xml’ on mac or ‘iTunes Music Library.xml’ on Windows
    You can find the file in the following directory: ‘C:\Users\Nick\Music\iTunes’
    If the file does not exist, you have to open iTunes. Go to Edit, Preferences, Advanced and check ‘Share iTunes Library XML with other applications’ and close iTunes
    (I recommend disabling this after you’ve copied the file as it creates unnecessary writes on disk)

  8. Modify import_ratings.py
    Open the script import_ratings.py in your favorite text editor and modify the following values:
    (Please see Plex documentation on how to get an authentication token)
    For example:
    plexUrl = ‘http://192.168.0.30:32400/:/rate
    plexName = ‘NAS’
    plexToken = ‘somerandomtokenwith20chars’
    plexAccount = ‘testmailaccount@gmail.com’
    plexPassword = ‘secret’
    plexIdentifier = ‘com.plexapp.plugins.library’
    itunesLibraryName = ‘Itunes Music Library.xml’
    logFile = ‘details.log’

  9. Run the script
    Open command prompt and navigate to ‘C:\Users\Nick\Desktop\itunes2plex’
    Execute the following command: python import_ratings.py
    It will ask you whether or not to overwrite existing ratings and to import 1 star ratings also

# Credits:

# Download
import_ratings.zip (1.5 KB)

Edit: new version

11 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.