1 ;;; nnweb.el --- retrieving articles via web search engines
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; Note: You need to have `url' and `w3' installed for this
39 (defvoo nnweb-type 'dejanews
40 "What search engine type is being used.")
42 (defvar nnweb-type-definition
44 (article . nnweb-dejanews-wash-article)
45 (map . nnweb-dejanews-create-mapping)
46 (search . nnweb-dejanews-search)
47 (address . "http://search.dejanews.com/dnquery.xp"))
49 (article . nnweb-reference-wash-article)
50 (map . nnweb-reference-create-mapping)
51 (search . nnweb-reference-search)
52 (address . "http://www.reference.com/cgi-bin/pn/go"))
54 (article . nnweb-altavista-wash-article)
55 (map . nnweb-altavista-create-mapping)
56 (search . nnweb-altavista-search)
57 (address . "http://www.altavista.digital.com/cgi-bin/query")
58 (id . "/cgi-bin/news?id@%s")))
59 "Type-definition alist.")
61 (defvoo nnweb-search nil
62 "Search string to feed to DejaNews.")
64 (defvoo nnweb-max-hits 100
65 "Maximum number of hits to display.")
67 ;;; Internal variables
69 (defvoo nnweb-articles nil)
70 (defvoo nnweb-buffer nil)
72 ;;; Interface functions
74 (nnoo-define-basics nnweb)
76 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
77 (nnweb-possibly-change-server server)
79 (set-buffer nntp-server-buffer)
82 (while (setq article (pop articles))
83 (when (setq header (cadr (assq article nnweb-articles)))
84 (nnheader-insert-nov header)))
87 (deffoo nnweb-request-group (group &optional server dont-check)
88 (nnweb-possibly-change-server server)
89 (when (or (not dont-check)
91 (funcall (nnweb-definition 'map)))
94 (nnheader-report 'nnweb "Couldn't request search"))
96 (nnheader-report 'nnweb "Opened group %s" group)
98 "211 %d %d %d %s\n" (length nnweb-articles)
99 (caar nnweb-articles) (caar (last nnweb-articles))
102 (deffoo nnweb-request-article (article &optional group server buffer)
103 (nnweb-possibly-change-server server)
105 (set-buffer (or buffer nntp-server-buffer))
106 (let ((url (caddr (assq article nnweb-articles))))
108 (nnweb-fetch-url url))
109 (and (stringp article)
110 (let ((fetch (nnweb-definition 'id))
112 (when (string-match "^<\\(.*\\)>$" article)
113 (setq art (match-string 1 article)))
117 (format fetch article))))))
118 (unless nnheader-callback-function
119 (funcall (nnweb-definition 'article))
120 (nnweb-decode-entities))
121 (nnheader-report 'nnweb "Fetched article %s" article)
124 (deffoo nnweb-close-server (&optional server)
125 (when (nnweb-server-opened server)
126 (gnus-kill-buffer nnweb-buffer))
127 (nnoo-close-server 'nnweb server))
129 (deffoo nnweb-request-update-info (group info &optional server)
130 (nnweb-possibly-change-server server)
131 (setcar (cddr info) nil))
133 (deffoo nnweb-asynchronous-p ()
136 (nnoo-define-skeleton nnweb)
138 ;;; Internal functions
140 (defun nnweb-definition (type)
141 "Return the definition of TYPE."
142 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
144 (error "Undefined definition %s" type))
147 (defun nnweb-possibly-change-server (&optional server)
150 (unless (nnweb-server-opened server)
151 (nnweb-open-server server))))
153 (defun nnweb-init (server)
154 "Initialize buffers and such."
155 (unless (gnus-buffer-live-p nnweb-buffer)
158 (nnheader-set-temp-buffer
159 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
161 (defun nnweb-fetch-url (url)
163 (if (not nnheader-callback-function)
164 (let ((buf (current-buffer)))
166 (set-buffer nnweb-buffer)
169 (url-insert-file-contents url)
170 (copy-to-buffer buf (point-min) (point-max)))))
171 (nnweb-url-retrieve-asynch
172 url 'nnweb-callback (current-buffer) nnheader-callback-function)
175 (defun nnweb-callback (buffer callback)
177 (set-buffer url-working-buffer)
178 (funcall (nnweb-definition 'article))
179 (nnweb-decode-entities)
181 (goto-char (point-max))
182 (insert-buffer-substring url-working-buffer))
184 (gnus-kill-buffer url-working-buffer))
186 (defun nnweb-url-retrieve-asynch (url callback &rest data)
187 (let ((url-request-method "GET")
188 (old-asynch url-be-asynchronous)
189 (url-request-data nil)
190 (url-request-extra-headers nil)
191 (url-working-buffer (generate-new-buffer-name " *dejanews*")))
192 (setq-default url-be-asynchronous t)
194 (set-buffer (get-buffer-create url-working-buffer))
195 (setq url-current-callback-data data
196 url-be-asynchronous t
197 url-current-callback-func callback)
199 (setq-default url-be-asynchronous old-asynch)))
201 (defun nnweb-encode-www-form-urlencoded (pairs)
202 "Return PAIRS encoded for forms."
206 (concat (w3-form-encode-xwfu (car data)) "="
207 (w3-form-encode-xwfu (cdr data))))) pairs "&"))
209 (defun nnweb-fetch-form (url pairs)
210 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
211 (url-request-method 'POST)
212 (url-request-extra-headers
213 '(("Content-type" . "application/x-www-form-urlencoded"))))
214 (url-insert-file-contents url)))
216 (defun nnweb-decode-entities ()
217 (goto-char (point-min))
218 (while (re-search-forward "&\\([a-z]+\\);" nil t)
219 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
224 (defun nnweb-remove-markup ()
225 (goto-char (point-min))
226 (while (search-forward "<!--" nil t)
227 (delete-region (match-beginning 0)
228 (or (search-forward "-->" nil t)
230 (goto-char (point-min))
231 (while (re-search-forward "<[^>]+>" nil t)
232 (replace-match "" t t)))
235 ;;; DejaNews functions.
238 (defun nnweb-dejanews-create-mapping ()
239 "Perform the search and create an number-to-url alist."
241 (set-buffer nnweb-buffer)
243 (when (funcall (nnweb-definition 'search) nnweb-search)
246 Subject Score Date Newsgroup Author
249 ;; Go through all the article hits on this page.
250 (goto-char (point-min))
251 (nnweb-decode-entities)
252 (goto-char (point-min))
253 (while (re-search-forward "^ +[0-9]+\\." nil t)
256 (if (re-search-forward "^ +[0-9]+\\." nil t)
259 (goto-char (point-min))
260 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
261 (setq url (match-string 1)))
262 (nnweb-remove-markup)
263 (goto-char (point-min))
264 (while (search-forward "\t" nil t)
266 (goto-char (point-min))
267 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
268 (set (intern (match-string 1)) (match-string 2)))
270 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
271 (setq Subject (substring Subject 0 (match-beginning 0))))
275 (make-full-mail-header
276 i (concat "(" Newsgroup ") " Subject) Author Date
277 (concat "<" (message-unique-id) "-" (int-to-string i)
279 nil 0 (string-to-int Score) nil)
282 ;; See whether there is a "Get next 20 hits" button here.
283 (if (or (not (re-search-forward
284 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
285 (> i nnweb-max-hits))
288 (setq more (match-string 1))
290 (url-insert-file-contents more)))
291 ;; Return the articles in the right order.
292 (setq nnweb-articles (nreverse map))))))
294 (defun nnweb-dejanews-wash-article ()
295 (goto-char (point-min))
296 (re-search-forward "<PRE>" nil t)
297 (delete-region (point-min) (point))
298 (re-search-forward "</PRE>" nil t)
299 (delete-region (point) (point-max))
300 (nnweb-remove-markup)
301 (goto-char (point-min))
302 (while (and (looking-at " *$")
305 (while (looking-at "\\(^[^ ]+:\\) *")
306 (replace-match "\\1 " t)
308 (when (re-search-forward "\n\n+" nil t)
309 (replace-match "\n" t t)))
311 (defun nnweb-dejanews-search (search)
313 (nnweb-definition 'address)
314 `(("query" . ,search)
315 ("defaultOp" . "AND")
316 ("svcclass" . "dncurrent")
318 ("format" . "verbose")
320 ("showsort" . "score")
322 ("ageweight" . "1"))))
328 (defun nnweb-reference-create-mapping ()
329 "Perform the search and create an number-to-url alist."
331 (set-buffer nnweb-buffer)
333 (when (funcall (nnweb-definition 'search) nnweb-search)
336 Subject Score Date Newsgroups From Message-ID
339 ;; Go through all the article hits on this page.
340 (goto-char (point-min))
341 (search-forward "</pre><hr>" nil t)
342 (delete-region (point-min) (point))
343 ;(nnweb-decode-entities)
344 (goto-char (point-min))
345 (while (re-search-forward "^ +[0-9]+\\." nil t)
348 (if (re-search-forward "^$" nil t)
351 (goto-char (point-min))
352 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
353 (setq url (match-string 1)))
354 (nnweb-remove-markup)
355 (goto-char (point-min))
356 (while (search-forward "\t" nil t)
358 (goto-char (point-min))
359 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
360 (set (intern (match-string 1)) (match-string 2)))
362 (search-forward "</pre>" nil t)
366 (make-full-mail-header
367 i (concat "(" Newsgroups ") " Subject) From Date
369 nil 0 (string-to-int Score) nil)
373 ;; Return the articles in the right order.
374 (setq nnweb-articles (nreverse map))))))
376 (defun nnweb-reference-wash-article ()
377 (goto-char (point-min))
378 (re-search-forward "^</center><hr>" nil t)
379 (delete-region (point-min) (point))
380 (search-forward "<pre>" nil t)
382 (let ((body (point-marker)))
383 (search-forward "</pre>" nil t)
384 (delete-region (point) (point-max))
385 (nnweb-remove-markup)
386 (goto-char (point-min))
387 (while (looking-at " *$")
389 (narrow-to-region (point-min) body)
390 (while (and (re-search-forward "^$" nil t)
393 (goto-char (point-min))
394 (while (looking-at "\\(^[^ ]+:\\) *")
395 (replace-match "\\1 " t)
397 (goto-char (point-min))
398 (when (re-search-forward "^References:" nil t)
400 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
403 (goto-char (point-min))
405 (unless (looking-at "References")
408 (goto-char (point-min))
409 (while (search-forward "," nil t)
410 (replace-match " " t t)))
412 (set-marker body nil)))
414 (defun nnweb-reference-search (search)
415 (url-insert-file-contents
417 (nnweb-definition 'address)
419 (nnweb-encode-www-form-urlencoded
420 `(("search" . "advanced")
421 ("querytext" . ,search)
426 ("organization" . "")
429 ("choice" . "Search")
430 ("startmonth" . "Jul")
432 ("startyear" . "1996")
437 ("verbosity" . "Verbose")
438 ("ranking" . "Relevance")
441 ("score" . "50"))))))
447 (defun nnweb-altavista-create-mapping ()
448 "Perform the search and create an number-to-url alist."
450 (set-buffer nnweb-buffer)
452 (when (funcall (nnweb-definition 'search) nnweb-search)
455 Subject Score Date Newsgroups From Message-ID
458 ;; Go through all the article hits on this page.
459 (goto-char (point-min))
460 (search-forward "<dt>" nil t)
461 (delete-region (point-min) (match-beginning 0))
462 (goto-char (point-min))
463 (while (search-forward "<dt>" nil t)
464 (replace-match "\n<blubb>"))
465 (nnweb-decode-entities)
466 (goto-char (point-min))
467 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
469 (setq url (match-string 1)
470 subject (match-string 2)
471 date (match-string 3)
472 group (match-string 4)
473 id (concat "<" (match-string 5) ">")
474 from (match-string 6))
478 (make-full-mail-header
479 i (concat "(" group ") " subject) from date
484 ;; Return the articles in the right order.
485 (setq nnweb-articles (nreverse map))))))
487 (defun nnweb-altavista-wash-article ()
488 (goto-char (point-min))
490 (when (re-search-forward "<H1>\\(.*\\)</H1>" nil t)
491 (setq subject (match-string 1)))
492 (re-search-forward "^<strong>" nil t)
493 (delete-region (point-min) (match-beginning 0))
494 (goto-char (point-min))
495 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
496 (replace-match "\\1: \\2" t)
498 (when (re-search-backward "^References:" nil t)
499 (narrow-to-region (point) (progn (forward-line 1) (point)))
500 (goto-char (point-min))
501 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
502 (replace-match "<\\1> " t))
504 (nnweb-remove-markup))))
506 (defun nnweb-altavista-search (search)
507 (url-insert-file-contents
509 (nnweb-definition 'address)
511 (nnweb-encode-www-form-urlencoded
522 ;;; nnweb.el ends here