Skip to content

Instantly share code, notes, and snippets.

@jonallured
Created February 5, 2015 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonallured/1b8c2b43619bca2f05dc to your computer and use it in GitHub Desktop.
Save jonallured/1b8c2b43619bca2f05dc to your computer and use it in GitHub Desktop.
Add common feed element
Feedjira::Feed.add_common_feed_element("image")
feed = Feedjira::Feed.fetch_and_parse("http://cltampa.com/tampa/Rss.xml?section=2065818")
feed.image
# => http://cltampa.com/binary/9697/adminIcon_clTampa.jpg
@sullysaurus
Copy link

Awesome thanks! How would I specific which image to grab? Each post that I am iterating through starts with an image, trying to display each one of those for each post.

@sullysaurus
Copy link

The image I need is nested inside @summary, which Feedjira gives me.

@jonallured
Copy link
Author

Gotcha - that's different. The way the feed author did this, Feedjira can't help you much. The summary attribute is just a simple Ruby string. We can parse it with Nokogiri and get at the images - maybe something like this:

feed = Feedjira::Feed.fetch_and_parse("http://cltampa.com/tampa/Rss.xml?section=2065818")
entry_docs = feed.entries.map { |entry| Nokogiri::XML entry.summary }
img_srcs = entry_docs.map { |doc| doc.xpath('/img/@src').first.value }
# => ["http://cltampa.com/imager/b/toc/5128677/97fd/pn.jpg",
#  "http://cltampa.com/imager/b/toc/5128539/0765/fiftyshades.jpg",
#  "http://cltampa.com/imager/b/toc/5117063/2692/VW1A8654.jpg",
#  "http://cltampa.com/imager/b/toc/5114867/0fe2/ATL_1Yolie_Capin_copy.jpg",
#  "http://cltampa.com/imager/b/toc/5113116/55a2/warriordashathletes.jpg",
#  "http://cltampa.com/imager/b/toc/5114241/108f/Screen_Shot_2015-01-28_at_10.38.13_AM.png",
#  "http://cltampa.com/imager/b/toc/5108127/be95/localtopia2.jpg",
#  "http://cltampa.com/imager/b/toc/5109466/a1a2/_DSC_0413.jpg",
#  "http://cltampa.com/imager/b/toc/5108628/034e/news_PN_hawaii_012215.jpg",
#  "http://cltampa.com/imager/b/toc/5108149/ea4a/midler.jpg",
#  "http://cltampa.com/imager/b/toc/5108105/56f6/TheDali_Sacrament_of_the_Last_Supper.jpg",
#  "http://cltampa.com/imager/b/toc/5104250/c1e6/Dynasty_on_7th_11.jpg",
#  "http://cltampa.com/imager/b/toc/5103499/e0db/poetsnotebook.jpg",
#  "http://cltampa.com/imager/b/toc/5103478/08bd/news_D_S_011515_jackson.png",
#  "http://cltampa.com/imager/b/toc/5101778/1f67/10818304_10205505141333734_3597687587416281929_o__1_.jpg",
#  "http://cltampa.com/imager/b/toc/5098631/361f/lockliberation1teaser.jpg.jpg",
#  "http://cltampa.com/imager/b/toc/5098476/7323/bike_life_TBWBC_girl_and_dog_on_bike_010815.jpg",
#  "http://cltampa.com/imager/b/toc/5098214/42db/Stock_photo_4.jpg",
#  "http://cltampa.com/imager/b/toc/5093356/2c79/LAWBI1.jpg",
#  "http://cltampa.com/imager/b/toc/5088458/668d/Kwanzaa-Myers.jpg",
#  "http://cltampa.com/imager/b/toc/5088392/9f7a/news_pn_122514.jpg",
#  "http://cltampa.com/imager/b/toc/5087870/9e36/pubnote2.jpg",
#  "http://cltampa.com/imager/b/toc/5084210/0824/news_PN_confidence_121814.jpg",
#  "http://cltampa.com/imager/b/toc/5079355/a234/topgolfBay.jpg",
#  "http://cltampa.com/imager/b/toc/5074054/6cfe/new_tease.jpg"]

It's not pretty, but it works. Any other thoughts on this?
Jon

@sullysaurus
Copy link

Sweet, thank you so much. Last question, would it be possible to just retain all of the html formatting that the authors used?

@jonallured
Copy link
Author

Hmmm, not sure I get your meaning - could you provide an example of what's not working? The summary does have HTML in it, so I think you're good to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment