RSS file reading/parsing and Connection timed out – connect(2) [resolved]


This post is about

1. Read Rss feed
2. Time out problem handling
3. Some explaination about RSS reading


def get_leonardo_hotels
   url, timeout =  'http://www.leonardo-hotels.mobi/rss.aspx?lang=eng', 60
   uri = URI.parse(url)
   http = Net::HTTP.new(uri.host, uri.port)
   http.open_timeout = timeout 
   http.read_timeout = timeout 
   # http.use_ssl = (uri.scheme == 'https')
   http.request_get(uri.request_uri) do |response|
     data = response.read_body
     data = RSS::Parser.parse(data, false)
     # puts data.channel.item.inspect
     data.channel.items.each do |item|
        puts "------->"
        puts item.title
        puts "------->"
     end

   end
end

All in all the above function is used for RSS reading and handling time out problem.

Time out increased by setting http open_timeout and read_timeout to 60 seconds


url, timeout =  'http://www.leonardo-hotels.mobi/rss.aspx?lang=eng', 60
#code here
http.open_timeout = timeout 
http.read_timeout = timeout 

To read RSS Feed


data = response.read_body
data = RSS::Parser.parse(data, false)
data.channel.items.each do |item|
   puts item.inspect
end

Reference:
Time out links

Http open_timeout and read_timeout
Rss feed links

http://www.robbyonrails.com/articles/2005/05/11/parsing-a-rss-feed

http://www.webmantras.com/2010/07/23/rails-rss-feed-reader-example/

RSS understanding

<rss version=”2.0″>
<channel>
<title>…</title>
<link>…</link>
<description>…</description>
<copyright>…</copyright>
<ttl>20</ttl>
<lastBuildDate>…</lastBuildDate>
<item>…</item>
<item>…</item>
<item>…</item>
<item>
<lang>
<![CDATA[ eng ]]>
</lang>
<id>
<![CDATA[ 246 ]]>
</id>
<title>
<![CDATA[ Leonardo Plaza Hotel Tiberias ]]>
</title>
<url>
<![CDATA[
http://www.leonardo-hotels.mobi/octopus/Upload/ResortImages/LeoPlazaTib – Superior Room 255×130.jpg
]]>
</url>
</item>
<item>…</item>
<item>…</item>
</channel>
</rss>

Note:

1. In above rss there is no <items> tag but it is important to note that you are getting list of them using items.

2. You will also skip node when reading data

Advertisements

I am Senior Software Engineer. I love reading, writing, sharing,developing, hiking, movies, trips, mountains, brooks, hills etc.

Tagged with: , , , , , ,
Posted in RSS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

StackOverFlow
Categories
Archives
%d bloggers like this: