blog/content/post/2009/11/18/2009-11-18-00001274.md

12 KiB
Raw Blame History

title author date url wordtwit_post_info categories
検索した店舗情報をデーターベースに登録するアクションを作る kazu634 2009-11-18 /2009/11/18/_1398/
O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4935;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}
Emacs

上記のelispを改良し、取得した店舗情報をデーターベースに登録するアクションを追加します。

説明には次のようにあります:

action (type 要素が指定されなかった場合、必須)
action 要素は「(表示文字列 . 関数)ペアー」のリストである。指定された関数は選択された候補を引数として呼び出される。
現在選択されている候補のためのデフォルト以外のアクションがこのリストから選ばれる(デフォルトでは TAB キーで)。 DISPLAY 文字列は、補完バッファーで表示され、アクションが選択されたときに関数が呼び出される。リスト中の先頭ののアクションがデフォルト・アクションになる。

http://d.hatena.ne.jp/sirocco634/searchdiary?word=anything&.submit=%B8%A1%BA%F7&type=detail:title

二つ目のアクションとしてデーターベースに登録する関数を割り当てます。データーベースは mysql を使うことにします。コマンドののたたき方は「2009-11-11 武蔵の日記」を参考にします。

mysql の設定

このようなテーブルを定義しました:

mysql> desc lunch;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| shopname | varchar(100) | NO   |     | NULL    |                |
| address  | varchar(150) | NO   |     | NULL    |                |
| tel      | varchar(20)  | NO   |     | NULL    |                |
| url      | varchar(100) | YES  |     | NULL    |                |
| comment  | varchar(500) | YES  |     | NULL    |                |
| lat      | float(10,7)  | YES  |     | NULL    |                |
| lng      | float(10,7)  | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
8 rows in set (.00 sec)

ソース

anything-c-source-gourmetを次のように変更します:

(defvar anything-c-source-gourmet
'((name . "Cross-Gourmet Site Search")
(candidates . (lambda ()
(delete ""
(split-string
(shell-command-to-string
(format "gourmet %s"
anything-c-source-gourmet-work))
"\n"))))
(candidate-transformer . (lambda (candidates)
(mapcar
(function (lambda(arg)
(let*
((str_list (split-string arg "\t"))
(site (nth 0 str_list))
(shopname (nth 1 str_list)))
(cons (format "[%s] %s" site shopname) arg))))
candidates)))
(action . (("Insert" . insert_gourmet)
("Database" . insert_gourmet_database)))
(migemo)))

また、関数 insert_gourmet_database を次のように定義します:

(defun insert_gourmet_database (candidate)
(let*
((str-list (split-string candidate "\t"))
(site (nth  str-list))
(shopname (nth 1 str-list))
(address (nth 2 str-list))
(url (nth 3 str-list))
(tel (nth 4 str-list))
(comments (read-string "Comments, if you have any: " ""  nil "")))
(call-process-shell-command
(format
"echo 'INSERT INTO lunch (shopname, address, tel, url, comment) values (\"%s\", \"%s\", \"%s\", \"%s\", \"%s\");' | mysql5 -uusr -ppassword -h srv634 shop"
shopname address tel url comments))
(message "Inserting DB finished.")))

私は店舗情報を登録するときにコメント用のフィールドにコメントを残したかったので、コメントの入力を促すようにしました。

「anything」に関連する最近のエントリ