Atoms blog About all, About nothing, About life, About computers

12Jul/081

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.

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]/"div[1]").remove
(docs[num]/"br").remove
link = (docs[num]/"a").last.to_s
linkid = link.gsub(/<\/?[^>]*>/, "")
linkid = linkid.split('#')
linktopage = 'http://www.millionreasonswhylatviaisthebestcountryintheworld.com/lv/' + linkid[1]
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"
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

novelkamā veidā šeit

Filed under: code Leave a comment
Comments (1) Trackbacks (1)
  1. Paldies! ;)

    Pārlūks: Firefox 2.0.0.15 Firefox 2.0.0.15 OS: Windows XP Windows XP

Leave a comment

(required)