parser.ex 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. defmodule PodcastFeed.Provider.Archive.Parser do
  2. def feed(url) do
  3. url
  4. |> fetch_xml()
  5. |> parse()
  6. |> IO.inspect()
  7. |> filter_mp3()
  8. |> compose()
  9. end
  10. defp fetch_xml(url) do
  11. {:ok, {_, _, xml}} = :httpc.request(:get, {url, []}, [], [body_format: :binary])
  12. xml
  13. end
  14. defp parse(xml) do
  15. xml |> XmlToMap.naive_map() |> Map.get("files") |> Map.get("file") |> Enum.map(fn f -> Map.get(f, "#content") |> Map.put("filename", Map.get(f, "-name")) end)
  16. end
  17. defp filter_mp3(files) do
  18. files |> Enum.filter(fn f -> Map.get(f, "format") =~ ~r/MP3/i end)
  19. end
  20. defp compose(files) do
  21. files
  22. |> Enum.map(&to_feed_item/1)
  23. end
  24. # <item>
  25. # <title>Episode Name 2</title>
  26. # <link>
  27. # http://podcast.example.com/episode2.mp4
  28. # </link>
  29. # <pubDate>Sat, 02 Jan 2016 16:00:00 PDT</pubDate>
  30. # <description>
  31. # The full length episode 2 description
  32. # </description>
  33. # <enclosure url="http://podcasts.example.com/episode.mp4" length="36715125" type="audio/mpeg"/>
  34. # <guid>
  35. # http://podcast.example.com/episode2.mp4
  36. # </guid>
  37. # <itunes:duration>19:07</itunes:duration>
  38. # <itunes:summary>
  39. # The full length episode 2 description
  40. # </itunes:summary>
  41. # <itunes:image href="http://www.example.com/image3000x3000.png"/>
  42. # <itunes:keywords>
  43. # comma,separated,key,words
  44. # </itunes:keywords>
  45. # <itunes:explicit>no</itunes:explicit>
  46. # </item>
  47. # %{
  48. # "album" => "Incontri al Piano Terra",
  49. # "artist" => "APE Milano",
  50. # "crc32" => "f1820595",
  51. # "creator" => "APE Milano",
  52. # "format" => "VBR MP3",
  53. # "genre" => "podcast",
  54. # "height" => "0",
  55. # "length" => "3943.31",
  56. # "md5" => "9ca26043a3e82e6f86c3a9309b88f4f5",
  57. # "mtime" => "1590154757",
  58. # "sha1" => "dcacfa46fcad1d656312784ad06886b5614c6420",
  59. # "size" => "47148690",
  60. # "title" => "Presentazione di Montagna femminile plurale con N1DM",
  61. # "track" => "03",
  62. # "width" => "0"
  63. # }
  64. defp to_feed_item(file) do
  65. %{
  66. title: file |> Map.get("title"),
  67. link: "http://archive.org/download/incontri-a-piano-terra/" <> (file |> Map.get("filename")) |> URI.encode(), #FIXME:! identifier should by dynamic
  68. pubDate: file |> Map.get("mtime") |> Integer.parse() |> elem(0) |> DateTime.from_unix!(:second),
  69. description: "",
  70. length: (file |> Map.get("length") |> Float.parse() |> elem(0)) * 100,
  71. guid: "",
  72. duration: "",
  73. summary: "",
  74. image: "",
  75. keywords: file |> Map.take(["album", "artist", "genre"]) |> Map.values(),
  76. explicit: "no",
  77. }
  78. end
  79. end
  80. # <?xml version="1.0" encoding="utf-8"?>
  81. # <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:itunesu="http://www.itunesu.com/feed" version="2.0">
  82. # <channel>
  83. # <link>http://www.YourSite.com</link>
  84. # <language>en-us</language>
  85. # <copyright>&#xA9;2013</copyright>
  86. # <webMaster>your@email.com (Your Name)</webMaster>
  87. # <managingEditor>your@email.com (Your Name)</managingEditor>
  88. # <image>
  89. # <url>http://www.YourSite.com/ImageSize300X300.jpg</url>
  90. # <title>Title or description of your logo</title>
  91. # <link>http://www.YourSite.com</link>
  92. # </image>
  93. # <itunes:owner>
  94. # <itunes:name>Your Name</itunes:name>
  95. # <itunes:email>your@email.com</itunes:email>
  96. # </itunes:owner>
  97. # <itunes:category text="Education">
  98. # <itunes:category text="Higher Education" />
  99. # </itunes:category>
  100. # <itunes:keywords>separate, by, comma, and, space</itunes:keywords>
  101. # <itunes:explicit>no</itunes:explicit>
  102. # <itunes:image href="http://www.YourSite.com/ImageSize300X300.jpg" />
  103. # <atom:link href="http://www.YourSite.com/feed.xml" rel="self" type="application/rss+xml" />
  104. # <pubDate>Sun, 01 Jan 2012 00:00:00 EST</pubDate>
  105. # <title>Verbose title of the podcast</title>
  106. # <itunes:author>College, school, or department owning the podcast</itunes:author>
  107. # <description>Verbose description of the podcast.</description>
  108. # <itunes:summary>Duplicate of above verbose description.</itunes:summary>
  109. # <itunes:subtitle>Short description of the podcast - 255 character max.</itunes:subtitle>
  110. # <lastBuildDate>Thu, 02 Feb 2012 00:00:00 EST</lastBuildDate>
  111. # <item>
  112. # <title>Verbose title of the episode</title>
  113. # <description>Verbose description of the episode.</description>
  114. # <itunes:summary>Duplicate of above verbose description.</itunes:summary>
  115. # <itunes:subtitle>Short description of the episode - 255 character max.</itunes:subtitle>
  116. # <itunesu:category itunesu:code="112" />
  117. # <enclosure url="http://www.YourSite.com/FILE.EXT" type="audio/mpeg" length="1" />
  118. # <guid>http://www.YourSite.com/FILE.EXT</guid>
  119. # <itunes:duration>H:MM:SS</itunes:duration>
  120. # <pubDate>Thu, 02 Feb 2012 00:00:00 EST</pubDate>
  121. # </item>
  122. # </channel>
  123. # </rss>