Hi, 

the following Powershell code is running here since some years stable
without any issues. I have named it TrackStat2iTunes. Runs on the
windows iTunes Machine. Keeps LastPlayed, Rating, PlayCount, Played in
sync from TrackStat to the iTunes Library.
Change the folder locations in the first 3 code lines to your needs. 

The code is splitted to 2 posts (size)

Have fun
Axel

Trackstat2iTunes.ps1 - Part 1
----------------------------
# Locations ?
# Dieser Script läuft auf dem iTunes Rechner
$TrackStat_iTunes_Hist_file =
"G:\Musik\Trackstat\TrackStat_iTunes_Hist.txt" # laufende History der
gespielten Titel
$TrackStat_iTunes_Complete_file =
"G:\Musik\Trackstat\TrackStat_iTunes_Complete.txt" # ? Manuelle Erzeugte
Datei - Custom Scan?
$ItunesMediaFolder = 'G:\Musik\iTunes Music'

# Debug ?
$Debug = 0 # Er tut nur so, macht aber nix, erhält auch den Trackstat
File, kann daher nur einen Durchlauf machen

# Loop ?
$Loop = $True

# Initializing
$ScriptFullPath = $script:MyInvocation.MyCommand.Path
$ScriptPath = Split-Path($ScriptFullPath) 
$ScriptPath = $ScriptPath + ""
$ScriptFileName = Split-Path ( $ScriptFullPath ) -Leaf
$ScriptName = $ScriptFileName.split('.')[-2]
$Log = $ScriptPath+$ScriptName+'.log' 

function Msg ($Msg, $ToLogFile, $Color) { 
# -------------------------------------          
if ($debug) { $Msg = '#DEBUG ' + $Msg }
switch ($Color) { 
Yello   { $Msg = "#WARN: $Msg" }
Red     { $Msg = "#ERR: $Msg" }
DarkCyan { $Msg = "#CHANGE: $Msg" }
default {$Color = 'Green' }
}
$Msg = "$($(Get-Date).ToString()) $Msg"        
Write-host $Msg -ForegroundColor  $Color
if ($ToLogFile) {
$Msg | Add-Content -Path $Log 
} # ToLogFile
if ($Color -eq 'Red') {
Send-MailMessage -From 'nisi1...@kpnmail.nl' -SmtpServer
'smtp.kpnmail.nl' -To 'axel.arn...@outlook.com' -Subject
"TrackStat2iTunes: $Msg"
}
} # function Msg

############
### Main ###
############

Msg "$ScriptName starting" $True
$waiting = $false
Do { # Loop

# Which Trackstat File to handle?    
$iTunes_Update_file = $null
if (Test-Path $TrackStat_iTunes_Complete_file) { $iTunes_Update_file
= $TrackStat_iTunes_Complete_file }
elseif (Test-Path $TrackStat_iTunes_Hist_file) { $iTunes_Update_file
= $TrackStat_iTunes_Hist_file }
elseif ($Loop) {
$S = 60
if (-not $waiting) { Msg "Nothing to do, waiting (checking news
every $S s) ..." }
$waiting = $True
sleep $S
} # Nix zu tun

if ($iTunes_Update_file) { # Something todo
$waiting = $False

Msg "Found $iTunes_Update_file"

# Connect to iTunes, read Tracks
if (-not (get-process -Name 'iTunes' -ErrorAction
SilentlyContinue)) {
Msg "iTunes not running!" $True 'Red'
sleep 3600
continue
}
Msg "Connecting to iTunes ..."
$iTunes = New-Object -ComObject iTunes.Application
if (-not $iTunes) {
Msg "Cannot connect to iTunes!" $True 'Red'
Sleep 3600
continue
#Exit
} # No connect
$Itunes_Tracks = $iTunes.LibraryPlaylist.Tracks
$LstLibChange = (Get-Item $iTunes.LibraryXMLPath).LastWriteTime
Msg "Succsessfully connected to iTunes,
$($iTunes.LibraryPlaylist.Name) has $($Itunes_Tracks.count) Items, last
Change: $($LstLibChange.ToString()) "

# (Re-)Build Hashtable with iTunes Locations and Index Numbers?
# iTunes Lib has changed ?
if ( ($Itunes_Tracks.Count -ne $Lst_iTunes_Track_Count) -or
($LstLibChange -gt $global:LstHTtime) ) {
if ($global:LstHTtime) { Msg "iTunes Lib changed at
$($LstLibChange.ToString()). Last Hashtable TimeStamp is
$($global:LstHTtime.ToString())" }
Msg "Building iTunes Track Location Hashtable (5 ms/Track)
..."
$HashTable = @{}
$InitTime = Measure-Command { 
ForEach ($_ in $Itunes_Tracks) { if ($_.Location) {
$HashTable.Add($_.Location,$_.Index) } }
#elseif ($Track.Kind -eq 1) { $Itunes_Track | FT } #
Track Location not found!
} # Measure-Command
if ($HashTable) {
Msg "Finished iTunes Hash building. $($HashTable.count)
Tracks with Location added in $([math]::Round($InitTime.TotalSeconds,1))
Seconds."
$global:LstHTtime = Get-Date
}
else { Msg "iTunes Hash building failed" $True 'Red' }
$Lst_iTunes_Track_Count = $Itunes_Tracks.Count
} # (Re-)Build Hashtable with iTunes Locations and Index
Numbers

<#
"Writing Hashtable to file ..."
Remove-Item -path $($ScriptPath+'HashTable.txt')
foreach ($key in $HashTable.keys) {
Add-Content -path $($ScriptPath+'HashTable.txt') "$key ;
$($HashTable.$key)" 
} # Write Hashtabe items
#>

# Read Trackstat File
Msg "Reading Trackstat File $iTunes_Update_file"
$TrackStat_Workfilename = "$iTunes_Update_file.Unicode"
Get-Content "$iTunes_Update_file" | Out-File
$TrackStat_Workfilename -Encoding Unicode         
$Trackstat_Items = $Null
$Trackstat_Items = Import-Csv -Path $TrackStat_Workfilename
-Delimiter '|' -Header
"title","artist","album","location","change","playedDate","rating","playedCount","UnknownID"

# The Field 'change' shows what has changed. Used are 'rated' or
'played' (rating change, Last Played Timestamp Change)
# The Delimiter '|' must not exist in Strings! :-)
-------
EO Part 1


------------------------------------------------------------------------
Aki7's Profile: http://forums.slimdevices.com/member.php?userid=67596
View this thread: http://forums.slimdevices.com/showthread.php?t=93450

_______________________________________________
plugins mailing list
plugins@lists.slimdevices.com
http://lists.slimdevices.com/mailman/listinfo/plugins

Reply via email to