|
@@ -1,4 +1,8 @@
|
|
|
#
|
|
#
|
|
|
|
|
+# 0.3 - ?
|
|
|
|
|
+# - switch from decode_html to htmlparse::mapEscape
|
|
|
|
|
+# - fix issue with encoding getting ascii
|
|
|
|
|
+#
|
|
|
# 0.2 - May 10 2010
|
|
# 0.2 - May 10 2010
|
|
|
# - fix for garbled utf chars in api queries
|
|
# - fix for garbled utf chars in api queries
|
|
|
# - added +google channel flag to enable
|
|
# - added +google channel flag to enable
|
|
@@ -17,6 +21,7 @@
|
|
|
|
|
|
|
|
package require http
|
|
package require http
|
|
|
package require json
|
|
package require json
|
|
|
|
|
+package require htmlparse
|
|
|
|
|
|
|
|
namespace eval google {
|
|
namespace eval google {
|
|
|
#variable output_cmd "cd::putnow"
|
|
#variable output_cmd "cd::putnow"
|
|
@@ -41,7 +46,6 @@ namespace eval google {
|
|
|
setudef flag google
|
|
setudef flag google
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
proc google::convert_fetch {terms} {
|
|
proc google::convert_fetch {terms} {
|
|
|
http::config -useragent $google::useragent
|
|
http::config -useragent $google::useragent
|
|
|
|
|
|
|
@@ -63,19 +67,17 @@ proc google::convert_fetch {terms} {
|
|
|
return $data
|
|
return $data
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
proc google::convert_parse {html} {
|
|
proc google::convert_parse {html} {
|
|
|
if {![regexp -- $google::convert_regexp $html -> result]} {
|
|
if {![regexp -- $google::convert_regexp $html -> result]} {
|
|
|
error "Parse error or no result"
|
|
error "Parse error or no result"
|
|
|
}
|
|
}
|
|
|
- set result [google::decode_html $result]
|
|
|
|
|
|
|
+ set result [htmlparse::mapEscapes $result]
|
|
|
# change <sup>num</sup> to ^num (exponent)
|
|
# change <sup>num</sup> to ^num (exponent)
|
|
|
set result [regsub -all -- {<sup>(.*?)</sup>} $result {^\1}]
|
|
set result [regsub -all -- {<sup>(.*?)</sup>} $result {^\1}]
|
|
|
# strip rest of html code
|
|
# strip rest of html code
|
|
|
return [regsub -all -- {<.*?>} $result ""]
|
|
return [regsub -all -- {<.*?>} $result ""]
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
# Query normal html for conversions
|
|
# Query normal html for conversions
|
|
|
proc google::convert {nick uhost hand chan argv} {
|
|
proc google::convert {nick uhost hand chan argv} {
|
|
|
if {![channel get $chan google]} { return }
|
|
if {![channel get $chan google]} { return }
|
|
@@ -98,21 +100,19 @@ proc google::convert {nick uhost hand chan argv} {
|
|
|
$google::output_cmd "PRIVMSG $chan :\002$result\002"
|
|
$google::output_cmd "PRIVMSG $chan :\002$result\002"
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
# Output for results from api query
|
|
# Output for results from api query
|
|
|
proc google::output {chan url title content} {
|
|
proc google::output {chan url title content} {
|
|
|
regsub -all -- {(?:<b>|</b>)} $title "\002" title
|
|
regsub -all -- {(?:<b>|</b>)} $title "\002" title
|
|
|
set output "$title @ $url"
|
|
set output "$title @ $url"
|
|
|
- $google::output_cmd "PRIVMSG $chan :[google::decode_html $output]"
|
|
|
|
|
|
|
+ $google::output_cmd "PRIVMSG $chan :[htmlparse::mapEscapes $output]"
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
# Return results from API query of $url
|
|
# Return results from API query of $url
|
|
|
proc google::api_fetch {terms url} {
|
|
proc google::api_fetch {terms url} {
|
|
|
set query [http::formatQuery v "1.0" q $terms safe off]
|
|
set query [http::formatQuery v "1.0" q $terms safe off]
|
|
|
set headers [list Referer $google::api_referer]
|
|
set headers [list Referer $google::api_referer]
|
|
|
|
|
|
|
|
- set token [http::geturl ${url}?${query} -headers $headers -method GET -binary 1]
|
|
|
|
|
|
|
+ set token [http::geturl ${url}?${query} -headers $headers -method GET]
|
|
|
set data [http::data $token]
|
|
set data [http::data $token]
|
|
|
set ncode [http::ncode $token]
|
|
set ncode [http::ncode $token]
|
|
|
http::cleanup $token
|
|
http::cleanup $token
|
|
@@ -130,7 +130,6 @@ proc google::api_fetch {terms url} {
|
|
|
return [json::json2dict $data]
|
|
return [json::json2dict $data]
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
# Validate input and then return list of results
|
|
# Validate input and then return list of results
|
|
|
proc google::api_validate {argv url} {
|
|
proc google::api_validate {argv url} {
|
|
|
if {[string length $argv] == 0} {
|
|
if {[string length $argv] == 0} {
|
|
@@ -151,7 +150,6 @@ proc google::api_validate {argv url} {
|
|
|
return $results
|
|
return $results
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
# Query api
|
|
# Query api
|
|
|
proc google::api_handler {chan argv url} {
|
|
proc google::api_handler {chan argv url} {
|
|
|
if {[catch {google::api_validate $argv $url} results]} {
|
|
if {[catch {google::api_validate $argv $url} results]} {
|
|
@@ -169,7 +167,6 @@ proc google::api_handler {chan argv url} {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
# Regular API search
|
|
# Regular API search
|
|
|
proc google::search {nick uhost hand chan argv} {
|
|
proc google::search {nick uhost hand chan argv} {
|
|
|
if {![channel get $chan google]} { return }
|
|
if {![channel get $chan google]} { return }
|
|
@@ -177,7 +174,6 @@ proc google::search {nick uhost hand chan argv} {
|
|
|
google::api_handler $chan $argv ${google::api_url}web
|
|
google::api_handler $chan $argv ${google::api_url}web
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
# News from API
|
|
# News from API
|
|
|
proc google::news {nick uhost hand chan argv} {
|
|
proc google::news {nick uhost hand chan argv} {
|
|
|
if {![channel get $chan google]} { return }
|
|
if {![channel get $chan google]} { return }
|
|
@@ -185,47 +181,9 @@ proc google::news {nick uhost hand chan argv} {
|
|
|
google::api_handler $chan $argv ${google::api_url}news
|
|
google::api_handler $chan $argv ${google::api_url}news
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
# Images from API
|
|
# Images from API
|
|
|
proc google::images {nick uhost hand chan argv} {
|
|
proc google::images {nick uhost hand chan argv} {
|
|
|
if {![channel get $chan google]} { return }
|
|
if {![channel get $chan google]} { return }
|
|
|
|
|
|
|
|
google::api_handler $chan $argv ${google::api_url}images
|
|
google::api_handler $chan $argv ${google::api_url}images
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-# From perpleXa's urbandictionary script
|
|
|
|
|
-# Replaces html special chars with their hex equivalent
|
|
|
|
|
-proc google::decode_html {content} {
|
|
|
|
|
- if {![string match *&* $content]} {
|
|
|
|
|
- return $content;
|
|
|
|
|
- }
|
|
|
|
|
- set escapes {
|
|
|
|
|
- \x20 " \x22 & \x26 ' \x27 – \x2D
|
|
|
|
|
- < \x3C > \x3E ˜ \x7E € \x80 ¡ \xA1
|
|
|
|
|
- ¢ \xA2 £ \xA3 ¤ \xA4 ¥ \xA5 ¦ \xA6
|
|
|
|
|
- § \xA7 ¨ \xA8 © \xA9 ª \xAA « \xAB
|
|
|
|
|
- ¬ \xAC ­ \xAD ® \xAE &hibar; \xAF ° \xB0
|
|
|
|
|
- ± \xB1 ² \xB2 ³ \xB3 ´ \xB4 µ \xB5
|
|
|
|
|
- ¶ \xB6 · \xB7 ¸ \xB8 ¹ \xB9 º \xBA
|
|
|
|
|
- » \xBB ¼ \xBC ½ \xBD ¾ \xBE ¿ \xBF
|
|
|
|
|
- À \xC0 Á \xC1 Â \xC2 Ã \xC3 Ä \xC4
|
|
|
|
|
- Å \xC5 Æ \xC6 Ç \xC7 È \xC8 É \xC9
|
|
|
|
|
- Ê \xCA Ë \xCB Ì \xCC Í \xCD Î \xCE
|
|
|
|
|
- Ï \xCF Ð \xD0 Ñ \xD1 Ò \xD2 Ó \xD3
|
|
|
|
|
- Ô \xD4 Õ \xD5 Ö \xD6 × \xD7 Ø \xD8
|
|
|
|
|
- Ù \xD9 Ú \xDA Û \xDB Ü \xDC Ý \xDD
|
|
|
|
|
- Þ \xDE ß \xDF à \xE0 á \xE1 â \xE2
|
|
|
|
|
- ã \xE3 ä \xE4 å \xE5 æ \xE6 ç \xE7
|
|
|
|
|
- è \xE8 é \xE9 ê \xEA ë \xEB ì \xEC
|
|
|
|
|
- í \xED î \xEE ï \xEF ð \xF0 ñ \xF1
|
|
|
|
|
- ò \xF2 ó \xF3 ô \xF4 õ \xF5 ö \xF6
|
|
|
|
|
- ÷ \xF7 ø \xF8 ù \xF9 ú \xFA û \xFB
|
|
|
|
|
- ü \xFC ý \xFD þ \xFE ÿ \xFF
|
|
|
|
|
- };
|
|
|
|
|
- set content [string map $escapes $content];
|
|
|
|
|
- set content [string map [list "\]" "\\\]" "\[" "\\\[" "\$" "\\\$" "\\" "\\\\"] $content];
|
|
|
|
|
- regsub -all -- {&#([[:digit:]]{1,5});} $content {[format %c [string trimleft "\1" "0"]]} content;
|
|
|
|
|
- regsub -all -- {&#x([[:xdigit:]]{1,4});} $content {[format %c [scan "\1" %x]]} content;
|
|
|
|
|
- regsub -all -- {&#?[[:alnum:]]{2,7};} $content "?" content;
|
|
|
|
|
- return [subst $content];
|
|
|
|
|
-}
|
|
|