emacs 小黄鸡插件
周末耍了下simsim,发现这只小鸡的对话能力简直超强啊,心想写代码些无聊了有只小鸡陪聊还是不错的,于是这个插件就诞生了,平时主要用emacs,所以就只写了emacs版.
惯例,上图上代码:
;simsim robot (defun http-url-encode (str content-type) "URL encode STR using CONTENT-TYPE as the coding system." (apply 'concat (mapcar (lambda (c) (if (or (and (>= c ?a) (<= c ?z)) (and (>= c ?A) (<= c ?Z)) (and (>= c ?0) (<= c ?9))) (string c) (format "%%%02x" c))) (encode-coding-string str content-type)))) (defun ask-simsim (content &optional lc) (let ((dest (concat "http://www.simsimi.com/func/req?lc=" (or lc "zh") "&msg=" (http-url-encode content 'utf-8) )) ) (get-url-content dest) ) ) (defun get-url-content (url) "simplily get content from url" (let (ret ) (with-current-buffer (url-retrieve-synchronously url) ; (setq status url-http-response-status) (goto-char (point-min)) (if (search-forward-regexp "{" nil t) (setq ret (decode-coding-string (substring (car (split-string (buffer-substring (point) (point-max)) "," )) 12 -1) 'utf-8) ) (message "invalid request") ) ) ret ) ) (defun sim-eof () (= (count-lines (point-min) (point-max)) (count-lines (point-min) (point)) ) ) (defun talk-to-sim (&optional arg) (interactive) ;(save-excursion (if (sim-eof) (if (re-search-backward "me:\\(.+\\)" nil t) ((lambda (msg) "insert msg to content" (goto-char (point-max)) (insert (concat "\n\n" "simsim:" msg "\n\n" )) (insert "me:") ) (ask-simsim (match-string-no-properties 1)) ) ) (message "unknow error") ) ; ) ) (defun simsim-start () (let ((buffer-name "simsim")) (if (get-buffer buffer-name) (pop-to-buffer buffer-name) (pop-to-buffer (get-buffer-create buffer-name)) (insert "simsim:主人好! \n\n") (insert "me:") ) ) ) (defvar simsim-mode-hook nil "hook for simsim") (defvar simsim-mod-map nil "key map") (if simsim-mod-map () (setq simsim-mod-map (make-sparse-keymap)) (define-key simsim-mod-map [return] 'talk-to-sim) ) (defun simsim () "simsim bot mod.... just for fun" (interactive) (simsim-start);//switch to simsim buffer (kill-all-local-variables) (use-local-map simsim-mod-map) ; (setq major-mode 'simsim-mod ; mode-name "simsim" ; ) ) (provide 'simsim)
### 补
Orz,官方竟然发现了....下面是解决办法,referr和ua换了 ps,emacs的ua还
(defun get-url-content (url) "simplily get content from url" (let ((ret nil) ; (url-proxy-services '(("http" . "localhost:8888"))) (url-request-extra-headers '(("Content-Type" . "application/json; charset=utf-8") ("X-Requested-With" . "XMLHttpRequest") ("User-Agent" . " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.75 Safari/537.1") ("Referer" . "http://www.simsimi.com/talk.htm") ) )) (with-current-buffer (url-retrieve-synchronously url) ; (setq status url-http-response-status) (goto-char (point-min)) (if (search-forward-regexp "{" nil t) (setq ret (decode-coding-string (substring (car (split-string (buffer-substring (point) (point-max)) "," )) 12 -1) 'utf-8) ) ; (message (buffer-substring (point-min) (point-max))) (message "invalid request") ) ) ret ) )
###补
官方采用了api形式对请求做了限制,请访问http://developer.simsimi.com/获取授权