Brian's Code

Code repository for my blog postings.

Wednesday, December 21, 2005

Oboe2M3u.vbs


'Builds a m3u playlist for each album found in your oboecache file for www.mp3tunes.com.
'Each playlist is saved in the current users home directory.


OPTION Explicit

dim sid 'Needed when requesting the mp3
dim sidFolder 'Needed when requesting the mp3
dim home 'Users Home folder
dim buffer 'Buffer for reading files
dim mp3Path 'Path to the mp3
dim artist 'Artist of the current mp3
dim album 'Album of the current mp3
dim duration 'Durration of the current mp3
dim title 'Title of the current mp3
dim seconds 'The length of the current mp3 in seconds
dim m3uPath 'Path to the playlist
dim setHeader 'If the playlist needs a header
dim durationParts 'Holds h m s for calculating seconds
dim keyCache 'Holds a list of keys to prevent duplicate songs in a playlist

dim oEnv 'Used to read the home drive & path from environment variables
dim oRegEx 'Regular Expression object: Used to find the sid
dim oFso 'FileSystemObject: Used to read & write the files
dim oShell 'Wscript Shell: Used to create the oEnv object
dim oShApp 'Shell.Application object: Used to read the ID3 tags
dim oNs 'Namespace object: Used to read the ID3 tags
Dim oItem 'Namespace Item: Used to read the ID3 tags
Dim oMatches 'Holds the matches from the regular expressions
dim oOboeLogStream 'TextStream for reading the log file
dim oOboeCacheStream'TextStream for reading the cache file
dim oM3u 'TextStream for writing the playlist
Dim oMp3 'FileObject for the current mp3

const REX_SID = "\?sid=([A-Za-z0-9]{32})"
const ENV = "PROCESS"
const ENV_HDRIVE = "HOMEDRIVE"
const ENV_HPATH = "HOMEPATH"
const FIL_OBOELOG = ".oboesync\oboe.log"
const FIL_OBOECACHE = ".oboesync\.oboecache"
const DET_AUTHOR = 9
const DET_TITLE = 10
const DET_ARTIST = 16
const DET_ALBUM = 17
const DET_TRACK = 19
const DET_DURATION = 21
const DET_BITRATE = 22

Set oShApp = CreateObject("Shell.Application")
Set oFso = CreateObject("Scripting.FileSystemObject")
Set oRegEx = New RegExp
Set oShell = CreateObject("Wscript.Shell")
Set oEnv = oShell.Environment(ENV)

'Get the home directory for the current user.
home = oEnv(ENV_HDRIVE) & oEnv(ENV_HPATH)
If Right(home, 1) <> "\" Then home = home & "\"

Set oEnv = Nothing
Set oShell = Nothing


'Open log file to pull the sid.
Set oOboeLogStream = oFso.OpenTextFile(home & FIL_OBOELOG)

buffer = oOboeLogStream.ReadAll

oOboeLogStream.Close
Set oOboeLogStream = Nothing

'Find the sid by using the regular expression
With oRegEx
.Pattern = REX_SID
.IgnoreCase = True
.Global = True
If .Test(buffer) Then
Set oMatches = .Execute(buffer)
sid = oMatches(0).SubMatches(0)
Else
MsgBox "Could not locate sid."
Wscript.Quit
End If
End With

buffer = Empty
Set oMatches = Nothing
Set oRegEx = Nothing

'Open the cache file.
Set oOboeCacheStream = oFso.OpenTextFile(home & FIL_OBOECACHE)

keyCache = "|"

Do Until oOboeCacheStream.AtEndOfStream
buffer = oOboeCacheStream.ReadLine

'Read the cache file line by line. It seems that all valid lines start with a "!"
If Left(buffer, 1) = "!" Then
'Grab folder sid, and the mp3 path
sidFolder = Mid(buffer, 2, 32)
mp3Path = Mid(buffer, 35, Len(buffer) - 33)

'Make sure the song is not a duplicate.
If InStr(1, keyCache, "|" & sidFolder & "|") = 0 Then
keyCache = keyCache & "|" & sidFolder & "|"
'If the mp3 exists, try reading its ID3 info, then write it to the playlist.
If oFso.FileExists(mp3Path) Then
Set oMp3 = oFso.GetFile(mp3Path)
'Try creating the namespace object
Set oNs = oShApp.NameSpace(oMp3.ParentFolder & "\")
If (Not oNs Is Nothing) Then
Set oItem = oNs.ParseName(oMp3.Name)

'Read the ID3 information
artist = oNs.GetDetailsOf(oItem, DET_ARTIST)
title = oNs.GetDetailsOf(oItem, DET_TITLE)
album = oNs.GetDetailsOf(oItem, DET_ALBUM)
duration = oNs.GetDetailsOf(oItem, DET_DURATION)

'Calculate the duration in seconds (this is required for the m3u file)
durationParts = Split(duration, ":")
seconds = durationParts(0) * 3600
seconds = seconds + durationParts(1) * 60
seconds = seconds + durationParts(2)

'Create a file name for the playlist.
m3uPath = home & CleanFileName(artist & " - " & album & ".m3u")

If oFso.FileExists(m3uPath) Then setHeader = false Else setHeader = true
Set oM3u = oFso.OpenTextFile(m3uPath, 8, true, 0)

'Write out the playlist information.
If setHeader Then oM3u.WriteLine "#EXTM3U"
oM3u.WriteLine "#EXTINF:" & seconds & "," & artist & " - " & title
oM3u.WriteLine "http://www.mp3tunes.com/storage/lockerget/" &_
sidFolder & "/?sid=" & sid & "&fileformat=mp3"

oM3u.Close
Set oM3u = Nothing
Set oItem = Nothing
End If
Set oNs = Nothing
Set oMp3 = Nothing
End If
End If
End If
Loop
oOboeCacheStream.Close

Set oOboeCacheStream = Nothing
Set oM3u = Nothing
Set oFso = Nothing

MsgBox "Done."

'Used to remove illegal characters from a file name.
Function CleanFileName(fileName)
dim i
dim buffer, output

For i = 1 To Len(fileName)
buffer = Mid(fileName, i, 1)
Select Case buffer
Case ":": buffer = "-"
Case "\": buffer = "-"
Case "/": buffer = "-"
Case "?": buffer = ""
Case "<": buffer = "["
Case ">": buffer = "]"
Case "*": buffer = "-"
Case "|": buffer = "-"
Case chr(34): buffer = "'"
Case chr(0): buffer = ""
End Select
output = output & buffer
Next

CleanFileName = output

End Function

0 Comments:

Post a Comment

<< Home