Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #!/bin/sh
  2. # /usr/bin/tclsh8.5 \
  3. exec tclsh8.5 "$0" "$@"
  4.  
  5. set urlUA "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.11) Gecko/20101013 Firefox/3.6.11"
  6. set urlHeaders [list "Accept-Language" "en, *" \
  7. "Accept-Encoding" "" \
  8. "Accept-Charset" "UTF-8,*"]
  9. set urlTimeOut 50000
  10.  
  11. package require http
  12.  
  13. proc fetch_quote {argv} {
  14. set http [::http::config -useragent $::urlUA -urlencoding "utf-8" \
  15. -accept "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"]
  16. if {[catch {set tok [::http::geturl $argv -headers $::urlHeaders -timeout $::urlTimeOut]} errmsg]} {
  17. set output $errmsg
  18. }
  19. switch [::http::ncode $tok] {
  20. "200" {
  21. if {[string match -nocase "*couldn't open socket*" $errmsg]} {
  22. set output "Socket Error:$errmsg"
  23. }
  24. if { [::http::status $tok] == "timeout" } {
  25. ::http::cleanup $tok
  26. set output "Timeout:timeout:$url"
  27. }
  28. set html [::http::data $tok]
  29. set ghtml ""
  30. foreach line [split $html \n] {append ghtml $line}
  31. regexp -nocase -- {<div class="vdmContent">.*<p>(.+?)</p>.*<div.*>.*</div>} $html -> vdm
  32. set output $vdm
  33. }
  34. default {
  35. set output "Error: [::http::ncode $tok]"
  36. }
  37. }
  38. ::http::cleanup $tok
  39. return $output
  40. }
  41. #argv is read from the cli
  42. puts "The quote: [fetch_quote $argv]"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement