Tuesday, October 25, 2011

Exporting songs in Windows Media Player by star rating

So, I used this some forever ago to move song ratings from iTunes to Windows Media Player with a powershell function.  Now I wanted to export the ratings to a file so I have that list outside of wmp.  Annoyingly, i didn't see a way to export the song list, saving the playlist just saved the search terms.  Meh, powershell to the rescue, yet again...

This function will export the name of all files w/ ‘-starcount’ stars.  Could be trivially modified to export other details.


function get-playlistbyStars {
      param ($starcount = $(throw "need -starcount")
           )
     
      #get WMP
      $wmp = New-object -COM WMPlayer.OCX
      $WMPLibrary= $wmp.mediaCollection
      #set vars
      $rated = 0
      $processed = 0
      $added = 0
      $unrated = 0
      $1star = 0
      $2star = 0
      $3star = 0
      $4star = 0
      $5star = 0
      $defstar = 0
      $ctr = 0
      $allsongs = $WMPLibrary.getall()
      $colOut = @()
      for ($i = 0; $i -lt $allsongs.count; $i++) {
            $processed++
            $song = $allsongs.Item($i)
            if (@(".jpg",".png") -notcontains ([System.IO.FileInfo]$song.sourceURL).Extension ) {
                  switch ($song.getiteminfo("UserRating")) {
                        "0" {
                              $unrated++
                              if ($starcount -eq 0) {
                                    $colOut += $song.sourceURL
                              }
                        }    
                        "1" {
                              $1star++
                              $rated++
                              if ($starcount -eq 1) {
                                    $colOut += $song.sourceURL
                              }
                        }
                        "25" {
                              $2star++
                              $rated++
                              if ($starcount -eq 2) {
                                    $colOut += $song.sourceURL
                              }
                        }
                        "50" {
                              $3star++
                              $rated++
                              if ($starcount -eq 3) {
                                    $colOut += $song.sourceURL
                              }
                        }
                        "75" {
                              $4star++
                              $rated++
                              if ($starcount -eq 4) {
                                    $colOut += $song.sourceURL
                              }
                        }
                        "99" {
                              $5star++
                              $rated++
                              if ($starcount -eq 5) {
                                    $colOut += $song.sourceURL
                              }
                        }
                        default {
                              #so I have something to query for random nums
                              $defstar++
                              if ($starcount -eq 42) {
                                    $colOut += $song.sourceURL
                              }

                        }
                  }
                  #so I have something to watch
                  $ctr++
                  if ($ctr%500 -eq 0) { Write-verbose $ctr}

            }
      }

      #output results
      Write-verbose "Processed:  $processed"
      Write-verbose "Rated:  $rated"
      Write-verbose "Added:  $added"
      Write-verbose "1:  $1star "
      Write-verbose "2:  $2star "
      Write-verbose "3:  $3star"
      Write-verbose "4:  $4star "
      Write-verbose "5:  $5star"
      Write-verbose "def:  $defstar"
     
      $colOut
}
     
get-playlistbyStars 3 | out-file –filepath .\3starsongs.txt


No comments:

Post a Comment

analytics