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

Adjusting layout

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur ligula. Aenean quis tortor ut nibh dignissim tristique. Curabitur urna magna, pretium ut, sodales in, viverra ac, sapien. Nullam ut ligula a elit luctus sagittis. Duis malesuada leo non sem. Vivamus risus nibh, porta eu, tincidunt ut, aliquam in, arcu. Integer lacus lacus, euismod et, egestas non, sollicitudin ut, elit. Duis enim nulla, posuere at, viverra ultrices, venenatis ac, nisi. Vivamus est orci, egestas quis, laoreet nec, pretium eu, mi. Phasellus eget magna sed purus pharetra aliquam. Donec purus magna, tristique id, ullamcorper quis, vestibulum nec, lectus. Proin ligula. Vivamus pulvinar nunc et risus.

Morbi auctor. Nunc suscipit, nisl eget varius nonummy, sem tellus nonummy erat, ut hendrerit justo tellus ut est. Cras viverra neque quis eros ultrices scelerisque. Ut pulvinar, risus ut elementum tristique, dolor ipsum pharetra erat, vel congue quam ipsum in ipsum. Morbi at augue. Nunc commodo, turpis eget mattis fringilla, urna tellus ultricies lectus, vitae fringilla nibh mauris at mi. Nulla feugiat bibendum lectus. Etiam risus nunc, blandit quis, dignissim nec, venenatis vel, massa. Vivamus elementum orci quis urna. Duis dui orci, elementum at, malesuada at, vulputate vitae, dui. Nulla facilisi. Integer interdum accumsan orci. Sed convallis neque. Mauris aliquam, ligula vel auctor pretium, sem odio vulputate velit, in tristique risus diam vel est. Cras sapien nibh, gravida vel, elementum sed, consectetuer at, eros. In hac habitasse platea dictumst. Suspendisse luctus dui id elit. Sed quis purus. Morbi eu dui. Sed laoreet interdum libero.

Aenean feugiat elit vitae mauris. Cras ut nulla. Duis scelerisque fringilla justo. In gravida, tellus eu facilisis sollicitudin, nulla felis dictum eros, sed pellentesque sapien justo nec augue. Maecenas vestibulum malesuada dolor. Mauris vehicula aliquet dolor. Nulla velit arcu, consectetuer vitae, bibendum eu, nonummy sed, eros. In magna est, consequat faucibus, consectetuer vitae, varius tincidunt, est. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris in enim ac nunc condimentum dignissim. Morbi diam metus, congue sit amet, semper et, feugiat eu, lectus. Integer sodales magna quis justo. Proin massa turpis, tristique quis, bibendum sed, facilisis sit amet, nulla. Aliquam id elit nec odio vehicula euismod.

Duis pellentesque metus ac mauris. Morbi ac ante quis tortor porta imperdiet. Praesent suscipit leo non libero. Sed euismod laoreet metus. Donec aliquam. Donec lectus quam, faucibus non, ullamcorper sit amet, sodales varius, diam. Phasellus mollis, orci eget hendrerit varius, neque tortor pulvinar pede, a scelerisque est leo pulvinar tortor. Fusce dolor dui, lacinia vel, interdum nec, posuere id, elit. In tempus. Praesent accumsan risus vitae massa. Cras vitae nibh. Integer rhoncus congue dolor. In hac habitasse platea dictumst. Praesent egestas, tortor et pulvinar euismod, augue leo tristique arcu, id porttitor augue dolor sed dui.

Nunc accumsan magna in ante. Aenean tempus mi molestie dolor. Sed interdum lacus eu dolor. Proin iaculis, felis eget dictum aliquam, mi risus suscipit magna, sit amet pharetra magna turpis ac nunc. Aliquam eget leo at lacus tempor scelerisque. Nulla facilisi. Vestibulum tincidunt quam vel orci. Nulla imperdiet magna eget lorem. Cras vel dui. Nulla interdum mauris sed odio.