Skip to content

Instantly share code, notes, and snippets.

@mugijiru
Created June 4, 2011 10:15
Show Gist options
  • Save mugijiru/1007783 to your computer and use it in GitHub Desktop.
Save mugijiru/1007783 to your computer and use it in GitHub Desktop.
image-clock.el
;; 拾って来るサーバの指定など
(defvar image-clock-root-url "http://2ji-tokei.com/")
(defvar image-clock-server-image-dir-path "/tokei/")
(defvar image-clock-server-file-name-format "%H/%M.jpg")
;; 保存するディレクトリや名前
(defvar image-clock-tmp-image-dir "/tmp")
(defvar image-clock-local-file-name-format "%H%M.jpg")
(defvar image-clock-current-image nil)
(defvar image-clock-buffer-name "*ImageClock*")
(defvar image-clock-get-image-timer nil)
(defvar image-clock-view-image-timer nil)
(defun image-clock-image-full-path (file-name)
(expand-file-name (concat image-clock-tmp-image-dir "/" file-name)))
(defun image-clock-local-file-name (time)
"画像の名前を生成する関数"
(format-time-string image-clock-local-file-name-format time))
(defun image-clock-server-file-name (time)
"画像の名前を生成する関数"
(format-time-string image-clock-server-file-name-format time))
(defun image-clock-image-url (time)
"画像のURLを生成する関数"
(concat image-clock-root-url
"/"
image-clock-server-image-dir-path
"/"
(image-clock-server-file-name time)))
(defun image-clock-get-image (&optional time)
"指定したサーバから画像を非同期で拾って来る"
(let ((time (or time (current-time)))
(command (concat "wget -q -b --referer=" image-clock-root-url " "
(image-clock-image-url time) " "
"-O " (concat image-clock-tmp-image-dir "/" (image-clock-local-file-name time)))))
(shell-command command)))
; (setq time (or time (current-time)))
; (setq command )
(defun image-clock-get-image-sync (&optional time)
"指定したサーバから画像を拾って来る。非同期じゃないよ"
(setq time (or time (current-time)))
(call-process "wget" nil "*Messages*" t
"-q"
(concat "--referer=" image-clock-root-url)
(image-clock-image-url time)
"-O" (concat image-clock-tmp-image-dir "/" (image-clock-local-file-name time))))
(defun image-clock-get-image-start-timer ()
"画像を拾うアクションを1分毎に実行するよ"
(run-at-time "0 sec" 60 'image-clock-get-image (time-add (current-time) (seconds-to-time 60))))
(defun image-clock-view-image-start-timer ()
"画像を表示するアクションを1秒毎に実行するよ"
(run-at-time "0 sec" 1 'image-clock-view-image))
(defun image-clock-stop-timer
"タイマー止めちゃうよ。動作確認してないよ"
(interactive)
(cancel-timer image-clock-get-image-timer)
(cancel-timer image-clock-view-image-timer))
(defun image-clock-view-image ()
"バッファに拾ってきた画像を表示するよ"
(setq time (current-time))
(setq file-name (image-clock-local-file-name time))
(setq image-full-path (image-clock-image-full-path file-name))
(unless (file-exists-p image-full-path)
(image-clock-get-image-sync time))
(unless (eq image-clock-current-image file-name)
(with-current-buffer image-clock-buffer-name
(save-excursion
(setq buffer-read-only nil)
(erase-buffer)
(insert-image (create-image image-full-path))
(setq image-clock-current-image file-name)
(setq buffer-read-only t) ))))
(defun image-clock-mode ()
"なんちゃら時計をemacs内に表示するモード"
(interactive)
(generate-new-buffer image-clock-buffer-name)
(switch-to-buffer image-clock-buffer-name)
(setq buffer-read-only t)
(buffer-disable-undo)
(setq major-mode 'image-clock)
(setq mode-name "image-clock")
(setq image-clock-get-image-timer (image-clock-get-image-start-timer))
(setq image-clock-get-image-timer (image-clock-view-image-start-timer)))
(provide 'image-clock)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment