[Date Prev][Date Next][Thread Prev][][Date Index][Thread Index]

(non-)hashing w3m-antenna?



Hi,

I just considered w3m-antenna as a replacement for some five-year-old
cronjobs, but I really dislike how it only considers the size of a
document when a Last-Modified header isn't available.

I figured one could easily turn the "size" field of w3m-antenna-alist
into a more generic "hash" field, and treat buffer-size as a
degenerated hash function.

Is that a sound idea?  Attached is a proof-of-concept patch.

regards,
andreas
Index: w3m-antenna.el
===================================================================
RCS file: /storage/cvsroot/emacs-w3m/w3m-antenna.el,v
retrieving revision 1.57
diff -u -r1.57 w3m-antenna.el
--- w3m-antenna.el	22 Dec 2005 13:52:18 -0000	1.57
+++ w3m-antenna.el	2 Oct 2006 06:23:18 -0000
@@ -103,8 +103,8 @@
  2. Class (Normal, HNS or TIME).
  3. Real URL.
  4. Last modification time.
- 5. Size in bytes.
- 6. Time when size modification is detected.
+ 5. Hash value.
+ 6. Time when hash value change was detected.
 ")
 
 (defmacro w3m-antenna-site-key (site)
@@ -117,9 +117,9 @@
   (` (nth 3 (, site))))
 (defmacro w3m-antenna-site-last-modified (site)
   (` (nth 4 (, site))))
-(defmacro w3m-antenna-site-size (site)
+(defmacro w3m-antenna-site-hash (site)
   (` (nth 5 (, site))))
-(defmacro w3m-antenna-site-size-detected (site)
+(defmacro w3m-antenna-site-hash-detected (site)
   (` (nth 6 (, site))))
 
 (defcustom w3m-antenna-file
@@ -155,7 +155,8 @@
 			       :value-from w3m-antenna-tmp-title)
 	   (choice
 	    :tag "Procedure"
-	    (const :tag "Check either its last modified time or its size" nil)
+	    (const :tag "Check either its last modified time, its size or its hash value" nil)
+	    (const :tag "Check either its last modified time or its hash value" size-is-evil)
 	    (const :tag "Check its last modified time only" time)
 	    (const :tag "Check its current date provided by Hyper Nikki System"
 		   hns)
@@ -226,6 +227,21 @@
 	  (function-item :tag "Do nothing." identity)
 	  (function :format "User function: %v\n" :size 0)))
 
