org-feed.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. ;;; org-feed.el --- Add RSS feed items to Org files
  2. ;;
  3. ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; 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. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; This module allows to create and change entries in an Org-mode
  26. ;; file triggered by items in an RSS feed. The basic functionality is
  27. ;; geared toward simply adding new items found in a feed as outline nodes
  28. ;; to an Org file. Using hooks, arbitrary actions can be triggered for
  29. ;; new or changed items.
  30. ;;
  31. ;; Selecting feeds and target locations
  32. ;; ------------------------------------
  33. ;;
  34. ;; This module is configured through a single variable, `org-feed-alist'.
  35. ;; Here is an example, using a notes/tasks feed from reQall.com.
  36. ;;
  37. ;; (setq org-feed-alist
  38. ;; '(("ReQall"
  39. ;; "http://www.reqall.com/user/feeds/rss/a1b2c3....."
  40. ;; "~/org/feeds.org" "ReQall Entries")
  41. ;;
  42. ;; With this setup, the command `M-x org-feed-update-all' will
  43. ;; collect new entries in the feed at the given URL and create
  44. ;; entries as subheadings under the "ReQall Entries" heading in the
  45. ;; file "~/org/feeds.org". Each feed should normally have its own
  46. ;; heading - however see the `:drawer' parameter.
  47. ;;
  48. ;; Besides these standard elements that need to be specified for each
  49. ;; feed, keyword-value pairs can set additional options. For example,
  50. ;; to de-select transitional entries with a title containing
  51. ;;
  52. ;; "reQall is typing what you said",
  53. ;;
  54. ;; you could use the `:filter' argument:
  55. ;;
  56. ;; (setq org-feed-alist
  57. ;; '(("ReQall"
  58. ;; "http://www.reqall.com/user/feeds/rss/a1b2c3....."
  59. ;; "~/org/feeds.org" "ReQall Entries"
  60. ;; :filter my-reqall-filter)))
  61. ;;
  62. ;; (defun my-reqall-filter (e)
  63. ;; (if (string-match "reQall is typing what you said"
  64. ;; (plist-get e :title))
  65. ;; nil
  66. ;; e))
  67. ;;
  68. ;; See the docstring for `org-feed-alist' for more details.
  69. ;;
  70. ;;
  71. ;; Keeping track of previously added entries
  72. ;; -----------------------------------------
  73. ;;
  74. ;; Since Org allows you to delete, archive, or move outline nodes,
  75. ;; org-feed.el needs to keep track of which feed items have been handled
  76. ;; before, so that they will not be handled again. For this, org-feed.el
  77. ;; stores information in a special drawer, FEEDSTATUS, under the heading
  78. ;; that received the input of the feed. You should add FEEDSTATUS
  79. ;; to your list of drawers in the files that receive feed input:
  80. ;;
  81. ;; #+DRAWERS: PROPERTIES CLOCK LOGBOOK RESULTS FEEDSTATUS
  82. ;;
  83. ;; Acknowledgments
  84. ;; ---------------
  85. ;;
  86. ;; org-feed.el is based on ideas by Brad Bozarth who implemented a
  87. ;; similar mechanism using shell and awk scripts.
  88. ;;; Code:
  89. (require 'org)
  90. (require 'sha1)
  91. (declare-function url-retrieve-synchronously "url" (url))
  92. (declare-function xml-node-children "xml" (node))
  93. (declare-function xml-get-children "xml" (node child-name))
  94. (declare-function xml-get-attribute "xml" (node attribute))
  95. (declare-function xml-get-attribute-or-nil "xml" (node attribute))
  96. (declare-function xml-substitute-special "xml" (string))
  97. (defgroup org-feed nil
  98. "Options concerning RSS feeds as inputs for Org files."
  99. :tag "Org Feed"
  100. :group 'org)
  101. (defcustom org-feed-alist nil
  102. "Alist specifying RSS feeds that should create inputs for Org.
  103. Each entry in this list specified an RSS feed tat should be queried
  104. to create inbox items in Org. Each entry is a list with the following items:
  105. name a custom name for this feed
  106. URL the Feed URL
  107. file the target Org file where entries should be listed
  108. headline the headline under which entries should be listed
  109. Additional arguments can be given using keyword-value pairs. Many of these
  110. specify functions that receive one or a list of \"entries\" as their single
  111. argument. An entry is a property list that describes a feed item. The
  112. property list has properties for each field in the item, for example `:title'
  113. for the `<title>' field and `:pubDate' for the publication date. In addition,
  114. it contains the following properties:
  115. `:item-full-text' the full text in the <item> tag
  116. `:guid-permalink' t when the guid property is a permalink
  117. Here are the keyword-value pair allows in `org-feed-alist'.
  118. :drawer drawer-name
  119. The name of the drawer for storing feed information. The default is
  120. \"FEEDSTATUS\". Using different drawers for different feeds allows
  121. several feeds to target the same inbox heading.
  122. :filter filter-function
  123. A function to select interesting entries in the feed. It gets a single
  124. entry as parameter. It should return the entry if it is relevant, or
  125. nil if it is not.
  126. :template template-string
  127. The default action on new items in the feed is to add them as children
  128. under the headline for the feed. The template describes how the entry
  129. should be formatted. If not given, it defaults to
  130. `org-feed-default-template'.
  131. :formatter formatter-function
  132. Instead of relying on a template, you may specify a function to format
  133. the outline node to be inserted as a child. This function gets passed
  134. a property list describing a single feed item, and it should return a
  135. string that is a properly formatted Org outline node of level 1.
  136. :new-handler function
  137. If adding new items as children to the outline is not what you want
  138. to do with new items, define a handler function that is called with
  139. a list of all new items in the feed, each one represented as a property
  140. list. The handler should do what needs to be done, and org-feed will
  141. mark all items given to this handler as \"handled\", i.e. they will not
  142. be passed to this handler again in future readings of the feed.
  143. When the handler is called, point will be at the feed headline.
  144. :changed-handler function
  145. This function gets passed a list of all entries that have been
  146. handled before, but are now still in the feed and have *changed*
  147. since last handled (as evidenced by a different sha1 hash).
  148. When the handler is called, point will be at the feed headline.
  149. :parse-feed function
  150. This function gets passed a buffer, and should return a list
  151. of entries, each being a property list containing the
  152. `:guid' and `:item-full-text' keys. The default is
  153. `org-feed-parse-rss-feed'; `org-feed-parse-atom-feed' is an
  154. alternative.
  155. :parse-entry function
  156. This function gets passed an entry as returned by the parse-feed
  157. function, and should return the entry with interesting properties added.
  158. The default is `org-feed-parse-rss-entry'; `org-feed-parse-atom-entry'
  159. is an alternative."
  160. :group 'org-feed
  161. :type '(repeat
  162. (list :value ("" "http://" "" "")
  163. (string :tag "Name")
  164. (string :tag "Feed URL")
  165. (file :tag "File for inbox")
  166. (string :tag "Headline for inbox")
  167. (repeat :inline t
  168. (choice
  169. (list :inline t :tag "Filter"
  170. (const :filter)
  171. (symbol :tag "Filter Function"))
  172. (list :inline t :tag "Template"
  173. (const :template)
  174. (string :tag "Template"))
  175. (list :inline t :tag "Formatter"
  176. (const :formatter)
  177. (symbol :tag "Formatter Function"))
  178. (list :inline t :tag "New items handler"
  179. (const :new-handler)
  180. (symbol :tag "Handler Function"))
  181. (list :inline t :tag "Changed items"
  182. (const :changed-handler)
  183. (symbol :tag "Handler Function"))
  184. (list :inline t :tag "Parse Feed"
  185. (const :parse-feed)
  186. (symbol :tag "Parse Feed Function"))
  187. (list :inline t :tag "Parse Entry"
  188. (const :parse-entry)
  189. (symbol :tag "Parse Entry Function"))
  190. )))))
  191. (defcustom org-feed-drawer "FEEDSTATUS"
  192. "The name of the drawer for feed status information.
  193. Each feed may also specify its own drawer name using the `:drawer'
  194. parameter in `org-feed-alist'.
  195. Note that in order to make these drawers behave like drawers, they must
  196. be added to the variable `org-drawers' or configured with a #+DRAWERS
  197. line."
  198. :group 'org-feed
  199. :type '(string :tag "Drawer Name"))
  200. (defcustom org-feed-default-template "\n* %h\n %U\n %description\n %a\n"
  201. "Template for the Org node created from RSS feed items.
  202. This is just the default, each feed can specify its own.
  203. Any fields from the feed item can be interpolated into the template with
  204. %name, for example %title, %description, %pubDate etc. In addition, the
  205. following special escapes are valid as well:
  206. %h the title, or the first line of the description
  207. %t the date as a stamp, either from <pubDate> (if present), or
  208. the current date.
  209. %T date and time
  210. %u,%U like %t,%T, but inactive time stamps
  211. %a A link, from <guid> if that is a permalink, else from <link>"
  212. :group 'org-feed
  213. :type '(string :tag "Template"))
  214. (defcustom org-feed-save-after-adding t
  215. "Non-nil means save buffer after adding new feed items."
  216. :group 'org-feed
  217. :type 'boolean)
  218. (defcustom org-feed-retrieve-method 'url-retrieve-synchronously
  219. "The method to be used to retrieve a feed URL.
  220. This can be `curl' or `wget' to call these external programs, or it can be
  221. an Emacs Lisp function that will return a buffer containing the content
  222. of the file pointed to by the URL."
  223. :group 'org-feed
  224. :type '(choice
  225. (const :tag "Internally with url.el" url-retrieve-synchronously)
  226. (const :tag "Externally with curl" curl)
  227. (const :tag "Externally with wget" wget)
  228. (function :tag "Function")))
  229. (defcustom org-feed-before-adding-hook nil
  230. "Hook that is run before adding new feed items to a file.
  231. You might want to commit the file in its current state to version control,
  232. for example."
  233. :group 'org-feed
  234. :type 'hook)
  235. (defcustom org-feed-after-adding-hook nil
  236. "Hook that is run after new items have been added to a file.
  237. Depending on `org-feed-save-after-adding', the buffer will already
  238. have been saved."
  239. :group 'org-feed
  240. :type 'hook)
  241. (defvar org-feed-buffer "*Org feed*"
  242. "The buffer used to retrieve a feed.")
  243. ;;;###autoload
  244. (defun org-feed-update-all ()
  245. "Get inbox items from all feeds in `org-feed-alist'."
  246. (interactive)
  247. (let ((nfeeds (length org-feed-alist))
  248. (nnew (apply '+ (mapcar 'org-feed-update org-feed-alist))))
  249. (message "%s from %d %s"
  250. (cond ((= nnew 0) "No new entries")
  251. ((= nnew 1) "1 new entry")
  252. (t (format "%d new entries" nnew)))
  253. nfeeds
  254. (if (= nfeeds 1) "feed" "feeds"))))
  255. ;;;###autoload
  256. (defun org-feed-update (feed &optional retrieve-only)
  257. "Get inbox items from FEED.
  258. FEED can be a string with an association in `org-feed-alist', or
  259. it can be a list structured like an entry in `org-feed-alist'."
  260. (interactive (list (org-completing-read "Feed name: " org-feed-alist)))
  261. (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
  262. (unless feed
  263. (error "No such feed in `org-feed-alist"))
  264. (catch 'exit
  265. (let ((name (car feed))
  266. (url (nth 1 feed))
  267. (file (nth 2 feed))
  268. (headline (nth 3 feed))
  269. (filter (nth 1 (memq :filter feed)))
  270. (formatter (nth 1 (memq :formatter feed)))
  271. (new-handler (nth 1 (memq :new-handler feed)))
  272. (changed-handler (nth 1 (memq :changed-handler feed)))
  273. (template (or (nth 1 (memq :template feed))
  274. org-feed-default-template))
  275. (drawer (or (nth 1 (memq :drawer feed))
  276. org-feed-drawer))
  277. (parse-feed (or (nth 1 (memq :parse-feed feed))
  278. 'org-feed-parse-rss-feed))
  279. (parse-entry (or (nth 1 (memq :parse-entry feed))
  280. 'org-feed-parse-rss-entry))
  281. feed-buffer inbox-pos new-formatted
  282. entries old-status status new changed guid-alist e guid olds)
  283. (setq feed-buffer (org-feed-get-feed url))
  284. (unless (and feed-buffer (bufferp (get-buffer feed-buffer)))
  285. (error "Cannot get feed %s" name))
  286. (when retrieve-only
  287. (throw 'exit feed-buffer))
  288. (setq entries (funcall parse-feed feed-buffer))
  289. (ignore-errors (kill-buffer feed-buffer))
  290. (save-excursion
  291. (save-window-excursion
  292. (setq inbox-pos (org-feed-goto-inbox-internal file headline))
  293. (setq old-status (org-feed-read-previous-status inbox-pos drawer))
  294. ;; Add the "handled" status to the appropriate entries
  295. (setq entries (mapcar (lambda (e)
  296. (setq e
  297. (plist-put e :handled
  298. (nth 1 (assoc
  299. (plist-get e :guid)
  300. old-status)))))
  301. entries))
  302. ;; Find out which entries are new and which are changed
  303. (dolist (e entries)
  304. (if (not (plist-get e :handled))
  305. (push e new)
  306. (setq olds (nth 2 (assoc (plist-get e :guid) old-status)))
  307. (if (and olds
  308. (not (string= (sha1
  309. (plist-get e :item-full-text))
  310. olds)))
  311. (push e changed))))
  312. ;; Parse the relevant entries fully
  313. (setq new (mapcar parse-entry new)
  314. changed (mapcar parse-entry changed))
  315. ;; Run the filter
  316. (when filter
  317. (setq new (delq nil (mapcar filter new))
  318. changed (delq nil (mapcar filter new))))
  319. (when (not (or new changed))
  320. (message "No new items in feed %s" name)
  321. (throw 'exit 0))
  322. ;; Get alist based on guid, to look up entries
  323. (setq guid-alist
  324. (append
  325. (mapcar (lambda (e) (list (plist-get e :guid) e)) new)
  326. (mapcar (lambda (e) (list (plist-get e :guid) e)) changed)))
  327. ;; Construct the new status
  328. (setq status
  329. (mapcar
  330. (lambda (e)
  331. (setq guid (plist-get e :guid))
  332. (list guid
  333. ;; things count as handled if we handle them now,
  334. ;; or if they were handled previously
  335. (if (assoc guid guid-alist) t (plist-get e :handled))
  336. ;; A hash, to detect changes
  337. (sha1 (plist-get e :item-full-text))))
  338. entries))
  339. ;; Handle new items in the feed
  340. (when new
  341. (if new-handler
  342. (progn
  343. (goto-char inbox-pos)
  344. (funcall new-handler new))
  345. ;; No custom handler, do the default adding
  346. ;; Format the new entries into an alist with GUIDs in the car
  347. (setq new-formatted
  348. (mapcar
  349. (lambda (e) (org-feed-format-entry e template formatter))
  350. new)))
  351. ;; Insert the new items
  352. (org-feed-add-items inbox-pos new-formatted))
  353. ;; Handle changed items in the feed
  354. (when (and changed-handler changed)
  355. (goto-char inbox-pos)
  356. (funcall changed-handler changed))
  357. ;; Write the new status
  358. ;; We do this only now, in case something goes wrong above, so
  359. ;; that would would end up with a status that does not reflect
  360. ;; which items truely have been handled
  361. (org-feed-write-status inbox-pos drawer status)
  362. ;; Normalize the visibility of the inbox tree
  363. (goto-char inbox-pos)
  364. (hide-subtree)
  365. (show-children)
  366. (org-cycle-hide-drawers 'children)
  367. ;; Hooks and messages
  368. (when org-feed-save-after-adding (save-buffer))
  369. (message "Added %d new item%s from feed %s to file %s, heading %s"
  370. (length new) (if (> (length new) 1) "s" "")
  371. name
  372. (file-name-nondirectory file) headline)
  373. (run-hooks 'org-feed-after-adding-hook)
  374. (length new))))))
  375. ;;;###autoload
  376. (defun org-feed-goto-inbox (feed)
  377. "Go to the inbox that captures the feed named FEED."
  378. (interactive
  379. (list (if (= (length org-feed-alist) 1)
  380. (car org-feed-alist)
  381. (org-completing-read "Feed name: " org-feed-alist))))
  382. (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
  383. (unless feed
  384. (error "No such feed in `org-feed-alist"))
  385. (org-feed-goto-inbox-internal (nth 2 feed) (nth 3 feed)))
  386. ;;;###autoload
  387. (defun org-feed-show-raw-feed (feed)
  388. "Show the raw feed buffer of a feed."
  389. (interactive
  390. (list (if (= (length org-feed-alist) 1)
  391. (car org-feed-alist)
  392. (org-completing-read "Feed name: " org-feed-alist))))
  393. (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
  394. (unless feed
  395. (error "No such feed in `org-feed-alist"))
  396. (org-pop-to-buffer-same-window
  397. (org-feed-update feed 'retrieve-only))
  398. (goto-char (point-min)))
  399. (defun org-feed-goto-inbox-internal (file heading)
  400. "Find or create HEADING in FILE.
  401. Switch to that buffer, and return the position of that headline."
  402. (find-file file)
  403. (widen)
  404. (goto-char (point-min))
  405. (if (re-search-forward
  406. (concat "^\\*+[ \t]+" heading "[ \t]*\\(:.*?:[ \t]*\\)?$")
  407. nil t)
  408. (goto-char (match-beginning 0))
  409. (goto-char (point-max))
  410. (insert "\n\n* " heading "\n\n")
  411. (org-back-to-heading t))
  412. (point))
  413. (defun org-feed-read-previous-status (pos drawer)
  414. "Get the alist of old GUIDs from the entry at POS.
  415. This will find DRAWER and extract the alist."
  416. (save-excursion
  417. (goto-char pos)
  418. (let ((end (save-excursion (org-end-of-subtree t t))))
  419. (if (re-search-forward
  420. (concat "^[ \t]*:" drawer ":[ \t]*\n\\([^\000]*?\\)\n[ \t]*:END:")
  421. end t)
  422. (read (match-string 1))
  423. nil))))
  424. (defun org-feed-write-status (pos drawer status)
  425. "Write the feed STATUS to DRAWER in entry at POS."
  426. (save-excursion
  427. (goto-char pos)
  428. (let ((end (save-excursion (org-end-of-subtree t t)))
  429. guid)
  430. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*\n")
  431. end t)
  432. (progn
  433. (goto-char (match-end 0))
  434. (delete-region (point)
  435. (save-excursion
  436. (and (re-search-forward "^[ \t]*:END:" nil t)
  437. (match-beginning 0)))))
  438. (outline-next-heading)
  439. (insert " :" drawer ":\n :END:\n")
  440. (beginning-of-line 0))
  441. (insert (pp-to-string status)))))
  442. (defun org-feed-add-items (pos entries)
  443. "Add the formatted items to the headline as POS."
  444. (let (entry level)
  445. (save-excursion
  446. (goto-char pos)
  447. (unless (looking-at org-complex-heading-regexp)
  448. (error "Wrong position"))
  449. (setq level (org-get-valid-level (length (match-string 1)) 1))
  450. (org-end-of-subtree t t)
  451. (skip-chars-backward " \t\n")
  452. (beginning-of-line 2)
  453. (setq pos (point))
  454. (while (setq entry (pop entries))
  455. (org-paste-subtree level entry 'yank))
  456. (org-mark-ring-push pos))))
  457. (defun org-feed-format-entry (entry template formatter)
  458. "Format ENTRY so that it can be inserted into an Org file.
  459. ENTRY is a property list. This function adds a `:formatted-for-org' property
  460. and returns the full property list.
  461. If that property is already present, nothing changes."
  462. (if formatter
  463. (funcall formatter entry)
  464. (let (dlines fmt tmp indent time name
  465. v-h v-t v-T v-u v-U v-a)
  466. (setq dlines (org-split-string (or (plist-get entry :description) "???")
  467. "\n")
  468. v-h (or (plist-get entry :title) (car dlines) "???")
  469. time (or (if (plist-get entry :pubDate)
  470. (org-read-date t t (plist-get entry :pubDate)))
  471. (current-time))
  472. v-t (format-time-string (org-time-stamp-format nil nil) time)
  473. v-T (format-time-string (org-time-stamp-format t nil) time)
  474. v-u (format-time-string (org-time-stamp-format nil t) time)
  475. v-U (format-time-string (org-time-stamp-format t t) time)
  476. v-a (if (setq tmp (or (and (plist-get entry :guid-permalink)
  477. (plist-get entry :guid))
  478. (plist-get entry :link)))
  479. (concat "[[" tmp "]]\n")
  480. ""))
  481. (with-temp-buffer
  482. (insert template)
  483. (goto-char (point-min))
  484. (while (re-search-forward "%\\([a-zA-Z]+\\)" nil t)
  485. (setq name (match-string 1))
  486. (cond
  487. ((member name '("h" "t" "T" "u" "U" "a"))
  488. (replace-match (symbol-value (intern (concat "v-" name))) t t))
  489. ((setq tmp (plist-get entry (intern (concat ":" name))))
  490. (save-excursion
  491. (save-match-data
  492. (beginning-of-line 1)
  493. (when (looking-at (concat "^\\([ \t]*\\)%" name "[ \t]*$"))
  494. (setq tmp (org-feed-make-indented-block
  495. tmp (org-get-indentation))))))
  496. (replace-match tmp t t))))
  497. (decode-coding-string
  498. (buffer-string) (detect-coding-region (point-min) (point-max) t))))))
  499. (defun org-feed-make-indented-block (s n)
  500. "Add indentation of N spaces to a multiline string S."
  501. (if (not (string-match "\n" s))
  502. s
  503. (mapconcat 'identity
  504. (org-split-string s "\n")
  505. (concat "\n" (make-string n ?\ )))))
  506. (defun org-feed-skip-http-headers (buffer)
  507. "Remove HTTP headers from BUFFER, and return it.
  508. Assumes headers are indeed present!"
  509. (with-current-buffer buffer
  510. (widen)
  511. (goto-char (point-min))
  512. (search-forward "\n\n")
  513. (delete-region (point-min) (point))
  514. buffer))
  515. (defun org-feed-get-feed (url)
  516. "Get the RSS feed file at URL and return the buffer."
  517. (cond
  518. ((eq org-feed-retrieve-method 'url-retrieve-synchronously)
  519. (org-feed-skip-http-headers (url-retrieve-synchronously url)))
  520. ((eq org-feed-retrieve-method 'curl)
  521. (ignore-errors (kill-buffer org-feed-buffer))
  522. (call-process "curl" nil org-feed-buffer nil "--silent" url)
  523. org-feed-buffer)
  524. ((eq org-feed-retrieve-method 'wget)
  525. (ignore-errors (kill-buffer org-feed-buffer))
  526. (call-process "wget" nil org-feed-buffer nil "-q" "-O" "-" url)
  527. org-feed-buffer)
  528. ((functionp org-feed-retrieve-method)
  529. (funcall org-feed-retrieve-method url))))
  530. (defun org-feed-parse-rss-feed (buffer)
  531. "Parse BUFFER for RSS feed entries.
  532. Returns a list of entries, with each entry a property list,
  533. containing the properties `:guid' and `:item-full-text'."
  534. (let ((case-fold-search t)
  535. entries beg end item guid entry)
  536. (with-current-buffer buffer
  537. (widen)
  538. (goto-char (point-min))
  539. (while (re-search-forward "<item\\>.*?>" nil t)
  540. (setq beg (point)
  541. end (and (re-search-forward "</item>" nil t)
  542. (match-beginning 0)))
  543. (setq item (buffer-substring beg end)
  544. guid (if (string-match "<guid\\>.*?>\\(.*?\\)</guid>" item)
  545. (org-match-string-no-properties 1 item)))
  546. (setq entry (list :guid guid :item-full-text item))
  547. (push entry entries)
  548. (widen)
  549. (goto-char end))
  550. (nreverse entries))))
  551. (defun org-feed-parse-rss-entry (entry)
  552. "Parse the `:item-full-text' field for xml tags and create new properties."
  553. (require 'xml)
  554. (with-temp-buffer
  555. (insert (plist-get entry :item-full-text))
  556. (goto-char (point-min))
  557. (while (re-search-forward "<\\([a-zA-Z]+\\>\\).*?>\\([^\000]*?\\)</\\1>"
  558. nil t)
  559. (setq entry (plist-put entry
  560. (intern (concat ":" (match-string 1)))
  561. (xml-substitute-special (match-string 2)))))
  562. (goto-char (point-min))
  563. (unless (re-search-forward "isPermaLink[ \t]*=[ \t]*\"false\"" nil t)
  564. (setq entry (plist-put entry :guid-permalink t))))
  565. entry)
  566. (defun org-feed-parse-atom-feed (buffer)
  567. "Parse BUFFER for Atom feed entries.
  568. Returns a list of entries, with each entry a property list,
  569. containing the properties `:guid' and `:item-full-text'.
  570. The `:item-full-text' property actually contains the sexp
  571. formatted as a string, not the original XML data."
  572. (require 'xml)
  573. (with-current-buffer buffer
  574. (widen)
  575. (let ((feed (car (xml-parse-region (point-min) (point-max)))))
  576. (mapcar
  577. (lambda (entry)
  578. (list
  579. :guid (car (xml-node-children (car (xml-get-children entry 'id))))
  580. :item-full-text (prin1-to-string entry)))
  581. (xml-get-children feed 'entry)))))
  582. (defun org-feed-parse-atom-entry (entry)
  583. "Parse the `:item-full-text' as a sexp and create new properties."
  584. (let ((xml (car (read-from-string (plist-get entry :item-full-text)))))
  585. ;; Get first <link href='foo'/>.
  586. (setq entry (plist-put entry :link
  587. (xml-get-attribute
  588. (car (xml-get-children xml 'link))
  589. 'href)))
  590. ;; Add <title/> as :title.
  591. (setq entry (plist-put entry :title
  592. (xml-substitute-special
  593. (car (xml-node-children
  594. (car (xml-get-children xml 'title)))))))
  595. (let* ((content (car (xml-get-children xml 'content)))
  596. (type (xml-get-attribute-or-nil content 'type)))
  597. (when content
  598. (cond
  599. ((string= type "text")
  600. ;; We like plain text.
  601. (setq entry (plist-put entry :description
  602. (xml-substitute-special
  603. (car (xml-node-children content))))))
  604. ((string= type "html")
  605. ;; TODO: convert HTML to Org markup.
  606. (setq entry (plist-put entry :description
  607. (xml-substitute-special
  608. (car (xml-node-children content))))))
  609. ((string= type "xhtml")
  610. ;; TODO: convert XHTML to Org markup.
  611. (setq entry (plist-put entry :description
  612. (prin1-to-string
  613. (xml-node-children content)))))
  614. (t
  615. (setq entry (plist-put entry :description
  616. (format "Unknown '%s' content." type)))))))
  617. entry))
  618. (provide 'org-feed)
  619. ;;; org-feed.el ends here