feed.ml 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. (*
  2. * Copyright (c) 2014, OCaml.org project
  3. * Copyright (c) 2015 KC Sivaramakrishnan <sk826@cl.cam.ac.uk>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. *)
  17. type source = { name : string; url : string }
  18. type content = Atom of Syndic.Atom.feed | Rss2 of Syndic.Rss2.channel
  19. let string_of_feed = function Atom _ -> "Atom" | Rss2 _ -> "Rss2"
  20. type t = { name : string; title : string; url : string; content : content }
  21. let classify_feed ~xmlbase (xml : string) =
  22. try Atom (Syndic.Atom.parse ~xmlbase (Xmlm.make_input (`String (0, xml))))
  23. with Syndic.Atom.Error.Error _ -> (
  24. try Rss2 (Syndic.Rss2.parse ~xmlbase (Xmlm.make_input (`String (0, xml))))
  25. with Syndic.Rss2.Error.Error _ -> failwith "Neither Atom nor RSS2 feed")
  26. let fetch (source : source) =
  27. let xmlbase = Uri.of_string @@ source.url in
  28. let response = Http.get source.url in
  29. let content = classify_feed ~xmlbase response in
  30. let title =
  31. match content with
  32. | Atom atom -> Util.string_of_text_construct atom.Syndic.Atom.title
  33. | Rss2 ch -> ch.Syndic.Rss2.title
  34. in
  35. { name = source.name; title; content; url = source.url }