+(defcustom w3m-antenna-hash-function
+  (cond ((functionp 'md5) 'md5)
+	((functionp 'sha1) 'sha1)
+	(t 'buffer-size))
+  "Function used to compute hash value of a site.
+The data to compute the hash on is present in the current buffer,
+which is also passed as an argument."
+  :group 'w3m-antenna
+  :type '(choice
+	  :format "%{%t%}:\n %[Value Menu%] %v"
+	  (function-item :tag "md5" md5)
+	  (function-item :tag "sha1" sha1)
+	  (function-item :tag "Buffer size" buffer-size)
+	  (function :format "User function: %v\n" :size 0)))
+
 (defun w3m-antenna-alist ()
   (let ((alist (w3m-load-list w3m-antenna-file)))
     (mapcar (lambda (site)
@@ -350,7 +366,7 @@
 It consists of 3 steps:
 \(1\) Check the time when the SITE was last modified with HEAD request.
 \(2\) Check the size of the SITE with HEAD request.
-\(3\) Get the real content of the SITE, and check its size.
+\(3\) Get the real content of the SITE, and check its hash value.
 "
   (lexical-let ((site site)
 		(url (or url
@@ -362,9 +378,11 @@
 	(if (nth 4 attr)	; Use the value of Last-modified header.
 	    (w3m-antenna-site-update site url (nth 4 attr) (nth 2 attr))
 	  (unless (eq 'time (w3m-antenna-site-class site))
-	    (if (nth 2 attr)	; Use the value of Content-Length header.
+	    (if (and (not (eq 'size-is-evil (w3m-antenna-site-class site)))
+		     (nth 2 attr))	; Use the value of Content-Length header.
 		(w3m-antenna-site-update site url nil (nth 2 attr))
-	      ;; Get the real content of the SITE, and calculate its size.
+	      ;; Get the real content of the SITE, and calculate its
+	      ;; hash value.
 	      (w3m-process-do-with-temp-buffer
 		  (type (w3m-retrieve url nil t nil nil handler))
 		(when type
@@ -372,24 +390,26 @@
 		  (w3m-remove-comments)
 		  (when w3m-use-filter
 		    (w3m-filter url))
-		  (w3m-antenna-site-update site url nil (buffer-size)))))))))))
-
-(defun w3m-antenna-site-update (site url time size)
-  "Update SITE's status information with specified TIME and SIZE."
-  ;; (w3m-antenna-site-size-detected site) keeps the time when SITE's
-  ;; size attribute is checked.
-  (setf (w3m-antenna-site-size-detected site)
-	(when size
+		  (w3m-antenna-site-update
+		   site url nil
+		   (funcall w3m-antenna-hash-function (current-buffer))))))))))))
+
+(defun w3m-antenna-site-update (site url time hash)
+  "Update SITE's status information with specified TIME and HASH."
+  ;; (w3m-antenna-site-hash-detected site) keeps the time when SITE's
+  ;; hash attribute is checked.
+  (setf (w3m-antenna-site-hash-detected site)
+	(when hash
 	  (or (when (and url
 			 (w3m-antenna-site-url site)
 			 (string= url (w3m-antenna-site-url site))
-			 (w3m-antenna-site-size site)
-			 (= size (w3m-antenna-site-size site)))
-		(w3m-antenna-site-size-detected site))
+			 (w3m-antenna-site-hash site)
+			 (equal hash (w3m-antenna-site-hash site)))
+		(w3m-antenna-site-hash-detected site))
 	      (current-time))))
   (setf (w3m-antenna-site-url site) url)
   (setf (w3m-antenna-site-last-modified site) time)
-  (setf (w3m-antenna-site-size site) size)
+  (setf (w3m-antenna-site-hash site) hash)
   site)
 
 (defun w3m-antenna-check-site (site handler)
@@ -466,19 +486,20 @@
 	  (cond
 	   ((w3m-antenna-site-last-modified site)
 	    (current-time-string (w3m-antenna-site-last-modified site)))
-	   ((w3m-antenna-site-size site) "Size")
+	   ((numberp (w3m-antenna-site-hash site)) "Size")
+	   ((w3m-antenna-site-hash site) "Hash")
 	   (t ""))))
 
 (defun w3m-antenna-make-summary-like-natsumican (site)
   (let ((t1 (w3m-antenna-site-last-modified site))
-	(t2 (w3m-antenna-site-size-detected site)))
+	(t2 (w3m-antenna-site-hash-detected site)))
     (format "<li>%20s&nbsp;&nbsp;(%s)&nbsp;&nbsp;<a href=\"%s\">%s</a>"
 	    (if (or t1 t2)
 		(format-time-string "%Y/%m/%d %R" (or t1 t2))
 	      "----/--/-- --:--")
 	    (cond
 	     (t1 "T")
-	     (t2 "S")
+	     (t2 (if (numberp t2) "S" "H"))
 	     (t "?"))
 	    (or (w3m-antenna-site-url site)
 		(w3m-antenna-site-key site))
@@ -489,9 +510,9 @@
 	(lambda (a b)
 	  (w3m-time-newer-p
 	   (or (w3m-antenna-site-last-modified a)
-	       (w3m-antenna-site-size-detected a))
+	       (w3m-antenna-site-hash-detected a))
 	   (or (w3m-antenna-site-last-modified b)
-	       (w3m-antenna-site-size-detected b))))))
+	       (w3m-antenna-site-hash-detected b))))))
 
 (defun w3m-antenna-sort-sites-by-title (sites)
   (sort sites
@@ -543,7 +564,7 @@
     (let (changed unchanged)
       (dolist (site alist)
 	(if (w3m-time-newer-p (or (w3m-antenna-site-last-modified site)
-				  (w3m-antenna-site-size-detected site))
+				  (w3m-antenna-site-hash-detected site))
 			      (or (w3m-arrived-last-modified
 				   (w3m-antenna-site-url site))
 				  (w3m-arrived-time
@@ -571,7 +592,8 @@
 With prefix, ask new url to add instead of current page."
   (interactive "P")
   (w3m-antenna-add (if arg (w3m-input-url) w3m-current-url)
-		   (w3m-encode-specials-string w3m-current-title)))
+		   (and w3m-current-title
+			(w3m-encode-specials-string (w3m-current-title)))))
 
 (defun w3m-antenna-add (url &optional title)
   "Add URL to antenna.