1
0

latoc.tcl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. package require tls
  7. ::http::register https 443 [list ::tls::socket -ssl2 0 -ssl3 0 -tls1 1]
  8. namespace eval ::latoc {
  9. variable output_cmd putserv
  10. variable user_agent "Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7e"
  11. variable list_regexp {<tr class="data-row.*?".*?</a></td></tr>}
  12. 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.*>(.*)</span></td><td class="data-col5.*>(.*)</span></td><td class="data-col6.*>(.*)</td><td class="data-col7.*>(.*)</td><td class="data-col8.*"}
  13. variable url "https://finance.yahoo.com/commodities?ltr=1"
  14. bind pub -|- "!oil" ::latoc::oil_handler
  15. bind pub -|- "!gold" ::latoc::gold_handler
  16. bind pub -|- "!silver" ::latoc::silver_handler
  17. setudef flag latoc
  18. }
  19. proc ::latoc::fetch {chan} {
  20. ::http::config -useragent $::latoc::user_agent
  21. set token [::http::geturl $::latoc::url -timeout 20000]
  22. set status [::http::status $token]
  23. if {$status != "ok"} {
  24. set http_error [::http::error $token]
  25. $::latoc::output_cmd "PRIVMSG $chan :HTTP error: $status: $http_error"
  26. ::http::cleanup $token
  27. return
  28. }
  29. set ncode [::http::ncode $token]
  30. if {$ncode != 200} {
  31. set code [::http::code $token]
  32. $::latoc::output_cmd "PRIVMSG $chan :HTTP error: $ncode: $code"
  33. ::http::cleanup $token
  34. return
  35. }
  36. set data [::http::data $token]
  37. ::http::cleanup $token
  38. return $data
  39. }
  40. proc ::latoc::parse {data} {
  41. set lines []
  42. foreach stock [regexp -all -inline -- $::latoc::list_regexp $data] {
  43. if {![regexp $::latoc::stock_regexp $stock -> symbol name price last change percent volume interest]} {
  44. error "error parsing HTML"
  45. }
  46. set direction none
  47. if {$change < 0} {
  48. set direction Down
  49. }
  50. if {$change > 0} {
  51. set direction Up
  52. }
  53. lappend lines [::latoc::format $name $price $last $direction $change $percent]
  54. }
  55. return $lines
  56. }
  57. proc ::latoc::output {chan lines symbol_pattern} {
  58. foreach line $lines {
  59. if {![regexp -- $symbol_pattern $line]} {
  60. continue
  61. }
  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. set data [::latoc::fetch $chan]
  68. set lines [::latoc::parse $data]
  69. ::latoc::output $chan $lines {Crude Oil}
  70. }
  71. proc ::latoc::gold_handler {nick uhost hand chan argv} {
  72. if {![channel get $chan latoc]} { return }
  73. set data [::latoc::fetch $chan]
  74. set lines [::latoc::parse $data]
  75. ::latoc::output $chan $lines {Gold}
  76. }
  77. proc ::latoc::silver_handler {nick uhost hand chan argv} {
  78. if {![channel get $chan latoc]} { return }
  79. set data [::latoc::fetch $chan]
  80. set lines [::latoc::parse $data]
  81. ::latoc::output $chan $lines {Silver}
  82. }
  83. proc ::latoc::format {name price last direction change percent} {
  84. return "$name: \00310$price [::latoc::colour $direction $change] [::latoc::colour $direction $percent]\003 $last"
  85. }
  86. proc ::latoc::colour {direction value} {
  87. if {[string match "Down" $direction]} {
  88. return \00304$value\017
  89. }
  90. if {[string match "Up" $direction]} {
  91. return \00309$value\017
  92. }
  93. return $value
  94. }
  95. putlog "latoc.tcl loaded"