Million Reasons Why Latvia Is The Best Country In The World RSS barotne

Šai lapai sen jau vajadzēja rss barotni, par cik pati neuzradās, tad izdomāju paspēlēties drusku ar Ruby, uztaisīju konsoles skriptu, kurš krontabā reizi 10 minūtēs uzģenerē jaunu index.xml. Protams neceru ka scripts ir baigi pareizs, bet vismaz izdara to kas vajadzīgs. Ja tu saproti Ruby, tad norādi man uz kļūdām, scripts tālāk.

[source:ruby]

require ‘rubygems’

require ‘open-uri’

require ‘hpricot’

require ‘rss/maker’

@url = “http://www.millionreasonswhylatviaisthebestcountryintheworld.com/lv/a/n/”

@response = ”

begin

open(@url, “User-Agent” => “Ruby/#{RUBY_VERSION}”,

“From” => “atoms@tups.lv”,

“Referer” => “http://tups.lv/blog/”) { |f|

@response = f.read

}

doc = Hpricot(@response)

docs = []

doks = []

links = []

docs = (doc/”div.ent”)

10.times do | num |

(docs[num]/”div.val”).remove

(docs[num]/”div.info”).remove

(docs[num]/”div1”).remove

(docs[num]/”br”).remove

link = (docs[num]/”a”).last.to_s

linkid = link.gsub(/<\/?[^>]*>/, “”)

linkid = linkid.split(‘#’)

linktopage = ‘http://www.millionreasonswhylatviaisthebestcountryintheworld.com/lv/&#8217; + linkid1

links << linktopage

doks << docs[num]

end

version = “2.0” # [“0.9”, “1.0”, “2.0”]

destination = “index.xml” # local file to write

content = RSS::Maker.make(version) do |m|

m.channel.title = “Million Reasons Why Latvia Is The Best Country In The World”

m.channel.link = “http://www.millionreasonswhylatviaisthebestcountryintheworld.com&#8221;

m.channel.description = “Million Reasons Why Latvia Is The Best Country In The World”

10.times do | nn |

i = m.items.new_item

i.title = “Million Reasons Why Latvia Is The Best Country In The World”

i.link = links[nn]

i.description = doks[nn]

end

end

File.open(destination,”w”) do |f|

f.write(content)

end

rescue Exception => e

print e, “\n”

end

[/source]

novelkamā veidā šeit

 
comments powered by Disqus