Hi all!
I'm a complete beginner at this so please bear with me...I dont program
at all but if I need to then I have been trying to use Ruby.
I made a hash with some URL's and i'm looping through this hash and
using the url stored there and getting some info from a site that i'm
storing into a file. Im attaching my code. This actually works
Now I would like to store the information in a db table. So I would like
to modify my code. I created an sqlite3 db with two tables called 'urls'
and 'stats'. 'urls' have the columns 'ID','Description', 'URL' and
'stats' have the columns 'ID','Date','Title'.
Now, I need to connect to my database and fetch this data. I have used
DataMapper for this.
My questions:
Should I query the rows one by one?
Should I query the whole table store into a hash and the loop through?
When I fetch the row how can I get the column URL?
My working code:
require 'rubygems'
require 'mechanize'
url = { "1" => "http://www.ruby-forum.com/",
"2" => "http://www.stackoverflow.com/",
"3" => "http://www.ruby-doc.org/"
}
time=Time.now.strftime("%Y-%m-%d %H:%M")
File.open("Stats_Test.txt","a+") do |vStats|
#Loop - hash
url.each do |number,url|
agent = Mechanize.new
page = agent.get(url)
title = agent.page.title.strip
vStats.puts "#{time} #{number} #{title}\n"
end
end
My modified code so far:
require 'rubygems'
require 'mechanize'
require 'data_mapper'
#Sqlite3 connection
DataMapper.setup(:default, 'sqlite:///Ruby/Stats/StatsDB.sqlite')
class Url
include DataMapper::Resource
end
class Stats
include DataMapper::Resource
end
time=Time.now.strftime("%Y-%m-%d %H:%M")
int = 0
num = 3
#What to do here?
while int<num do
ds = Url.get(int)
#ds has the first row. Now how do I get the column 'URL'?
agent = Mechanize.new
page = agent.get(ds:url) #I want to give it the url from the table
title = agent.page.title.strip #get the title from website
#write title to database table stats
#Havent thgought of this yet
end
Any suggestions are apreciated!
Br
cristian
···
--
Posted via http://www.ruby-forum.com/.