Advertisement
Lee_Dailey

Itunes_Restore-R-PC-SC.ps1

Feb 25th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. file named - Itunes_Restore-R-PC-SC.ps1
  3.  
  4. what it does
  5. - reads rating, playedcount, skippedcount values stored in comment field
  6. - compares to itunes values, writes 'em to itunes if different
  7.  
  8. currently runs 1000 tracks in ~ 4 minutes if they all need to be changed
  9. currently runs 1000 tracks in ~ 30 seconds if none need to be changed
  10.  
  11. that seems to be fast enuf to auto-run daily with task scheduler
  12.  
  13. #>
  14.  
  15. # function list start
  16.  
  17. # write to console/file/both
  18. function WriteTo($what, [String]$where, [String]$filename)
  19.     {
  20.     if($where -ne "console" -and $filename -eq "")
  21.         {
  22.         $filename = -join ($Env:Temp, "\", "PowerShell_LogFile_", (get-date).ToString("yyyy-MM-dd"), ".log")
  23.         }
  24.     switch($where.ToLower())
  25.         {
  26.         "console" {$what | Write-Host}
  27.         "file"    {Add-Content -path $filename -value $what}
  28.         "both"    {Add-Content -path $filename -value $what -passthru}
  29.         default   {$what | Write-Host}
  30.         }
  31.     }
  32.  
  33. # function list end
  34.  
  35. $ItunesAlreadyRunning = (Get-Process | Where-Object {$_.ProcessName -eq "iTunes"}) -match "iTunes"
  36.  
  37. $iTunesApp = New-Object -comObject iTunes.Application
  38. $iMainLibrary = $iTunesApp.LibraryPlaylist
  39. $iTracks = $iMainLibrary.Tracks
  40.  
  41. $NumTracks = $iTracks.Count
  42. # itunes no longer uses "1" to represent ONLY the audio file "kind".
  43. # - that also is used for books and possibly other file "kind" info.
  44. # - switched to using "KindAsString" instead of "Kind" value for media type test.
  45. $AudioTrack = "MPEG audio file"
  46. $LogFile = -join ($Env:Temp, "\", $myinvocation.mycommand.name, "_", (get-date).ToString("yyyy-MM-dd"), ".log")
  47. $StartTime = Get-Date
  48.  
  49. # swap these two when you wanna test just a few tracks
  50. $x = $NumTracks
  51. #$x = 10
  52.  
  53. $ProcessedCount = 0
  54. $ChangedCount = 0
  55. while($x -gt 0)
  56.     {
  57.     $ProcessedCount ++
  58.     $iCurrentItem = $iTracks.Item($x)
  59.     if($iCurrentItem.KindAsString -eq $AudioTrack)
  60.         {
  61.         $Rating = $iCurrentItem.Rating
  62.         $PlayedCount = $iCurrentItem.PlayedCount
  63.         $SkippedCount = $iCurrentItem.SkippedCount
  64.         $Location = $iCurrentItem.Location
  65.         $Comment = $iCurrentItem.Comment
  66.         $Changed = $False
  67.         # get the stored rating
  68.         if($Comment -match "rating=(\d{1,3})")
  69.             {
  70.             $ComRating = [int] $Matches[1]
  71.             }
  72.             else
  73.             {
  74.             $ComRating = $Null
  75.             }
  76.         # get the stored playedcount
  77.         if($Comment -match "playedcount=(\d{1,})")
  78.             {
  79.             $ComPlayedCount = [int] $Matches[1]
  80.             }
  81.             else
  82.             {
  83.             $ComPlayedCount = $Null
  84.             }
  85.         # get the stored skippedcount
  86.         if($Comment -match "skippedcount=(\d{1,})")
  87.             {
  88.             $ComSkippedCount = [int] $Matches[1]
  89.             }
  90.             else
  91.             {
  92.             $ComSkippedCount = $Null
  93.             }
  94.  
  95.         # restore 'em if they aint the same
  96.         if($ComRating -ne $Null -and $Rating -ne $ComRating)
  97.             {
  98.             $iCurrentItem.Rating = $ComRating
  99.             $Changed = $True
  100.             }
  101.         if($ComPlayedCount -ne $Null -and $PlayedCount -ne $ComPlayedCount)
  102.             {
  103.             $iCurrentItem.PlayedCount = $ComPlayedCount
  104.             $Changed = $True
  105.             }
  106.         if($ComSkippedCount -ne $Null -and $SkippedCount -ne $ComSkippedCount)
  107.             {
  108.             $iCurrentItem.SkippedCount = $ComSkippedCount
  109.             $Changed = $True
  110.             }
  111.         if($Changed)
  112.             {
  113.             $ChangedCount ++
  114.             }
  115.  
  116.         #show what was done
  117.         "track index            = $x"
  118.         Write-Host ""
  119.         "OLD track rating       = $Rating"
  120.         "OLD track playedcount  = $PlayedCount"
  121.         "OLD track skippedcount = $SkippedCount"
  122.         Write-Host ""
  123.         "stored rating          = $ComRating"
  124.         "stored playedcount     = $ComPlayedCount"
  125.         "stored skippedcount    = $ComSkippedCount"
  126.         Write-Host ""
  127.         Write-Host ("itunes rating          =", $iCurrentItem.Rating)
  128.         Write-Host ("itunes playedcount     =", $iCurrentItem.PlayedCount)
  129.         Write-Host ("itunes skippedcount    =", $iCurrentItem.SkippedCount)
  130.         "track comment          = $Comment"
  131.         "track location         = $Location"
  132.         Write-Host "******************************"
  133.         Write-Host ""
  134.         }
  135.     $x --
  136.     }
  137.  
  138. # if itunes was NOT already running
  139. #   and itunes is NOT currently playing
  140. #   then we started it and we are not using it, so shut it down
  141. if((-not $ItunesAlreadyRunning) -and ($itunesApp.Playerstate -eq "0"))
  142.     {
  143.     $iTunesApp.Quit()
  144.     }
  145.  
  146. $StopTime = Get-Date
  147. $TotalTime = $StopTime - $StartTime
  148.  
  149. WriteTo (-join ("started at = ", $StartTime)) "both" $LogFile
  150. WriteTo (-join ("stopped at = ", $StopTime)) "both" $LogFile
  151. WriteTo (-join ("total time = ", $TotalTime.ToString())) "both" $LogFile
  152. WriteTo (-join ("processed  = ", $ProcessedCount)) "both" $LogFile
  153. WriteTo (-join ("changed    = ", $ChangedCount)) "both" $LogFile
  154. WriteTo (-join ("log file   = ", $LogFile)) "both" $LogFile
  155. WriteTo ("") "both" $LogFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement