Tuesday, April 22, 2008

Grab Radio Shows Instantly

Whoohoo! I just found out the radio show I listen to is automatically posted every day on the newsgroup alt.binaries.sounds.radio.misc! This makes the program from my last post much simpler. Instead of capturing and encoding and tagging all I need to do is download the show using nget.

First I downloaded the nget binaries for Windows. I extracted this into "C:\nget" using 7-Zip. Then I setup the nget configuration with this .bat file.


@echo off
rem Create the _nget5 directory.
mkdir "%USERPROFILE%\nget5"
rem Copy a fresh _ngetrc
copy C:\nget\_ngetrc "%USERPROFILE%\nget5"
rem Open notepad to edit the file
notepad "%USERPROFILE%\nget5\_ngetrc"


The _ngetrc configuration file is fairly easy to setup. When notepad opens all you need is to look for the (halias line and change <yourhostalias> to MyHost, <yourhostaddress> to "news.whateveryourhostis.com", remove the "#" from the "user" and "password" lines, then change them to match your user name and password. In my case it was for GigaNews. So I changed it to news.giganews.com and then added my user name and password. Incredibly easy.

I then wrote this PureBasic script to fetch the file, extract it and clean up.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Change These Variables ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;

home.s = GetEnvironmentVariable("USERPROFILE")
dir.s = home + "/My Documents/My Music/Show"
tmpdir.s = home + "/Local Settings/Temp"
e7z.s = "C:/Program Files/7-Zip/7z.exe"
nget.s = "C:/nget/nget.exe"
group.s = "alt.binaries.sounds.radio.misc"

;;;;;;;;;;;;;;;;;;;;;
;; Do The Download ;;
;;;;;;;;;;;;;;;;;;;;;

started = Date()
showExp.s = FormatDate("Show.%dd-%mm-%yyyy.", started)
SetEnvironmentVariable("HOME", home)
SetEnvironmentVariable("TMPDIR", tmpdir)

; If it's Saturday or Sunday then skip out.
If DayOfWeek(started) = 0 Or DayOfWeek(started) = 6
PrintN("Show is weekdays only! Exiting..")
End
EndIf

; Grab the show.
If RunProgram(nget, "-g "+Chr(34)+group+Chr(34)+" -r "+Chr(34)+showExp+"*"+Chr(34), dir, #PB_Program_Wait) = 0
PrintN("NGet failed!")
End
EndIf

; Check to see if we got it.
If FileSize(showExp+"part1.rar") <= 0
PrintN("Failed to get show: "+showExp)
End
EndIf

; UnRar the files.
If RunProgram(e7z, "e "+showExp+"part1.rar", dir, #PB_Program_Wait) = 0
PrintN("Could not start 7-Zip for decompression!")
End
EndIf

; Check if that succeeded.
; Note: I could use ProgramExitCode() to see
; if it failed or not. I'm just being lazy.
If FileSize(showExp+"mp3") <= 0
PrintN("Decompression failed!")
End
EndIf

; Now unlink the .rar and .nzb files.
#dir = 0

; Note: ExamineDirectory() supports wildcards, that
; is what the +"*" is. It's almost as cool as
; Perl filename globbing.
If ExamineDirectory(#dir, dir, showExp+"*")
While NextDirectoryEntry(#dir)
; Skip "." and ".."
If DirectoryEntryType(#dir) = #PB_DirectoryEntry_File
name.s = DirectoryEntryName(#dir)
; Skip deleting the mp3.
If Right(LCase(name), 3) = "mp3"
Continue
Else
; But delete everything else.
If DeleteFile(dir+"/"+name) = 0
PrintN("Failed to remove file: "+dir+"/"+name+" : "+GetErrorDescription())
EndIf
EndIf
EndIf
Wend
EndIf


You know what the amazing thing is? When this is compiled I get a 25kB .exe file! That's all that is required to run it. I don't need any kind of interpreter, because there is no interpreter. It's a compiled language baby! This is the stuff magic is made of, the worlds first ultra high level compiled language.

No comments: