#!/usr/local/bin/rebol -cs ; ; usage: ; http://localhost/jukebox.r ; http://localhost/jukebox.r?max=50 ; http://localhost/jukebox.r?search=xyz ; http://localhost/jukebox.r?action=update ; http://localhost/jukebox.r?action=test ; http://localhost/jukebox.r?action=test&search=xyz ; REBOL [] ;print "Content-type: text/plain^/" jukebox: make object! [ database: %songs.dat cache: make block! 5000 alpha: uppercase "abcdefghijklmnopqrstuvwxyz" compose-url: func [ c [ char! ] /local base ] [ base: http://stream.sil.at:3333/file/ return to-url rejoin [ base c "/" ] ] update-database: func [ /local data char url content filename ] [ ; sil.at obviously is checking the user agent... system/schemes/http/user-agent: "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3" data: make block! 0 foreach char alpha [ print join "Retrieving song titles starting with the letter " char url: compose-url char foreach filename extract-filenames read url [ append/only data reduce [ char filename ] ] ] save database data print "Database was updated successfully." return ] extract-filenames: func [ html [ string! ] /local list filename ] [ list: make block! 0 parse html [ any [ thru {dummy.pls">} copy filename to (append list filename) ] ] return list ] render-playlist: func [ max /local nr song ] [ random/seed now for nr 1 max 1 [ song: random/only cache print join compose-url first song second song ] return ] search-database: func [ str [ string! ] /local song max cnt ] [ cnt: 0 max: 100 foreach song cache [ if find second song str [ print join compose-url first song second song cnt: cnt + 1 ] if cnt >= 100 [ break ] ] return ] main: func [ /local query max ] [ if not exists? database [ print "Content-type: text/plain^/" print "Sorry, the jukebox is currently out of order." return ] cache: load database default!: make object![ max: 10 action: none search: none ] if error? try [ query: make default! decode-cgi system/options/cgi/query-string ][ query: make default! [] ] prin "Content-type: " prin either query/action [ "text/plain" ][ "audio/x-mpegurl" ] print "^/" max: minimum to-integer query/max 100 either query/action = "update" [ update-database ][ either query/search [ search-database query/search ][ print "#EXTM3U" render-playlist max ] ] return ] ] args: system/script/args either none? args [ jukebox/main ][ if (first args) = "update" [ jukebox/update-database ] ]