manukall / feeder_ex

Wrapper for feeder. Elixir RSS parser
70 stars 13 forks source link

Enumerating through Channel then Posts #1

Open ghost opened 9 years ago

ghost commented 9 years ago

Apologies, Elixir noob trying to port something from C#.

Once we have our successfully interpreted RSS feed, how do we enumerate through each post adding the data to a map (title and url specifically, not that it makes much difference)

-Wil

shymega commented 7 years ago

Hi there! Sorry to see the long wait you've had, and I hope this response isn't too late for you.

I'm working on a RSS aggregator in my free time, and I had this exact problem. Here's a example piece of code for you, which adds the title & link to a Map.

    HTTPoison.start()

    feed_url = "https://www.theguardian.com/uk/rss"

    {:ok, %HTTPoison.Response{body: body}} = HTTPoison.get(feed_url)
    {:ok, feed, _} = FeederEx.parse(body)

    IO.puts("Feed title: #{feed.title}")

    rss_map = Enum.map(feed.entries, fn (entry) -> %{:title => entry.title,
                                                 :link => entry.link} end)
    |> Enum.uniq()

    IO.inspect(rss_map)

This code will enumerate over the feed items returned from The Guardian (UK) newspaper, and return a Map in List form, which you can then enumerate over to retrieve each item, which should, fingers crossed, have a title and link key and respective value.

I hope this helps, Will.