latoc.tcl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.*>(.*)<!-- /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.*"}
  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. regexp $::latoc::stock_regexp $stock -> symbol name price last change percent volume interest
  44. set direction none
  45. if {$change < 0} {
  46. set direction Down
  47. }
  48. if {$change > 0} {
  49. set direction Up
  50. }
  51. lappend lines [::latoc::format $name $price $last $direction $change $percent]
  52. }
  53. return $lines
  54. }
  55. proc ::latoc::output {chan lines symbol_pattern} {
  56. foreach line $lines {
  57. if {![regexp -- $symbol_pattern $line]} {
  58. continue
  59. }
  60. $::latoc::output_cmd "PRIVMSG $chan :$line"
  61. }
  62. }
  63. proc ::latoc::oil_handler {nick uhost hand chan argv} {
  64. if {![channel get $chan latoc]} { return }
  65. set data [::latoc::fetch $chan]
  66. set lines [::latoc::parse $data]
  67. ::latoc::output $chan $lines {Crude Oil}
  68. }
  69. proc ::latoc::gold_handler {nick uhost hand chan argv} {
  70. if {![channel get $chan latoc]} { return }
  71. set data [::latoc::fetch $chan]
  72. set lines [::latoc::parse $data]
  73. ::latoc::output $chan $lines {Gold}
  74. }
  75. proc ::latoc::silver_handler {nick uhost hand chan argv} {
  76. if {![channel get $chan latoc]} { return }
  77. set data [::latoc::fetch $chan]
  78. set lines [::latoc::parse $data]
  79. ::latoc::output $chan $lines {Silver}
  80. }
  81. proc ::latoc::format {name price last direction change percent} {
  82. return "$name: \00310$price [::latoc::colour $direction $change] [::latoc::colour $direction $percent]\003 $last"
  83. }
  84. proc ::latoc::colour {direction value} {
  85. if {[string match "Down" $direction]} {
  86. return \00304$value\017
  87. }
  88. if {[string match "Up" $direction]} {
  89. return \00309$value\017
  90. }
  91. return $value
  92. }
  93. putlog "latoc.tcl loaded"