org-feed.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. ;;; org-feed.el --- Add RSS feed items to Org files
  2. ;;
  3. ;; Copyright (C) 2009 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. ;; Version: 6.24trans
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This library allows to create entries in an Org-mode file from
  26. ;; RSS feeds.
  27. ;;
  28. ;; Selecting feeds and target locations
  29. ;; -----------------------------------
  30. ;;
  31. ;; This module is configured through a single variable, `org-feed-alist'.
  32. ;; Here is an example, using a notes/tasks feed from reQall.com.
  33. ;;
  34. ;; (setq org-feed-alist
  35. ;; '(("ReQall"
  36. ;; "http://www.reqall.com/user/feeds/rss/a1b2c3....."
  37. ;; "~/org/feeds.org" "ReQall Entries" nil)
  38. ;;
  39. ;; With this setup, the command `M-x org-feed-update-all' will
  40. ;; collect new entries in the feed at the given URL and create
  41. ;; entries as subheading under the "ReQall Entries" heading in the
  42. ;; file "~/org.feeds.org". The final element in the alist entry in
  43. ;; this list can be a filter function to further process the parsed
  44. ;; information. For example, here we turn entries with
  45. ;; "<category>Task</category>" into TODO entries by adding the
  46. ;; keyword to the title:
  47. ;;
  48. ;; (setq org-feed-alist
  49. ;; '(("ReQall"
  50. ;; "http://www.reqall.com/user/feeds/rss/a1b2c3....."
  51. ;; "~/org/feeds.org" "ReQall Entries"
  52. ;; my-raquall-filter)))
  53. ;;
  54. ;; (defun my-requall-filter (e)
  55. ;; (when (equal (plist-get e :category) "Task")
  56. ;; (setq e (plist-put e :title
  57. ;; (concat "TODO " (plist-get e :title)))))
  58. ;; e)
  59. ;;
  60. ;; Another possibility for the filter function would be to format
  61. ;; the entire Org node for the feed item, by adding the formatted
  62. ;; entry as a `:formatted-for-org' property:
  63. ;;
  64. ;;
  65. ;; (defun my-requall-filter (e)
  66. ;; (setq e (plist-put
  67. ;; e :formatted-for-org
  68. ;; (format "* %s\n%s"
  69. ;; (plist-get e :title)
  70. ;; (plist-get e :description))))
  71. ;; e)
  72. ;;
  73. ;; The filter function may also decide that certain feed items
  74. ;; should be ignored, by returning nil instead of the entry.
  75. ;;
  76. ;;
  77. ;; Keeping track of old GUIDs
  78. ;; --------------------------
  79. ;;
  80. ;; Since Org allows you to delete, archive, or move outline nodes,
  81. ;; org-feed.el needs to keep track of GUIDs in the feed it has
  82. ;; already processed. It does so by listing them in a special
  83. ;; drawer, FEEDGUIDS, under the heading that received the input of
  84. ;; te feed. You should add FEEDGUIDS to your list of drawers
  85. ;; in the files that receive feed input:
  86. ;;
  87. ;; #+DRAWERS: PROPERTIES LOGBOOK FEEDGUIDS
  88. ;;
  89. ;; Acknowledgements
  90. ;; ----------------
  91. ;;
  92. ;; org-feed.el is based on ideas by Brad Bozarth who implemented it
  93. ;; using shell and awk scripts, and who in this way made me for the
  94. ;; first time look into an RSS feed, showing me how simple this really
  95. ;; was.
  96. (require 'org)
  97. (declare-function url-retrieve-synchronously "url" (url))
  98. (defgroup org-feed nil
  99. "Options concerning RSS feeds as inputs for Org files."
  100. :tag "Org ID"
  101. :group 'org)
  102. ;;;###autoload
  103. (defcustom org-feed-alist nil
  104. "Alist specifying RSS feeds that should create inputs for Org.
  105. Each entry in this list specified an RSS feed tat should be queried
  106. to create inbox items in Org. Each entry is a list with the following items:
  107. name a custom name for this feed
  108. URL the Feed URL
  109. file the target Org file where entries should be listed
  110. headline the headline under which entries should be listed
  111. filter a filter function to modify the property list before
  112. an Org entry is created from it.
  113. The filter function gets as a argument a property list describing the item.
  114. That list has a property for each field, for example `:title' for the
  115. `<title>' field and `:pubDate' for the publication date. In addition,
  116. it contains the following properties:
  117. `:item-full-text' the full text in the <item> tag.
  118. `:guid-permalink' t when the guid property is a permalink
  119. The filter function can modify the existing fields before an item
  120. is constructed from the `:title', `:pubDate', `:link', `:guid', and
  121. `:description' fields. For more control, the filter can construct
  122. the Org item itself, by adding a `:formatted-for-org' property that
  123. specifies the complete outline node that should be added.
  124. If the filter returns nil for some entries, these will be marked as seen
  125. but *not* inserted into the inbox."
  126. :group 'org-feed
  127. :type '(repeat
  128. (list :value ("" "http://" "" "" nil)
  129. (string :tag "Name")
  130. (string :tag "Feed URL")
  131. (file :tag "File for inbox")
  132. (string :tag "Headline for inbox")
  133. (symbol :tag "Filter Function"))))
  134. (defcustom org-feed-save-after-adding t
  135. "Non-nil means, save buffer after adding new feed items."
  136. :group 'org-feed
  137. :type 'boolean)
  138. (defcustom org-feed-retrieve-method 'url-retrieve-synchronously
  139. "The method to be used to retrieve a feed URL.
  140. This can be `curl' or `wget' to call these external programs, or it can be
  141. an Emacs Lisp function that will return a buffer containing the content
  142. of the file pointed to by the URL."
  143. :group 'org-feed
  144. :type '(choice
  145. (const :tag "Internally with url.el" url-retrieve-synchronously)
  146. (const :tag "Externally with curl" curl)
  147. (const :tag "Externally with wget" wget)
  148. (function :tag "Function")))
  149. (defcustom org-feed-assume-stable t
  150. "Non-nil means, assume feeds to be stable.
  151. A stable feed is one which only adds and removes items, but never removes
  152. an item with a given GUID and then later adds it back in. So if the feed
  153. is stable, this means we can simple remember the GUIDs present as the ones
  154. we have seen, and we can forget GUIDs that used to be in the feed but no
  155. longer are. So for stable feeds, we only need to remember a limited
  156. number of GUIDs. For unstable ones, we need to remember all GUIDs we have
  157. ever seen, which can be a very long list indeed."
  158. :group 'org-feed
  159. :type 'boolean)
  160. (defcustom org-feed-before-adding-hook nil
  161. "Hook that is run before adding new feed items to a file.
  162. You might want to commit the file in its current state to version control,
  163. for example."
  164. :group 'org-feed
  165. :type 'hook)
  166. (defcustom org-feed-after-adding-hook nil
  167. "Hook that is run after new items have been added to a file.
  168. Depending on `org-feed-save-after-adding', the buffer will already
  169. have been saved."
  170. :group 'org-feed
  171. :type 'hook)
  172. (defvar org-feed-buffer "*Org feed*"
  173. "The buffer used to retrieve a feed.")
  174. ;;;###autoload
  175. (defun org-feed-update-all ()
  176. "Get inbox items from all feeds in `org-feed-alist'."
  177. (interactive)
  178. (let ((nfeeds (length org-feed-alist))
  179. (nnew (apply '+ (mapcar 'org-feed-update org-feed-alist))))
  180. (message "%s from %d %s"
  181. (cond ((= nnew 0) "No new entries")
  182. ((= nnew 1) "1 new entry")
  183. (t (format "%d new entries" nnew)))
  184. nfeeds
  185. (if (= nfeeds 1) "feed" "feeds"))))
  186. ;;;###autoload
  187. (defun org-feed-update (feed)
  188. "Get inbox items from FEED.
  189. FEED can be a string with an association in `org-feed-alist', or
  190. it can be a list structured like an entry in `org-feed-alist'."
  191. (interactive (list (org-completing-read "Feed name: " org-feed-alist)
  192. current-prefix-arg))
  193. (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
  194. (unless feed
  195. (error "No such feed in `org-feed-alist"))
  196. (let ((feed-name (car feed))
  197. (feed-url (nth 1 feed))
  198. (feed-file (nth 2 feed))
  199. (feed-headline (nth 3 feed))
  200. (feed-formatter (nth 4 feed))
  201. feed-buffer feed-pos
  202. entries entries2 old-guids current-guids new new-selected e)
  203. (setq feed-buffer (org-feed-get-feed feed-url))
  204. (unless (and feed-buffer (bufferp feed-buffer))
  205. (error "Cannot get feed %s" feed-name))
  206. (setq entries (org-feed-parse-feed feed-buffer)
  207. entries2 entries)
  208. (ignore-errors (kill-buffer feed-buffer))
  209. (save-excursion
  210. (save-window-excursion
  211. (setq feed-pos (org-feed-goto-inbox-internal feed-file feed-headline))
  212. (setq old-guids (org-feed-get-old-guids feed-pos))
  213. (while (setq e (pop entries2))
  214. (unless (member (plist-get e :guid) old-guids)
  215. (push (org-feed-parse-entry e) new)))
  216. (if (not new)
  217. (progn (message "No new items in feed %s" feed-name) 0)
  218. ;; Format the new entries
  219. (run-hooks 'org-feed-before-adding-hook)
  220. (setq new-selected new)
  221. (when feed-formatter
  222. (setq new-selected (mapcar feed-formatter new-selected)))
  223. (setq new-selected (mapcar 'org-feed-format new-selected))
  224. (setq new-selected (delq nil new-selected))
  225. ;; Insert the new items
  226. (apply 'org-feed-add-items feed-pos new-selected)
  227. ;; Update the list of seen GUIDs in a drawer
  228. (if org-feed-assume-stable
  229. (apply 'org-feed-add-guids feed-pos 'replace entries)
  230. (apply 'org-feed-add-guids feed-pos nil new))
  231. (goto-char feed-pos)
  232. (show-children)
  233. (when org-feed-save-after-adding
  234. (save-buffer))
  235. (message "Added %d new item%s from feed %s to file %s, heading %s"
  236. (length new) (if (> (length new) 1) "s" "")
  237. feed-name
  238. (file-name-nondirectory feed-file) feed-headline)
  239. (run-hooks 'org-feed-after-adding-hook)
  240. (length new))))))
  241. ;;;###autoload
  242. (defun org-feed-goto-inbox (feed)
  243. "Go to the inbox that captures feed FEED."
  244. (interactive
  245. (list (if (= (length org-feed-alist) 1)
  246. (car org-feed-alist)
  247. (org-completing-read "Feed name: " org-feed-alist))))
  248. (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
  249. (unless feed
  250. (error "No such feed in `org-feed-alist"))
  251. (org-feed-goto-inbox (nth 2 feed) (nth 3 feed)))
  252. (defun org-feed-goto-inbox-internal (file heading)
  253. "Find or create HEADING in FILE.
  254. Switch to that buffer, and return the position of that headline."
  255. (find-file file)
  256. (widen)
  257. (goto-char (point-min))
  258. (if (re-search-forward
  259. (concat "^\\*+[ \t]+" heading "[ \t]*\\(:.*?:[ \t]*\\)?$")
  260. nil t)
  261. (goto-char (match-beginning 0))
  262. (goto-char (point-max))
  263. (insert "\n\n* " heading "\n\n")
  264. (org-back-to-heading t))
  265. (point))
  266. (defun org-feed-get-old-guids (pos)
  267. "Get the list of old GUIDs from the entry at POS.
  268. This will find the FEEDGUIDS drawer and extract the IDs."
  269. (save-excursion
  270. (goto-char pos)
  271. (let ((end (save-excursion (org-end-of-subtree t t))))
  272. (if (re-search-forward
  273. "^[ \t]*:FEEDGUIDS:[ \t]*\n\\([^\000]*?\\)\n[ \t]*:END:"
  274. end t)
  275. (org-split-string (org-trim (org-match-string-no-properties 1))
  276. "[ \t]*\n[ \t]*")
  277. nil))))
  278. (defun org-feed-add-guids (pos replace &rest entries)
  279. "Add GUIDs for headline at POS.
  280. When REPLACE is non-nil, replace all GUIDs by the new ones."
  281. (save-excursion
  282. (goto-char pos)
  283. (let ((end (save-excursion (org-end-of-subtree t t)))
  284. guid)
  285. (if (re-search-forward "^[ \t]*:FEEDGUIDS:[ \t]*\n" end t)
  286. (progn
  287. (goto-char (match-end 0))
  288. (when replace
  289. (delete-region (point)
  290. (save-excursion
  291. (and (re-search-forward "^[ \t]*:END:" nil t)
  292. (match-beginning 0))))))
  293. (outline-next-heading)
  294. (insert " :FEEDGUIDS:\n :END:\n")
  295. (beginning-of-line 0))
  296. (while entries
  297. (when (setq guid (plist-get (pop entries) :guid))
  298. (insert " " guid "\n"))))))
  299. (defun org-feed-add-items (pos &rest entries)
  300. "Add the formatted items to the headline as POS."
  301. (let (entry level)
  302. (save-excursion
  303. (goto-char pos)
  304. (unless (looking-at org-complex-heading-regexp)
  305. (error "Wrong position"))
  306. (setq level (org-get-valid-level (length (match-string 1)) 1))
  307. (org-end-of-subtree t t)
  308. (skip-chars-backward " \t\n")
  309. (beginning-of-line 2)
  310. (setq pos (point))
  311. (while (setq entry (pop entries))
  312. (org-paste-subtree level (plist-get entry :formatted-for-org) 'yank))
  313. (org-mark-ring-push pos))))
  314. (defun org-feed-format (entry)
  315. "Format ENTRY so that it can be inserted into an Org file.
  316. ENTRY is a property list. This function adds a `:formatted-for-org' property
  317. and returns the full property list.
  318. If that property is already present, nothing changes."
  319. (unless (or (not entry) ; not an entry at all
  320. (plist-get entry :formatted-for-org)) ; already formatted
  321. (let (lines fmt tmp indent)
  322. (setq lines (org-split-string (or (plist-get entry :description) "???")
  323. "\n")
  324. indent " ")
  325. (setq fmt
  326. (concat
  327. "* " (or (plist-get entry :title) (car lines)) "\n"
  328. (if (setq tmp (plist-get entry :pubDate))
  329. (concat
  330. " ["
  331. (substring
  332. (format-time-string (cdr org-time-stamp-formats)
  333. (org-read-date t t tmp))
  334. 1 -1)
  335. "]\n"))
  336. (concat " :PROPERTIES:\n :FEED-GUID: "
  337. (plist-get entry :guid)
  338. "\n :END:\n")
  339. (mapconcat (lambda (x) (concat indent x)) lines "\n") "\n"
  340. (if (setq tmp (or (and (plist-get entry :guid-permalink)
  341. (plist-get entry :guid))
  342. (plist-get entry :link)))
  343. (concat " [[" tmp "]]\n")))
  344. entry (plist-put entry :formatted-for-org fmt))))
  345. entry)
  346. (defun org-feed-get-feed (url)
  347. "Get the RSS feed file at URL and return the buffer."
  348. (cond
  349. ((eq org-feed-retrieve-method 'url-retrieve-synchronously)
  350. (url-retrieve-synchronously url))
  351. ((eq org-feed-retrieve-method 'curl)
  352. (ignore-errors (kill-buffer org-feed-buffer))
  353. (call-process "curl" nil org-feed-buffer nil url)
  354. org-feed-buffer)
  355. ((eq org-feed-retrieve-method 'wget)
  356. (ignore-errors (kill-buffer org-feed-buffer))
  357. (call-process "curl" nil org-feed-buffer nil "-q" "-O" "-" url)
  358. org-feed-buffer)
  359. ((functionp org-feed-retrieve-method)
  360. (funcall org-feed-retrieve-method url))))
  361. (defun org-feed-parse-feed (buffer)
  362. "Parse BUFFER for RS feed entries.
  363. Returns a list of entries, with each entry a property list,
  364. containing the properties `:guid' and `:item-full-text'."
  365. (let (entries beg end item guid entry)
  366. (with-current-buffer buffer
  367. (widen)
  368. (goto-char (point-min))
  369. (while (re-search-forward "<item>" nil t)
  370. (setq beg (point)
  371. end (and (re-search-forward "</item>" nil t)
  372. (match-beginning 0)))
  373. (setq item (buffer-substring beg end)
  374. guid (if (string-match "<guid\\>.*?>\\(.*?\\)</guid>" item)
  375. (org-match-string-no-properties 1 item)))
  376. (setq entry (list :guid guid :item-full-text item))
  377. (push entry entries)
  378. (widen)
  379. (goto-char end))
  380. (nreverse entries))))
  381. (defun org-feed-parse-entry (entry)
  382. "Parse the `:item-full-text' field for xml tags and create new properties."
  383. (with-temp-buffer
  384. (insert (plist-get entry :item-full-text))
  385. (goto-char (point-min))
  386. (while (re-search-forward "<\\([a-zA-Z]+\\>\\).*?>\\([^\000]*?\\)</\\1>"
  387. nil t)
  388. (setq entry (plist-put entry
  389. (intern (concat ":" (match-string 1)))
  390. (match-string 2))))
  391. (goto-char (point-min))
  392. (unless (re-search-forward "isPermaLink[ \t]*=[ \t]*\"false\"" nil t)
  393. (setq entry (plist-put entry :guid-permalink t))))
  394. entry)
  395. (provide 'org-feed)
  396. ;;; org-feed.el ends here
  397. ;; arch-tag: 0929b557-9bc4-47f4-9633-30a12dbb5ae2