Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # /usr/bin/tclsh8.5 \
- exec tclsh8.5 "$0" "$@"
- set urlUA "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.11) Gecko/20101013 Firefox/3.6.11"
- set urlHeaders [list "Accept-Language" "en, *" \
- "Accept-Encoding" "" \
- "Accept-Charset" "UTF-8,*"]
- set urlTimeOut 50000
- package require http
- proc fetch_quote {argv} {
- set http [::http::config -useragent $::urlUA -urlencoding "utf-8" \
- -accept "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"]
- if {[catch {set tok [::http::geturl $argv -headers $::urlHeaders -timeout $::urlTimeOut]} errmsg]} {
- set output $errmsg
- }
- switch [::http::ncode $tok] {
- "200" {
- if {[string match -nocase "*couldn't open socket*" $errmsg]} {
- set output "Socket Error:$errmsg"
- }
- if { [::http::status $tok] == "timeout" } {
- ::http::cleanup $tok
- set output "Timeout:timeout:$url"
- }
- set html [::http::data $tok]
- set ghtml ""
- foreach line [split $html \n] {append ghtml $line}
- regexp -nocase -- {<div class="vdmContent">.*<p>(.+?)</p>.*<div.*>.*</div>} $html -> vdm
- set output $vdm
- }
- default {
- set output "Error: [::http::ncode $tok]"
- }
- }
- ::http::cleanup $tok
- return $output
- }
- #argv is read from the cli
- puts "The quote: [fetch_quote $argv]"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement