minimalist wget

2004-10-29 VI This is a 10 line replacement for the far-more-capable wget program.
This only works for http and just gets a url and puts it in a file. To invoke - have an alias like this:

 alias wget '/usr/local/tcl/8.4.7/bin/tclsh8.4 ~/tcltools/wget.tcl \!*'

And use it as

 wget https://wiki.tcl-lang.org/12871

Code for wget.tcl - only requires the tcl core:

  package require http

  set url [lindex $argv 0]
  set filename [file tail $url]
  set r [http::geturl $url -binary 1]

  set fo [open $filename w]
  fconfigure $fo -translation binary
  puts -nonewline $fo [http::data $r]
  close $fo

  ::http::cleanup $r
  puts "Got $url -> $filename"

male - 30th October 2004:

If the file should be written in binary mode, then the data should be requested and transferred as binary too!

I changed the code above to get the url in binary mode.


HJG 2012-12-28 - Here is a variation of the above program that downloads a list of files / webpages:

 #: minimal wget - wiki.tcl.tk/12871 / Radio-edition - 2012-12-28
  package require http

  puts "# Download playlists for webradio HR4 ..."

 #: Assemble list of wanted files, each one needs an URL, and a filename for saving.
 # In this example: Radio-playlists (containing 1 hour each) for a given date at several hours
 # One of those URLs looks like:
 #set url "http://playlist.hr-online.de/playlist/playlist.php?tpl=hr4&datum=2012-12-28&uhr=22"
  set D1 "2012-12-28"
  set FL {21 22 23} 
  foreach D2 $FL {
    set url "http://playlist.hr-online.de/playlist/playlist.php?tpl=hr4&datum=$D1&uhr=$D2"
    set filename "HR4_$D1\_$D2.htm"

 ## puts "URL=$url."
 ## puts "filename=$filename."

    set r  [http::geturl $url -binary 1]
    set fo [open $filename w]
    fconfigure $fo -translation binary
    puts -nonewline $fo [http::data $r]
    close $fo
    ::http::cleanup $r
    puts "Got $url -> $filename"
  }
  puts "# Done."

Note: in the above example, the playlists from that radiostation are only filled upto the current time. So, you cannot peek at what will be played next, and using clock to set the date in the url would be of little use :( Also, only the playlists of the past 2 weeks are available. I hope this little script will be useful to you, and happy new year :)

Todo: couple this with Stephen Uhler's HTML parser in 10 lines, to extract the actual data from those playlists.