Преглед изворни кода

- fix issue with ascii encoding
- switch from decode_html to htmlparse::mapEscapes

horgh пре 16 година
родитељ
комит
77abe50a90
1 измењених фајлова са 8 додато и 50 уклоњено
  1. 8 50
      google.tcl

+ 8 - 50
google.tcl

@@ -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 {
-		&nbsp; \x20 &quot; \x22 &amp; \x26 &apos; \x27 &ndash; \x2D
-		&lt; \x3C &gt; \x3E &tilde; \x7E &euro; \x80 &iexcl; \xA1
-		&cent; \xA2 &pound; \xA3 &curren; \xA4 &yen; \xA5 &brvbar; \xA6
-		&sect; \xA7 &uml; \xA8 &copy; \xA9 &ordf; \xAA &laquo; \xAB
-		&not; \xAC &shy; \xAD &reg; \xAE &hibar; \xAF &deg; \xB0
-		&plusmn; \xB1 &sup2; \xB2 &sup3; \xB3 &acute; \xB4 &micro; \xB5
-		&para; \xB6 &middot; \xB7 &cedil; \xB8 &sup1; \xB9 &ordm; \xBA
-		&raquo; \xBB &frac14; \xBC &frac12; \xBD &frac34; \xBE &iquest; \xBF
-		&Agrave; \xC0 &Aacute; \xC1 &Acirc; \xC2 &Atilde; \xC3 &Auml; \xC4
-		&Aring; \xC5 &AElig; \xC6 &Ccedil; \xC7 &Egrave; \xC8 &Eacute; \xC9
-		&Ecirc; \xCA &Euml; \xCB &Igrave; \xCC &Iacute; \xCD &Icirc; \xCE
-		&Iuml; \xCF &ETH; \xD0 &Ntilde; \xD1 &Ograve; \xD2 &Oacute; \xD3
-		&Ocirc; \xD4 &Otilde; \xD5 &Ouml; \xD6 &times; \xD7 &Oslash; \xD8
-		&Ugrave; \xD9 &Uacute; \xDA &Ucirc; \xDB &Uuml; \xDC &Yacute; \xDD
-		&THORN; \xDE &szlig; \xDF &agrave; \xE0 &aacute; \xE1 &acirc; \xE2
-		&atilde; \xE3 &auml; \xE4 &aring; \xE5 &aelig; \xE6 &ccedil; \xE7
-		&egrave; \xE8 &eacute; \xE9 &ecirc; \xEA &euml; \xEB &igrave; \xEC
-		&iacute; \xED &icirc; \xEE &iuml; \xEF &eth; \xF0 &ntilde; \xF1
-		&ograve; \xF2 &oacute; \xF3 &ocirc; \xF4 &otilde; \xF5 &ouml; \xF6
-		&divide; \xF7 &oslash; \xF8 &ugrave; \xF9 &uacute; \xFA &ucirc; \xFB
-		&uuml; \xFC &yacute; \xFD &thorn; \xFE &yuml; \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];
-}