org-feed.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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")
  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 subheadings under the "ReQall Entries" heading in the
  42. ;; file "~/org.feeds.org".
  43. ;; In addition to these standard arguments, additional keyword-value
  44. ;; pairs are possible. For example, here we deselect entries with
  45. ;; a description containing "Reqall is typing" using the `:filter'
  46. ;; argument:
  47. ;;
  48. ;; (setq org-feed-alist
  49. ;; '(("ReQall"
  50. ;; "http://www.reqall.com/user/feeds/rss/a1b2c3....."
  51. ;; "~/org/feeds.org" "ReQall Entries"
  52. ;; :filter my-reqall-filter)))
  53. ;;
  54. ;; (defun my-reqall-filter (e)
  55. ;; (if (string-match "Reqall is typing" (plist-get e :description))
  56. ;; nil
  57. ;; e)
  58. ;;
  59. ;; A `:template' entry in the alist would override the template
  60. ;; in `org-feed-default-template' for the construction of the outline
  61. ;; node to be inserted. You may also write your own function to format
  62. ;; the entry and specify it using the `:formatter' keyword.
  63. ;;
  64. ;; Keeping track of previously added entries
  65. ;; -----------------------------------------
  66. ;;
  67. ;; Since Org allows you to delete, archive, or move outline nodes,
  68. ;; org-feed.el needs to keep track of which feed items have been added
  69. ;; before, so that they will not be added again. For this, org-feed.el
  70. ;; stores information in a special drawer, FEEDSTATUS, under the heading
  71. ;; that received the input of the feed. For this reason, each feed must
  72. ;; have its own headline in an Org file. You should add FEEDSTATUS
  73. ;; to your list of drawers in the files that receive feed input:
  74. ;;
  75. ;; #+DRAWERS: PROPERTIES LOGBOOK FEEDSTATUS
  76. ;;
  77. ;; Acknowledgements
  78. ;; ----------------
  79. ;;
  80. ;; org-feed.el is based on ideas by Brad Bozarth who implemented a
  81. ;; similar mechanism using shell and awk scripts, and who in this
  82. ;; way made me for the first time look into an RSS feed, showing
  83. ;; how simple this really was. Because I wanted to include a
  84. ;; solution into Org with as few dependencies as possible, I
  85. ;; reimplemented his ideas in Emacs Lisp.
  86. ;;; Code:
  87. (require 'org)
  88. (declare-function url-retrieve-synchronously "url" (url))
  89. (defgroup org-feed nil
  90. "Options concerning RSS feeds as inputs for Org files."
  91. :tag "Org ID"
  92. :group 'org)
  93. ;;;###autoload
  94. (defcustom org-feed-alist nil
  95. "Alist specifying RSS feeds that should create inputs for Org.
  96. Each entry in this list specified an RSS feed tat should be queried
  97. to create inbox items in Org. Each entry is a list with the following items:
  98. name a custom name for this feed
  99. URL the Feed URL
  100. file the target Org file where entries should be listed
  101. headline the headline under which entries should be listed
  102. Additional argumetns can be given using keyword-value pairs:
  103. :filter filter-function
  104. A function to filter entries before Org nodes are
  105. created from them.
  106. :template template-string
  107. The template to create an Org node from a feed item.
  108. For more control, use the `:formatter'.
  109. :formatter formatter-function
  110. A function to filter entries before Org nodes are
  111. created from them.
  112. The filter function gets as a argument a property list describing the item.
  113. That list has a property for each field, for example `:title' for the
  114. `<title>' field and `:pubDate' for the publication date. In addition,
  115. it contains the following properties:
  116. `:item-full-text' the full text in the <item> tag
  117. `:guid-permalink' t when the guid property is a permalink
  118. The filter function should do only one thing: decide whether this entry
  119. is worth being added now to the Org file. The filter does not need to worry
  120. if the entry was added in the past, just decide if this is a junk entry,
  121. or something useful. Entries with a given GUID will be added only once,
  122. the first time they pass the filter.
  123. Entries will be turned onto Org nodes acccording to a template. If no
  124. template is given here, `org-feed-default-template' is used. See the
  125. docstring of that variable for information on the template syntax. If
  126. creating the node requires more logic than a template can provide, define a
  127. :formatter function that will take an entry and return the formatted Org
  128. node as a string."
  129. :group 'org-feed
  130. :type '(repeat
  131. (list :value ("" "http://" "" "")
  132. (string :tag "Name")
  133. (string :tag "Feed URL")
  134. (file :tag "File for inbox")
  135. (string :tag "Headline for inbox")
  136. (repeat :inline t
  137. (choice
  138. (list :inline t :tag "Template"
  139. (const :template) (string :tag "Template"))
  140. (list :inline t :tag "Filter"
  141. (const :filter) (symbol :tag "Filter Function"))
  142. (list :inline t :tag "Formatter"
  143. (const :filter) (symbol :tag "Formatter Function"))
  144. )))))
  145. (defcustom org-feed-default-template "\n* %h\n %U\n %description\n %a\n"
  146. "Template for the Org node created from RSS feed items.
  147. This is just the default, each feed can specify its own.
  148. Any fields from the feed item can be interpolated into the template with
  149. %name, for example %title, %description, %pubDate etc. In addition, the
  150. following special escapes are valid as well:
  151. %h the title, or the first line of the description
  152. %t the date as a stamp, either from <pubDate> (if present), or
  153. the current date.
  154. %T date and time
  155. %u,%U like %t,%T, but inactive time stamps
  156. %a A link, from <guid> if that is a permalink, else from <link>"
  157. :group 'org-feed
  158. :type '(string :tag "Template"))
  159. (defcustom org-feed-save-after-adding t
  160. "Non-nil means, save buffer after adding new feed items."
  161. :group 'org-feed
  162. :type 'boolean)
  163. (defcustom org-feed-retrieve-method 'url-retrieve-synchronously
  164. "The method to be used to retrieve a feed URL.
  165. This can be `curl' or `wget' to call these external programs, or it can be
  166. an Emacs Lisp function that will return a buffer containing the content
  167. of the file pointed to by the URL."
  168. :group 'org-feed
  169. :type '(choice
  170. (const :tag "Internally with url.el" url-retrieve-synchronously)
  171. (const :tag "Externally with curl" curl)
  172. (const :tag "Externally with wget" wget)
  173. (function :tag "Function")))
  174. (defcustom org-feed-before-adding-hook nil
  175. "Hook that is run before adding new feed items to a file.
  176. You might want to commit the file in its current state to version control,
  177. for example."
  178. :group 'org-feed
  179. :type 'hook)
  180. (defcustom org-feed-after-adding-hook nil
  181. "Hook that is run after new items have been added to a file.
  182. Depending on `org-feed-save-after-adding', the buffer will already
  183. have been saved."
  184. :group 'org-feed
  185. :type 'hook)
  186. (defvar org-feed-buffer "*Org feed*"
  187. "The buffer used to retrieve a feed.")
  188. ;;;###autoload
  189. (defun org-feed-update-all ()
  190. "Get inbox items from all feeds in `org-feed-alist'."
  191. (interactive)
  192. (let ((nfeeds (length org-feed-alist))
  193. (nnew (apply '+ (mapcar 'org-feed-update org-feed-alist))))
  194. (message "%s from %d %s"
  195. (cond ((= nnew 0) "No new entries")
  196. ((= nnew 1) "1 new entry")
  197. (t (format "%d new entries" nnew)))
  198. nfeeds
  199. (if (= nfeeds 1) "feed" "feeds"))))
  200. ;;;###autoload
  201. (defun org-feed-update (feed)
  202. "Get inbox items from FEED.
  203. FEED can be a string with an association in `org-feed-alist', or
  204. it can be a list structured like an entry in `org-feed-alist'."
  205. (interactive (list (org-completing-read "Feed name: " org-feed-alist)))
  206. (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
  207. (unless feed
  208. (error "No such feed in `org-feed-alist"))
  209. (catch 'exit
  210. (let ((name (car feed))
  211. (url (nth 1 feed))
  212. (file (nth 2 feed))
  213. (headline (nth 3 feed))
  214. (filter (nth 1 (memq :filter feed)))
  215. (formatter (nth 1 (memq :formatter feed)))
  216. (template (or (nth 1 (memq :template feed))
  217. org-feed-default-template))
  218. feed-buffer inbox-pos
  219. entries old-status status new e guid)
  220. (setq feed-buffer (org-feed-get-feed url))
  221. (unless (and feed-buffer (bufferp feed-buffer))
  222. (error "Cannot get feed %s" name))
  223. (setq entries (org-feed-parse-feed feed-buffer))
  224. (ignore-errors (kill-buffer feed-buffer))
  225. (save-excursion
  226. (save-window-excursion
  227. (setq inbox-pos (org-feed-goto-inbox-internal file headline))
  228. (setq old-status (org-feed-read-previous-status inbox-pos))
  229. ;; Add the "added" status to the appropriate entries
  230. (setq entries (mapcar (lambda (e)
  231. (setq e (plist-put e :added
  232. (nth 1 (assoc
  233. (plist-get e :guid)
  234. old-status)))))
  235. entries))
  236. ;; Find out which entries are new
  237. (setq new (delq nil (mapcar (lambda (e)
  238. (if (plist-get e :added) nil e))
  239. entries)))
  240. ;; Parse the entries fully
  241. (setq new (mapcar 'org-feed-parse-entry new))
  242. ;; Run the filter
  243. (when filter
  244. (setq new (delq nil (mapcar filter new))))
  245. (when (not new)
  246. (message "No new items in feed %s" name)
  247. (throw 'exit 0))
  248. ;; Format the new entries into an alist with GUIDs in the car
  249. (setq new (mapcar
  250. (lambda (e)
  251. (list (plist-get e :guid)
  252. (org-feed-format-entry e template formatter)))
  253. new))
  254. ;; Construct the new status
  255. (setq status
  256. (mapcar
  257. (lambda (e)
  258. (setq guid (plist-get e :guid))
  259. (list guid (if (assoc guid new) t (plist-get e :added))))
  260. entries))
  261. ;; Insert the new items
  262. (org-feed-add-items inbox-pos new)
  263. ;; Write the new status
  264. (org-feed-write-status inbox-pos status)
  265. ;; Normalize the visibility of the inbox tree
  266. (goto-char inbox-pos)
  267. (hide-subtree)
  268. (show-children)
  269. (org-cycle-hide-drawers 'children)
  270. (when org-feed-save-after-adding (save-buffer))
  271. (message "Added %d new item%s from feed %s to file %s, heading %s"
  272. (length new) (if (> (length new) 1) "s" "")
  273. name
  274. (file-name-nondirectory file) headline)
  275. (run-hooks 'org-feed-after-adding-hook)
  276. (length new))))))
  277. ;;;###autoload
  278. (defun org-feed-goto-inbox (feed)
  279. "Go to the inbox that captures the feed named FEED."
  280. (interactive
  281. (list (if (= (length org-feed-alist) 1)
  282. (car org-feed-alist)
  283. (org-completing-read "Feed name: " org-feed-alist))))
  284. (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
  285. (unless feed
  286. (error "No such feed in `org-feed-alist"))
  287. (org-feed-goto-inbox-internal (nth 2 feed) (nth 3 feed)))
  288. (defun org-feed-goto-inbox-internal (file heading)
  289. "Find or create HEADING in FILE.
  290. Switch to that buffer, and return the position of that headline."
  291. (find-file file)
  292. (widen)
  293. (goto-char (point-min))
  294. (if (re-search-forward
  295. (concat "^\\*+[ \t]+" heading "[ \t]*\\(:.*?:[ \t]*\\)?$")
  296. nil t)
  297. (goto-char (match-beginning 0))
  298. (goto-char (point-max))
  299. (insert "\n\n* " heading "\n\n")
  300. (org-back-to-heading t))
  301. (point))
  302. (defun org-feed-read-previous-status (pos)
  303. "Get the alist of old GUIDs from the entry at POS.
  304. This will find the FEEDSTATUS drawer and extract the alist."
  305. (save-excursion
  306. (goto-char pos)
  307. (let ((end (save-excursion (org-end-of-subtree t t))))
  308. (if (re-search-forward
  309. "^[ \t]*:FEEDSTATUS:[ \t]*\n\\([^\000]*?\\)\n[ \t]*:END:"
  310. end t)
  311. (read (match-string 1))
  312. nil))))
  313. (defun org-feed-write-status (pos status)
  314. "Write the feed status to the FEEDSTATUS drawer."
  315. (save-excursion
  316. (goto-char pos)
  317. (let ((end (save-excursion (org-end-of-subtree t t)))
  318. guid)
  319. (if (re-search-forward "^[ \t]*:FEEDSTATUS:[ \t]*\n" end t)
  320. (progn
  321. (goto-char (match-end 0))
  322. (delete-region (point)
  323. (save-excursion
  324. (and (re-search-forward "^[ \t]*:END:" nil t)
  325. (match-beginning 0)))))
  326. (outline-next-heading)
  327. (insert " :FEEDSTATUS:\n :END:\n")
  328. (beginning-of-line 0))
  329. (insert (pp-to-string status)))))
  330. (defun org-feed-add-items (pos entries)
  331. "Add the formatted items to the headline as POS."
  332. (let (entry level)
  333. (save-excursion
  334. (goto-char pos)
  335. (unless (looking-at org-complex-heading-regexp)
  336. (error "Wrong position"))
  337. (setq level (org-get-valid-level (length (match-string 1)) 1))
  338. (org-end-of-subtree t t)
  339. (skip-chars-backward " \t\n")
  340. (beginning-of-line 2)
  341. (setq pos (point))
  342. (while (setq entry (pop entries))
  343. (insert "\n")
  344. (org-paste-subtree level (nth 1 entry)))
  345. (org-mark-ring-push pos))))
  346. (defun org-feed-format-entry (entry template formatter)
  347. "Format ENTRY so that it can be inserted into an Org file.
  348. ENTRY is a property list. This function adds a `:formatted-for-org' property
  349. and returns the full property list.
  350. If that property is already present, nothing changes."
  351. (if formatter
  352. (funcall formatter entry)
  353. (let (dlines fmt tmp indent time
  354. v-h v-t v-T v-u v-U v-a)
  355. (setq dlines (org-split-string (or (plist-get entry :description) "???")
  356. "\n")
  357. v-h (or (plist-get entry :title) (car dlines) "???")
  358. time (or (if (plist-get entry :pubDate)
  359. (org-read-date t t (plist-get entry :pubDate)))
  360. (current-time))
  361. v-t (format-time-string (org-time-stamp-format nil nil) time)
  362. v-T (format-time-string (org-time-stamp-format t nil) time)
  363. v-u (format-time-string (org-time-stamp-format nil t) time)
  364. v-U (format-time-string (org-time-stamp-format t t) time)
  365. v-a (if (setq tmp (or (and (plist-get entry :guid-permalink)
  366. (plist-get entry :guid))
  367. (plist-get entry :link)))
  368. (concat "[[" tmp "]]\n")
  369. ""))
  370. (with-temp-buffer
  371. (insert template)
  372. (goto-char (point-min))
  373. (while (re-search-forward "%\\([a-zA-Z]+\\)" nil t)
  374. (setq name (match-string 1))
  375. (cond
  376. ((member name '("h" "t" "T" "u" "U" "a"))
  377. (replace-match (symbol-value (intern (concat "v-" name))) t t))
  378. ((setq tmp (plist-get entry (intern (concat ":" name))))
  379. (save-excursion
  380. (save-match-data
  381. (beginning-of-line 1)
  382. (when (looking-at (concat "^\\([ \t]*\\)%" name "[ \t]*$"))
  383. (setq tmp (org-feed-make-indented-block
  384. tmp (org-get-indentation))))))
  385. (replace-match tmp t t))))
  386. (buffer-string)))))
  387. (defun org-feed-make-indented-block (s n)
  388. "Add indentaton of N spaces to a multiline string S."
  389. (if (not (string-match "\n" s))
  390. s
  391. (mapconcat 'identity
  392. (org-split-string s "\n")
  393. (concat "\n" (make-string n ?\ )))))
  394. (defun org-feed-get-feed (url)
  395. "Get the RSS feed file at URL and return the buffer."
  396. (cond
  397. ((eq org-feed-retrieve-method 'url-retrieve-synchronously)
  398. (url-retrieve-synchronously url))
  399. ((eq org-feed-retrieve-method 'curl)
  400. (ignore-errors (kill-buffer org-feed-buffer))
  401. (call-process "curl" nil org-feed-buffer nil url)
  402. org-feed-buffer)
  403. ((eq org-feed-retrieve-method 'wget)
  404. (ignore-errors (kill-buffer org-feed-buffer))
  405. (call-process "curl" nil org-feed-buffer nil "-q" "-O" "-" url)
  406. org-feed-buffer)
  407. ((functionp org-feed-retrieve-method)
  408. (funcall org-feed-retrieve-method url))))
  409. (defun org-feed-parse-feed (buffer)
  410. "Parse BUFFER for RS feed entries.
  411. Returns a list of entries, with each entry a property list,
  412. containing the properties `:guid' and `:item-full-text'."
  413. (let (entries beg end item guid entry)
  414. (with-current-buffer buffer
  415. (widen)
  416. (goto-char (point-min))
  417. (while (re-search-forward "<item>" nil t)
  418. (setq beg (point)
  419. end (and (re-search-forward "</item>" nil t)
  420. (match-beginning 0)))
  421. (setq item (buffer-substring beg end)
  422. guid (if (string-match "<guid\\>.*?>\\(.*?\\)</guid>" item)
  423. (org-match-string-no-properties 1 item)))
  424. (setq entry (list :guid guid :item-full-text item))
  425. (push entry entries)
  426. (widen)
  427. (goto-char end))
  428. (nreverse entries))))
  429. (defun org-feed-parse-entry (entry)
  430. "Parse the `:item-full-text' field for xml tags and create new properties."
  431. (with-temp-buffer
  432. (insert (plist-get entry :item-full-text))
  433. (goto-char (point-min))
  434. (while (re-search-forward "<\\([a-zA-Z]+\\>\\).*?>\\([^\000]*?\\)</\\1>"
  435. nil t)
  436. (setq entry (plist-put entry
  437. (intern (concat ":" (match-string 1)))
  438. (match-string 2))))
  439. (goto-char (point-min))
  440. (unless (re-search-forward "isPermaLink[ \t]*=[ \t]*\"false\"" nil t)
  441. (setq entry (plist-put entry :guid-permalink t))))
  442. entry)
  443. (provide 'org-feed)
  444. ;;; org-feed.el ends here
  445. ;; arch-tag: 0929b557-9bc4-47f4-9633-30a12dbb5ae2
  446. ;1. parse all items
  447. ;2. filter with user filter
  448. ;3. Remove GUIDs that we have already *added* before
  449. ;4. Format, using user or built-in formatter
  450. ;5. add new items
  451. ;6. Store the guids from step 2, after the filtering
  452. ; This means that the feed could go back, have the entry
  453. ; pass the filter, and then it will be added.;
  454. ;Each item will be added once, when it first passes the filter.