From: bg66 Date: Thu, 3 Feb 2005 15:08:46 +0000 (+0000) Subject: * riece-google.el: New add-on. X-Git-Tag: riece-1_0_7~58 X-Git-Url: http://git.chise.org/gitweb/?p=elisp%2Friece.git;a=commitdiff_plain;h=cf6bb0b92f75973dcf661b9884a5a7033d9e91ed * riece-google.el: New add-on. * COMPILE (riece-modules): Add riece-google. * Makefile.am (EXTRA_DIST): Add riece-google.el. --- diff --git a/doc/riece-ja.texi b/doc/riece-ja.texi index 790788a..18fa8d4 100644 --- a/doc/riece-ja.texi +++ b/doc/riece-ja.texi @@ -564,6 +564,8 @@ PING に応答する local proxy 経由で IRC サーバに接続 ツールバーを表示 @item riece-eval 入力された文字列を lisp オブジェクトとして評価 +@item riece-google +キーワードを Google で検索 @end table これらのうち、@samp{riece-highlight} と @samp{riece-ctcp}、 diff --git a/lisp/COMPILE b/lisp/COMPILE index 82d3438..caab210 100644 --- a/lisp/COMPILE +++ b/lisp/COMPILE @@ -74,7 +74,8 @@ riece-kakasi riece-yank riece-toolbar - riece-eval)))) + riece-eval + riece-google)))) (defvar riece-icons '(riece-command-previous-channel diff --git a/lisp/ChangeLog b/lisp/ChangeLog index eed70d2..f029570 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,9 @@ +2005-02-03 OHASHI Akira + + * riece-google.el: New add-on. + * COMPILE (riece-modules): Add riece-google. + * Makefile.am (EXTRA_DIST): Add riece-google.el. + 2005-02-03 Daiki Ueno * riece-400.el (riece-handle-475-message): New handler; query diff --git a/lisp/Makefile.am b/lisp/Makefile.am index 3116515..8f0310e 100644 --- a/lisp/Makefile.am +++ b/lisp/Makefile.am @@ -15,7 +15,7 @@ EXTRA_DIST = COMPILE ChangeLog ChangeLog.Liece \ riece-menu.el riece-icon.el riece-async.el riece-lsdb.el \ riece-xface.el riece-ctlseq.el riece-ignore.el riece-hangman.el \ riece-biff.el riece-kakasi.el riece-foolproof.el riece-yank.el \ - riece-toolbar.el riece-eval.el url-riece.el + riece-toolbar.el riece-eval.el riece-google.el url-riece.el CLEANFILES = auto-autoloads.el custom-load.el *.elc FLAGS ?= -batch -q -no-site-file diff --git a/lisp/riece-google.el b/lisp/riece-google.el new file mode 100644 index 0000000..b149774 --- /dev/null +++ b/lisp/riece-google.el @@ -0,0 +1,244 @@ +;;; riece-google.el --- Interface to Google API +;; Copyright (C) 2005 OHASHI Akira + +;; Author: OHASHI Akira +;; SASADA Koichi +;; Keywords: IRC, riece + +;; This file is part of Riece. + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 2, or (at your option) +;; any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to the +;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, +;; Boston, MA 02111-1307, USA. + +;;; Commentary: + +;; This add-on searches keywords by Google. + +;; Ruby code was stolen (and modified) from nadoka. + +;; To use, add the following line to your ~/.riece/init.el: +;; (add-to-list 'riece-addons 'riece-google) + +;;; Code: + +(require 'riece-message) + +(defgroup riece-google nil + "Interface to Google API." + :prefix "riece-" + :group 'riece) + +(defcustom riece-google-ruby-command "ruby" + "Command name for Ruby interpreter." + :type 'string + :group 'riece-google) + +(defcustom riece-google-program + '("\ +# Copyright (c) 2004 SASADA Koichi +# +# This program is free software with ABSOLUTELY NO WARRANTY. +# You can re-distribute and/or modify this program under +# the same terms of the Ruby's lisence. + +require 'soap/wsdlDriver' +require 'iconv' +require 'kconv' +require 'cgi' + +keywords = '" keywords "' +max_results = " max-results " +license_key = '" license-key "' +default_lang = '" lang "' +google_wsdl = 'http://api.google.com/GoogleSearch.wsdl' +google = SOAP::WSDLDriverFactory.new(google_wsdl).create_driver + +def erace_tag str + CGI.unescapeHTML(str.gsub(/\\<.+?\\>/, '')) +end + +def lang_check lang + if lang.empty? || /^lang_/ =~ lang + lang + else + 'lang_' + lang + end +end + +def show_char_code_and_erace_tag str + case $KCODE + when 'EUC', 'SJIS' + CGI.unescapeHTML(str.gsub(/\\<.+?\\>/, '')).tojis + when 'NONE', 'UTF-8' + begin + str = Iconv.conv(\"EUC-JP\", \"UTF-8\", str) + CGI.unescapeHTML(str.gsub(/\\<.+?\\>/, '')).tojis + rescue => e + \"(char code problem: #{e.class}[#{e.message.dump}])\" + end + else + str + end +end + +def search_char_code str + case $KCODE + when 'EUC', 'SJIS' + str.toeuc + when 'NONE' + begin + Iconv.conv(\"UTF-8\", \"EUC-JP\", str.toeuc) + rescue => e + \"(char code problem: #{e.class})\" + end + when 'UTF-8' + str + else + raise + end +end + +begin + lang = lang_check(default_lang) + word = search_char_code(keywords) + result = google.doGoogleSearch( + license_key, word, 0, max_results, false, \"\", + false, lang, 'utf-8', 'utf-8' + ) + + count = result.estimatedTotalResultsCount + if count > 0 + word = show_char_code_and_erace_tag(keywords) + count = count.to_s.gsub(/(\\d)(?=\\d{3}+$)/, '\\\\1,') + time = result.searchTime.to_s + print \"Search results for #{word} (Hits: #{count}: Time: #{time}):\\n\" + + result.resultElements.each_with_index{|e, i| + title = show_char_code_and_erace_tag(e.title) + url = e.URL + print \"#{title} - #{url}\\n\" + } + else + print \"no match\\n\" + end + +rescue Exception => e + print \"#{e.class}(#{e.message})\" +end +") + "Ruby program for searching by Google." + :type 'list + :group 'riece-google) + +(defcustom riece-google-license-key nil + "*License key for Google API." + :type 'string + :group 'riece-google) + +(defcustom riece-google-default-lang "lang_ja" + "*Default language for search keywords." + :type 'string + :group 'riece-google) + +(defconst riece-google-regexp + "^go\\(o+\\)gle\\(:\\([a-z]+\\)\\)?>\\s-*\\(.*\\)") + +(defvar riece-google-enabled nil) + +(defconst riece-google-description + "Search keywords by Google.") + +(defvar riece-google-target nil) + +(defun riece-google-display-message-function (message) + (when (and riece-google-enabled + (stringp riece-google-license-key) + (string-match riece-google-regexp (riece-message-text message))) + (let ((keywords (match-string 4 (riece-message-text message))) + (max-results (number-to-string + (length + (match-string 1 (riece-message-text message))))) + (lang (or (match-string 3 (riece-message-text message)) + riece-google-default-lang)) + (process-connection-type nil) + selective-display + (coding-system-for-read 'binary) + (coding-system-for-write 'binary) + (process (start-process "Google" (generate-new-buffer " *Google*") + riece-google-ruby-command))) + (setq riece-google-target (riece-message-target message)) + (process-send-string process + (apply #'concat + (riece-google-substitute-variables + (riece-google-substitute-variables + (riece-google-substitute-variables + (riece-google-substitute-variables + riece-google-program + 'keywords keywords) + 'max-results max-results) + 'license-key riece-google-license-key) + 'lang lang))) + (process-send-eof process) + (with-current-buffer (process-buffer process) + (set-buffer-multibyte t) + (erase-buffer) + (set-buffer-modified-p nil)) + (set-process-filter process #'riece-google-filter) + (set-process-sentinel process #'riece-google-sentinel)))) + +(defun riece-google-filter (process output) + (when (buffer-live-p (process-buffer process)) + (with-current-buffer (process-buffer process) + (goto-char (point-max)) + (insert output) + (goto-char (point-min)) + (while (progn (end-of-line) (and (not (eobp)) (eq (char-after) ?\n))) + (if (eq (char-after (1- (point))) ?\r) ; cut off preceding LF + (delete-region (1- (point)) (point))) + (riece-google-send-string riece-google-target + (buffer-substring (point-min) (point))) + (delete-region (point-min) (progn (beginning-of-line 2) (point))))))) + +(defun riece-google-sentinel (process string) + (delete-process process)) + +(defun riece-google-send-string (target message) + (riece-send-string + (format "NOTICE %s :%s\r\n" (riece-identity-prefix target) message)) + (riece-display-message + (riece-make-message (riece-current-nickname) target message 'notice))) + +(defun riece-google-substitute-variables (program variable value) + (setq program (copy-sequence program)) + (let ((pointer program)) + (while pointer + (setq pointer (memq variable program)) + (if pointer + (setcar pointer value))) + program)) + +(defun riece-google-insinuate () + (add-hook 'riece-after-display-message-functions + 'riece-google-display-message-function)) + +(defun riece-google-enable () + (setq riece-google-enabled t)) + +(defun riece-google-disable () + (setq riece-google-enabled nil)) + +(provide 'riece-google) + +;;; riece-google.el ends here