latoc.tcl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Provides binds to read Yahoo.com futures
  2. #
  3. # If you update this, update the one in
  4. # https://github.com/horgh/irssi-tcl-scripts.
  5. package require http
  6. namespace eval ::latoc {
  7. variable output_cmd putserv
  8. variable user_agent "Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7e"
  9. variable list_regexp {<tr class="data-row.*?".*?</a></td></tr>}
  10. variable stock_regexp {<td class="data-col0.*>(.*)</a></td><td class="data-col1.*>(.*)</td><td class="data-col2.*>(.*)</td><td class="data-col3.*>(.*)</td><td class="data-col4.*>(.*)<!-- /react-text --></span></td><td class="data-col5.*>(.*)<!-- /react-text --></span></td><td class="data-col6.*>(.*)</td><td class="data-col7.*>(.*)</td><td class="data-col8.*"}
  11. variable url "https://finance.yahoo.com/commodities?ltr=1"
  12. bind pub -|- "!oil" ::latoc::oil_handler
  13. bind pub -|- "!gold" ::latoc::gold_handler
  14. bind pub -|- "!silver" ::latoc::silver_handler
  15. setudef flag latoc
  16. }
  17. proc ::latoc::fetch {chan} {
  18. ::http::config -useragent $::latoc::user_agent
  19. set token [::http::geturl $::latoc::url -timeout 20000]
  20. set status [::http::status $token]
  21. if {$status != "ok"} {
  22. set http_error [::http::error $token]
  23. $::latoc::output_cmd "PRIVMSG $chan :HTTP error: $status: $http_error"
  24. ::http::cleanup $token
  25. return
  26. }
  27. set ncode [::http::ncode $token]
  28. if {$ncode != 200} {
  29. set code [::http::code $token]
  30. $::latoc::output_cmd "PRIVMSG $chan :HTTP error: $ncode: $code"
  31. ::http::cleanup $token
  32. return
  33. }
  34. set data [::http::data $token]
  35. ::http::cleanup $token
  36. return $data
  37. }
  38. proc ::latoc::parse {data} {
  39. set lines []
  40. foreach stock [regexp -all -inline -- $::latoc::list_regexp $data] {
  41. regexp $::latoc::stock_regexp $stock -> symbol name price last change percent volume interest
  42. set direction none
  43. if {$change < 0} {
  44. set direction Down
  45. }
  46. if {$change > 0} {
  47. set direction Up
  48. }
  49. lappend lines [::latoc::format $name $price $last $direction $change $percent]
  50. }
  51. return $lines
  52. }
  53. proc ::latoc::output {chan lines symbol_pattern} {
  54. foreach line $lines {
  55. if {![regexp -- $symbol_pattern $line]} {
  56. continue
  57. }
  58. $::latoc::output_cmd "PRIVMSG $chan :$line"
  59. }
  60. }
  61. proc ::latoc::oil_handler {nick uhost hand chan argv} {
  62. if {![channel get $chan latoc]} { return }
  63. set data [::latoc::fetch $chan]
  64. set lines [::latoc::parse $data]
  65. ::latoc::output $chan $lines {Crude Oil}
  66. }
  67. proc ::latoc::gold_handler {nick uhost hand chan argv} {
  68. if {![channel get $chan latoc]} { return }
  69. set data [::latoc::fetch $chan]
  70. set lines [::latoc::parse $data]
  71. ::latoc::output $chan $lines {Gold}
  72. }
  73. proc ::latoc::silver_handler {nick uhost hand chan argv} {
  74. if {![channel get $chan latoc]} { return }
  75. set data [::latoc::fetch $chan]
  76. set lines [::latoc::parse $data]
  77. ::latoc::output $chan $lines {Silver}
  78. }
  79. proc ::latoc::format {name price last direction change percent} {
  80. return "$name: \00310$price [::latoc::colour $direction $change] [::latoc::colour $direction $percent]\003 $last"
  81. }
  82. proc ::latoc::colour {direction value} {
  83. if {[string match "Down" $direction]} {
  84. return \00304$value\017
  85. }
  86. if {[string match "Up" $direction]} {
  87. return \00309$value\017
  88. }
  89. return $value
  90. }
  91. putlog "latoc.tcl loaded"