org-feed.el 25 KB

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