latoc.tcl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # created by horgh
  2. #
  3. package require http
  4. bind pub -|- "!oil" latoc::oil_handler
  5. bind pub -|- "!gold" latoc::gold_handler
  6. bind pub -|- "!c" latoc::commodity_handler
  7. bind pub -|- "!silver" latoc::silver_handler
  8. bind pub -|- "!url" latoc::url_handler
  9. namespace eval latoc {
  10. variable user_agent "Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7e"
  11. variable output_cmd putserv
  12. variable list_regexp {<tr><td class="first">.*?<td class="last">.*?</td></tr>}
  13. #variable stock_regexp {<a href="/q\?s=(.*?)">.*?<td class="second name">(.*?)</td><td><b><span id=".*?">(.*?)</span></b> <nobr><span .*?>(.*?)(?:</span>)??</nobr>.*?(?:alt="(.*?)">)?? <b style="color.*?;">(.*?)</b>.*?<b style="color.*?;"> \((.*?)\)</b>}
  14. variable stock_regexp {<a href="/q\?s=(.*?)">.*?<td class="second name">(.*?)</td>.*?<span id=".*?">(.*?)</span></b> <nobr><span id=".*?">(.*?)</span></nobr>.*?(?:alt="(.*?)">)?? <b style="color.*?;">(.*?)</b>.*?<b style="color.*?;"> \((.*?)\)</b>}
  15. # any names matching this pattern are not shown
  16. variable skip_regexp {(5000 oz)|(100 oz)}
  17. variable commodities [list energy metals grains livestock softs]
  18. variable energy_futures "http://finance.yahoo.com/futures?t=energy"
  19. variable commodities_url "http://finance.yahoo.com/futures?t="
  20. setudef flag latoc
  21. }
  22. proc latoc::url_handler {nick uhost hand chan argv} {
  23. $latoc::output_cmd "PRIVMSG $chan :$latoc::commodities_url"
  24. }
  25. # fetch lines from given commodity type (url) and only return lines that
  26. # match the given pattern (regexp) to Name (optional)
  27. # return list of lines, each a stock
  28. proc latoc::fetch {type {pattern {}}} {
  29. set token [http::geturl ${latoc::commodities_url}${type} -timeout 60000]
  30. set data [http::data $token]
  31. set ncode [http::ncode $token]
  32. http::cleanup $token
  33. if {$ncode != 200} {
  34. error "HTTP error: (code: $ncode): $data"
  35. }
  36. set lines []
  37. foreach stock [regexp -all -inline -- $latoc::list_regexp $data] {
  38. regexp $latoc::stock_regexp $stock -> symbol name price last direction change percent
  39. if {[regexp -- $pattern $name]} {
  40. if {[regexp -- $latoc::skip_regexp $name]} {
  41. continue
  42. }
  43. lappend lines [latoc::format $name $price $last $direction $change $percent]
  44. }
  45. }
  46. if {[llength $lines] == 0} {
  47. lappend lines "No results."
  48. }
  49. return $lines
  50. }
  51. proc latoc::commodity_handler {nick uhost hand chan argv} {
  52. if {![channel get $chan latoc]} { return }
  53. if {[lsearch $latoc::commodities $argv] == -1} {
  54. $latoc::output_cmd "PRIVMSG $chan :Valid commodities are: $latoc::commodities"
  55. return
  56. }
  57. if {[catch {latoc::fetch $argv} result]} {
  58. $latoc::output_cmd "PRIVMSG $chan :Error: $result"
  59. return
  60. }
  61. foreach line $result {
  62. $latoc::output_cmd "PRIVMSG $chan :$line"
  63. }
  64. }
  65. proc latoc::oil_handler {nick uhost hand chan argv} {
  66. if {![channel get $chan latoc]} { return }
  67. if {[catch {latoc::fetch "energy" "Crude Oil"} result]} {
  68. $latoc::output_cmd "PRIVMSG $chan :Error: $result"
  69. return
  70. }
  71. foreach line $result {
  72. $latoc::output_cmd "PRIVMSG $chan :$line"
  73. }
  74. }
  75. proc latoc::gold_handler {nick uhost hand chan argv} {
  76. if {![channel get $chan latoc]} { return }
  77. if {[catch {latoc::fetch "metals" "Gold"} result]} {
  78. $latoc::output_cmd "PRIVMSG $chan :Error: $result"
  79. return
  80. }
  81. foreach line $result {
  82. $latoc::output_cmd "PRIVMSG $chan :$line"
  83. }
  84. }
  85. proc latoc::silver_handler {nick uhost hand chan argv} {
  86. if {![channel get $chan latoc]} { return }
  87. if {[catch {latoc::fetch "metals" "Silver"} result]} {
  88. $latoc::output_cmd "PRIVMSG $chan :Error: $result"
  89. return
  90. }
  91. foreach line $result {
  92. $latoc::output_cmd "PRIVMSG $chan :$line"
  93. }
  94. }
  95. proc latoc::format {name price last direction change percent} {
  96. # this cuts off the Jun 09 part from Crude Oil Jun 09
  97. # set name [lrange $name 0 [expr [llength $name]-3]]
  98. return "$name: \00310$price [latoc::colour $direction $change] [latoc::colour $direction $percent]\003 $last"
  99. }
  100. proc latoc::colour {direction value} {
  101. if {[string match "Down" $direction]} {
  102. return \00304-$value\017
  103. } elseif {[string match "Up" $direction]} {
  104. return \00309+$value\017
  105. } else {
  106. return $value
  107. }
  108. }