org-feed.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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 of entries,
  152. each being a property list containing the `:guid' and `:item-full-text'
  153. keys. The default is `org-feed-parse-rss-feed'; `org-feed-parse-atom-feed'
  154. is an 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. (defun org-feed-unescape (s)
  244. "Unescape protected entities in S."
  245. (let ((re (concat "&\\("
  246. (mapconcat (lambda (e)
  247. (car e)) xml-entity-alist "\\|")
  248. "\\);")))
  249. (while (string-match re s)
  250. (setq s (replace-match
  251. (cdr (assoc (match-string 1 s) xml-entity-alist)) nil nil s)))
  252. s))
  253. ;;;###autoload
  254. (defun org-feed-update-all ()
  255. "Get inbox items from all feeds in `org-feed-alist'."
  256. (interactive)
  257. (let ((nfeeds (length org-feed-alist))
  258. (nnew (apply '+ (mapcar 'org-feed-update org-feed-alist))))
  259. (message "%s from %d %s"
  260. (cond ((= nnew 0) "No new entries")
  261. ((= nnew 1) "1 new entry")
  262. (t (format "%d new entries" nnew)))
  263. nfeeds
  264. (if (= nfeeds 1) "feed" "feeds"))))
  265. ;;;###autoload
  266. (defun org-feed-update (feed &optional retrieve-only)
  267. "Get inbox items from FEED.
  268. FEED can be a string with an association in `org-feed-alist', or
  269. it can be a list structured like an entry in `org-feed-alist'."
  270. (interactive (list (org-completing-read "Feed name: " org-feed-alist)))
  271. (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
  272. (unless feed
  273. (error "No such feed in `org-feed-alist"))
  274. (catch 'exit
  275. (let ((name (car feed))
  276. (url (nth 1 feed))
  277. (file (nth 2 feed))
  278. (headline (nth 3 feed))
  279. (filter (nth 1 (memq :filter feed)))
  280. (formatter (nth 1 (memq :formatter feed)))
  281. (new-handler (nth 1 (memq :new-handler feed)))
  282. (changed-handler (nth 1 (memq :changed-handler feed)))
  283. (template (or (nth 1 (memq :template feed))
  284. org-feed-default-template))
  285. (drawer (or (nth 1 (memq :drawer feed))
  286. org-feed-drawer))
  287. (parse-feed (or (nth 1 (memq :parse-feed feed))
  288. 'org-feed-parse-rss-feed))
  289. (parse-entry (or (nth 1 (memq :parse-entry feed))
  290. 'org-feed-parse-rss-entry))
  291. feed-buffer inbox-pos new-formatted
  292. entries old-status status new changed guid-alist e guid olds)
  293. (setq feed-buffer (org-feed-get-feed url))
  294. (unless (and feed-buffer (bufferp (get-buffer feed-buffer)))
  295. (error "Cannot get feed %s" name))
  296. (when retrieve-only
  297. (throw 'exit feed-buffer))
  298. (setq entries (funcall parse-feed feed-buffer))
  299. (ignore-errors (kill-buffer feed-buffer))
  300. (save-excursion
  301. (save-window-excursion
  302. (setq inbox-pos (org-feed-goto-inbox-internal file headline))
  303. (setq old-status (org-feed-read-previous-status inbox-pos drawer))
  304. ;; Add the "handled" status to the appropriate entries
  305. (setq entries (mapcar (lambda (e)
  306. (setq e (plist-put e :handled
  307. (nth 1 (assoc
  308. (plist-get e :guid)
  309. old-status)))))
  310. entries))
  311. ;; Find out which entries are new and which are changed
  312. (dolist (e entries)
  313. (if (not (plist-get e :handled))
  314. (push e new)
  315. (setq olds (nth 2 (assoc (plist-get e :guid) old-status)))
  316. (if (and olds
  317. (not (string= (sha1
  318. (plist-get e :item-full-text))
  319. olds)))
  320. (push e changed))))
  321. ;; Parse the relevant entries fully
  322. (setq new (mapcar parse-entry new)
  323. changed (mapcar parse-entry changed))
  324. ;; Run the filter
  325. (when filter
  326. (setq new (delq nil (mapcar filter new))
  327. changed (delq nil (mapcar filter new))))
  328. (when (not (or new changed))
  329. (message "No new items in feed %s" name)
  330. (throw 'exit 0))
  331. ;; Get alist based on guid, to look up entries
  332. (setq guid-alist
  333. (append
  334. (mapcar (lambda (e) (list (plist-get e :guid) e)) new)
  335. (mapcar (lambda (e) (list (plist-get e :guid) e)) changed)))
  336. ;; Construct the new status
  337. (setq status
  338. (mapcar
  339. (lambda (e)
  340. (setq guid (plist-get e :guid))
  341. (list guid
  342. ;; things count as handled if we handle them now,
  343. ;; or if they were handled previously
  344. (if (assoc guid guid-alist) t (plist-get e :handled))
  345. ;; A hash, to detect changes
  346. (sha1 (plist-get e :item-full-text))))
  347. entries))
  348. ;; Handle new items in the feed
  349. (when new
  350. (if new-handler
  351. (progn
  352. (goto-char inbox-pos)
  353. (funcall new-handler new))
  354. ;; No custom handler, do the default adding
  355. ;; Format the new entries into an alist with GUIDs in the car
  356. (setq new-formatted
  357. (mapcar
  358. (lambda (e) (org-feed-format-entry e template formatter))
  359. new)))
  360. ;; Insert the new items
  361. (org-feed-add-items inbox-pos new-formatted))
  362. ;; Handle changed items in the feed
  363. (when (and changed-handler changed)
  364. (goto-char inbox-pos)
  365. (funcall changed-handler changed))
  366. ;; Write the new status
  367. ;; We do this only now, in case something goes wrong above, so
  368. ;; that would would end up with a status that does not reflect
  369. ;; which items truely have been handled
  370. (org-feed-write-status inbox-pos drawer status)
  371. ;; Normalize the visibility of the inbox tree
  372. (goto-char inbox-pos)
  373. (hide-subtree)
  374. (show-children)
  375. (org-cycle-hide-drawers 'children)
  376. ;; Hooks and messages
  377. (when org-feed-save-after-adding (save-buffer))
  378. (message "Added %d new item%s from feed %s to file %s, heading %s"
  379. (length new) (if (> (length new) 1) "s" "")
  380. name
  381. (file-name-nondirectory file) headline)
  382. (run-hooks 'org-feed-after-adding-hook)
  383. (length new))))))
  384. ;;;###autoload
  385. (defun org-feed-goto-inbox (feed)
  386. "Go to the inbox that captures the feed named FEED."
  387. (interactive
  388. (list (if (= (length org-feed-alist) 1)
  389. (car org-feed-alist)
  390. (org-completing-read "Feed name: " org-feed-alist))))
  391. (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
  392. (unless feed
  393. (error "No such feed in `org-feed-alist"))
  394. (org-feed-goto-inbox-internal (nth 2 feed) (nth 3 feed)))
  395. ;;;###autoload
  396. (defun org-feed-show-raw-feed (feed)
  397. "Show the raw feed buffer of a feed."
  398. (interactive
  399. (list (if (= (length org-feed-alist) 1)
  400. (car org-feed-alist)
  401. (org-completing-read "Feed name: " org-feed-alist))))
  402. (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
  403. (unless feed
  404. (error "No such feed in `org-feed-alist"))
  405. (switch-to-buffer
  406. (org-feed-update feed 'retrieve-only))
  407. (goto-char (point-min)))
  408. (defun org-feed-goto-inbox-internal (file heading)
  409. "Find or create HEADING in FILE.
  410. Switch to that buffer, and return the position of that headline."
  411. (find-file file)
  412. (widen)
  413. (goto-char (point-min))
  414. (if (re-search-forward
  415. (concat "^\\*+[ \t]+" heading "[ \t]*\\(:.*?:[ \t]*\\)?$")
  416. nil t)
  417. (goto-char (match-beginning 0))
  418. (goto-char (point-max))
  419. (insert "\n\n* " heading "\n\n")
  420. (org-back-to-heading t))
  421. (point))
  422. (defun org-feed-read-previous-status (pos drawer)
  423. "Get the alist of old GUIDs from the entry at POS.
  424. This will find DRAWER and extract the alist."
  425. (save-excursion
  426. (goto-char pos)
  427. (let ((end (save-excursion (org-end-of-subtree t t))))
  428. (if (re-search-forward
  429. (concat "^[ \t]*:" drawer ":[ \t]*\n\\([^\000]*?\\)\n[ \t]*:END:")
  430. end t)
  431. (read (match-string 1))
  432. nil))))
  433. (defun org-feed-write-status (pos drawer status)
  434. "Write the feed STATUS to DRAWER in entry at POS."
  435. (save-excursion
  436. (goto-char pos)
  437. (let ((end (save-excursion (org-end-of-subtree t t)))
  438. guid)
  439. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*\n")
  440. end t)
  441. (progn
  442. (goto-char (match-end 0))
  443. (delete-region (point)
  444. (save-excursion
  445. (and (re-search-forward "^[ \t]*:END:" nil t)
  446. (match-beginning 0)))))
  447. (outline-next-heading)
  448. (insert " :" drawer ":\n :END:\n")
  449. (beginning-of-line 0))
  450. (insert (pp-to-string status)))))
  451. (defun org-feed-add-items (pos entries)
  452. "Add the formatted items to the headline as POS."
  453. (let (entry level)
  454. (save-excursion
  455. (goto-char pos)
  456. (unless (looking-at org-complex-heading-regexp)
  457. (error "Wrong position"))
  458. (setq level (org-get-valid-level (length (match-string 1)) 1))
  459. (org-end-of-subtree t t)
  460. (skip-chars-backward " \t\n")
  461. (beginning-of-line 2)
  462. (setq pos (point))
  463. (while (setq entry (pop entries))
  464. (org-paste-subtree level entry 'yank))
  465. (org-mark-ring-push pos))))
  466. (defun org-feed-format-entry (entry template formatter)
  467. "Format ENTRY so that it can be inserted into an Org file.
  468. ENTRY is a property list. This function adds a `:formatted-for-org' property
  469. and returns the full property list.
  470. If that property is already present, nothing changes."
  471. (if formatter
  472. (funcall formatter entry)
  473. (let (dlines fmt tmp indent time name
  474. v-h v-t v-T v-u v-U v-a)
  475. (setq dlines (org-split-string (or (plist-get entry :description) "???")
  476. "\n")
  477. v-h (or (plist-get entry :title) (car dlines) "???")
  478. time (or (if (plist-get entry :pubDate)
  479. (org-read-date t t (plist-get entry :pubDate)))
  480. (current-time))
  481. v-t (format-time-string (org-time-stamp-format nil nil) time)
  482. v-T (format-time-string (org-time-stamp-format t nil) time)
  483. v-u (format-time-string (org-time-stamp-format nil t) time)
  484. v-U (format-time-string (org-time-stamp-format t t) time)
  485. v-a (if (setq tmp (or (and (plist-get entry :guid-permalink)
  486. (plist-get entry :guid))
  487. (plist-get entry :link)))
  488. (concat "[[" tmp "]]\n")
  489. ""))
  490. (with-temp-buffer
  491. (insert template)
  492. (goto-char (point-min))
  493. (while (re-search-forward "%\\([a-zA-Z]+\\)" nil t)
  494. (setq name (match-string 1))
  495. (cond
  496. ((member name '("h" "t" "T" "u" "U" "a"))
  497. (replace-match (symbol-value (intern (concat "v-" name))) t t))
  498. ((setq tmp (plist-get entry (intern (concat ":" name))))
  499. (save-excursion
  500. (save-match-data
  501. (beginning-of-line 1)
  502. (when (looking-at (concat "^\\([ \t]*\\)%" name "[ \t]*$"))
  503. (setq tmp (org-feed-make-indented-block
  504. tmp (org-get-indentation))))))
  505. (replace-match tmp t t))))
  506. (buffer-string)))))
  507. (defun org-feed-make-indented-block (s n)
  508. "Add indentation of N spaces to a multiline string S."
  509. (if (not (string-match "\n" s))
  510. s
  511. (mapconcat 'identity
  512. (org-split-string s "\n")
  513. (concat "\n" (make-string n ?\ )))))
  514. (defun org-feed-skip-http-headers (buffer)
  515. "Remove HTTP headers from BUFFER, and return it.
  516. Assumes headers are indeed present!"
  517. (with-current-buffer buffer
  518. (widen)
  519. (goto-char (point-min))
  520. (search-forward "\n\n")
  521. (delete-region (point-min) (point))
  522. buffer))
  523. (defun org-feed-get-feed (url)
  524. "Get the RSS feed file at URL and return the buffer."
  525. (cond
  526. ((eq org-feed-retrieve-method 'url-retrieve-synchronously)
  527. (org-feed-skip-http-headers (url-retrieve-synchronously url)))
  528. ((eq org-feed-retrieve-method 'curl)
  529. (ignore-errors (kill-buffer org-feed-buffer))
  530. (call-process "curl" nil org-feed-buffer nil "--silent" url)
  531. org-feed-buffer)
  532. ((eq org-feed-retrieve-method 'wget)
  533. (ignore-errors (kill-buffer org-feed-buffer))
  534. (call-process "wget" nil org-feed-buffer nil "-q" "-O" "-" url)
  535. org-feed-buffer)
  536. ((functionp org-feed-retrieve-method)
  537. (funcall org-feed-retrieve-method url))))
  538. (defun org-feed-parse-rss-feed (buffer)
  539. "Parse BUFFER for RSS feed entries.
  540. Returns a list of entries, with each entry a property list,
  541. containing the properties `:guid' and `:item-full-text'."
  542. (let ((case-fold-search t)
  543. entries beg end item guid entry)
  544. (with-current-buffer buffer
  545. (widen)
  546. (goto-char (point-min))
  547. (while (re-search-forward "<item\\>.*?>" nil t)
  548. (setq beg (point)
  549. end (and (re-search-forward "</item>" nil t)
  550. (match-beginning 0)))
  551. (setq item (buffer-substring beg end)
  552. guid (if (string-match "<guid\\>.*?>\\(.*?\\)</guid>" item)
  553. (org-match-string-no-properties 1 item)))
  554. (setq entry (list :guid guid :item-full-text item))
  555. (push entry entries)
  556. (widen)
  557. (goto-char end))
  558. (nreverse entries))))
  559. (defun org-feed-parse-rss-entry (entry)
  560. "Parse the `:item-full-text' field for xml tags and create new properties."
  561. (with-temp-buffer
  562. (insert (plist-get entry :item-full-text))
  563. (goto-char (point-min))
  564. (while (re-search-forward "<\\([a-zA-Z]+\\>\\).*?>\\([^\000]*?\\)</\\1>"
  565. nil t)
  566. (setq entry (plist-put entry
  567. (intern (concat ":" (match-string 1)))
  568. (org-feed-unescape (match-string 2)))))
  569. (goto-char (point-min))
  570. (unless (re-search-forward "isPermaLink[ \t]*=[ \t]*\"false\"" nil t)
  571. (setq entry (plist-put entry :guid-permalink t))))
  572. entry)
  573. (defun org-feed-parse-atom-feed (buffer)
  574. "Parse BUFFER for Atom feed entries.
  575. Returns a list of entries, with each entry a property list,
  576. containing the properties `:guid' and `:item-full-text'.
  577. The `:item-full-text' property actually contains the sexp
  578. formatted as a string, not the original XML data."
  579. (with-current-buffer buffer
  580. (widen)
  581. (let ((feed (car (xml-parse-region (point-min) (point-max)))))
  582. (mapcar
  583. (lambda (entry)
  584. (list
  585. :guid (car (xml-node-children (car (xml-get-children entry 'id))))
  586. :item-full-text (prin1-to-string entry)))
  587. (xml-get-children feed 'entry)))))
  588. (defun org-feed-parse-atom-entry (entry)
  589. "Parse the `:item-full-text' as a sexp and create new properties."
  590. (let ((xml (car (read-from-string (plist-get entry :item-full-text)))))
  591. ;; Get first <link href='foo'/>.
  592. (setq entry (plist-put entry :link
  593. (xml-get-attribute
  594. (car (xml-get-children xml 'link))
  595. 'href)))
  596. ;; Add <title/> as :title.
  597. (setq entry (plist-put entry :title
  598. (org-feed-unescape (car (xml-node-children
  599. (car (xml-get-children xml 'title)))))))
  600. (let* ((content (car (xml-get-children xml 'content)))
  601. (type (xml-get-attribute-or-nil content 'type)))
  602. (when content
  603. (cond
  604. ((string= type "text")
  605. ;; We like plain text.
  606. (setq entry (plist-put entry :description (org-feed-unescape (car (xml-node-children content))))))
  607. ((string= type "html")
  608. ;; TODO: convert HTML to Org markup.
  609. (setq entry (plist-put entry :description (org-feed-unescape (car (xml-node-children content))))))
  610. ((string= type "xhtml")
  611. ;; TODO: convert XHTML to Org markup.
  612. (setq entry (plist-put entry :description (prin1-to-string (xml-node-children content)))))
  613. (t
  614. (setq entry (plist-put entry :description (format "Unknown '%s' content." type)))))))
  615. entry))
  616. (provide 'org-feed)
  617. ;; arch-tag: 0929b557-9bc4-47f4-9633-30a12dbb5ae2
  618. ;;; org-feed.el ends here