google.tcl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #
  2. # 0.3 - ?
  3. # - switch from decode_html to htmlparse::mapEscape
  4. # - fix issue with encoding getting ascii
  5. #
  6. # 0.2 - May 10 2010
  7. # - fix for garbled utf chars in api queries
  8. # - added +google channel flag to enable
  9. # - strip html from !convert as some formatting may be present
  10. # - fix decode_html to convert html utf to hex
  11. # - convert <sup></sup> to exponent
  12. #
  13. # 0.1 - Some time in April 2010
  14. # - Initial release
  15. #
  16. # Created Feb 28 2010
  17. #
  18. # Requires Tcl 8.5+
  19. # Requires tcllib for json
  20. #
  21. package require http
  22. package require json
  23. package require htmlparse
  24. namespace eval google {
  25. #variable output_cmd "cd::putnow"
  26. variable output_cmd "putserv"
  27. # Not enforced for API queries
  28. variable useragent "Lynx/2.8.8dev.2 libwww-FM/2.14 SSL-MM/1.4.1"
  29. variable convert_url "http://www.google.ca/search"
  30. variable convert_regexp {<table class=std>.*?<b>(.*?)</b>.*?</table>}
  31. variable api_url "http://ajax.googleapis.com/ajax/services/search/"
  32. variable api_referer "http://www.egghelp.org"
  33. bind pub -|- "!g" google::search
  34. bind pub -|- "!google" google::search
  35. bind pub -|- "!news" google::news
  36. bind pub -|- "!images" google::images
  37. bind pub -|- "!convert" google::convert
  38. setudef flag google
  39. }
  40. proc google::convert_fetch {terms} {
  41. http::config -useragent $google::useragent
  42. set query [http::formatQuery q $terms]
  43. set token [http::geturl ${google::convert_url}?${query}]
  44. set data [http::data $token]
  45. set ncode [http::ncode $token]
  46. http::cleanup $token
  47. # debug
  48. #set fid [open "g-debug.txt" w]
  49. #puts $fid $data
  50. #close $fid
  51. if {$ncode != 200} {
  52. error "HTTP query failed: $ncode"
  53. }
  54. return $data
  55. }
  56. proc google::convert_parse {html} {
  57. if {![regexp -- $google::convert_regexp $html -> result]} {
  58. error "Parse error or no result"
  59. }
  60. set result [htmlparse::mapEscapes $result]
  61. # change <sup>num</sup> to ^num (exponent)
  62. set result [regsub -all -- {<sup>(.*?)</sup>} $result {^\1}]
  63. # strip rest of html code
  64. return [regsub -all -- {<.*?>} $result ""]
  65. }
  66. # Query normal html for conversions
  67. proc google::convert {nick uhost hand chan argv} {
  68. if {![channel get $chan google]} { return }
  69. if {[string length $argv] == 0} {
  70. $google::output_cmd "PRIVMSG $chan :Please provide a query."
  71. return
  72. }
  73. if {[catch {google::convert_fetch $argv} data]} {
  74. $google::output_cmd "PRIVMSG $chan :Error fetching results: $data."
  75. return
  76. }
  77. if {[catch {google::convert_parse $data} result]} {
  78. $google::output_cmd "PRIVMSG $chan :Error: $result."
  79. return
  80. }
  81. $google::output_cmd "PRIVMSG $chan :\002$result\002"
  82. }
  83. # Output for results from api query
  84. proc google::output {chan url title content} {
  85. regsub -all -- {(?:<b>|</b>)} $title "\002" title
  86. set output "$title @ $url"
  87. $google::output_cmd "PRIVMSG $chan :[htmlparse::mapEscapes $output]"
  88. }
  89. # Return results from API query of $url
  90. proc google::api_fetch {terms url} {
  91. set query [http::formatQuery v "1.0" q $terms safe off]
  92. set headers [list Referer $google::api_referer]
  93. set token [http::geturl ${url}?${query} -headers $headers -method GET]
  94. set data [http::data $token]
  95. set ncode [http::ncode $token]
  96. http::cleanup $token
  97. # debug
  98. #set fid [open "g-debug.txt" w]
  99. #fconfigure $fid -translation binary -encoding binary
  100. #puts $fid $data
  101. #close $fid
  102. if {$ncode != 200} {
  103. error "HTTP query failed: $ncode"
  104. }
  105. return [json::json2dict $data]
  106. }
  107. # Validate input and then return list of results
  108. proc google::api_validate {argv url} {
  109. if {[string length $argv] == 0} {
  110. error "Please supply search terms."
  111. }
  112. if {[catch {google::api_fetch $argv $url} data]} {
  113. error "Error fetching results: $data."
  114. }
  115. set response [dict get $data responseData]
  116. set results [dict get $response results]
  117. if {[llength $results] == 0} {
  118. error "No results."
  119. }
  120. return $results
  121. }
  122. # Query api
  123. proc google::api_handler {chan argv url} {
  124. if {[catch {google::api_validate $argv $url} results]} {
  125. $google::output_cmd "PRIVMSG $chan :$results"
  126. return
  127. }
  128. foreach result $results {
  129. dict with result {
  130. # $language holds lang in news results, doesn't exist in web results
  131. if {![info exists language] || $language == "en"} {
  132. google::output $chan $unescapedUrl $title $content
  133. }
  134. }
  135. }
  136. }
  137. # Regular API search
  138. proc google::search {nick uhost hand chan argv} {
  139. if {![channel get $chan google]} { return }
  140. google::api_handler $chan $argv ${google::api_url}web
  141. }
  142. # News from API
  143. proc google::news {nick uhost hand chan argv} {
  144. if {![channel get $chan google]} { return }
  145. google::api_handler $chan $argv ${google::api_url}news
  146. }
  147. # Images from API
  148. proc google::images {nick uhost hand chan argv} {
  149. if {![channel get $chan google]} { return }
  150. google::api_handler $chan $argv ${google::api_url}images
  151. }