読者です 読者をやめる 読者になる 読者になる

今日作った

Ruby DB
#! /bin/env ruby

require 'sqlite3'

SQLITEFILE = "/home/hoge/crawler/crawler.db"

def create_db
  sql =<<-SQL
CREATE TABLE IF NOT EXISTS crawler(
  id integer primary key,
  name text,
  path text,
  created_at TIMESTAMP DEFAULT (DATETIME('now','localtime'))
);
SQL
  db = SQLite3::Database.new(SQLITEFILE)
  db.execute sql
  db.close
end

def insert path
  sql =<<-SQL
insert into crawler(name,path) values(:name,:path)
SQL
  db = SQLite3::Database.new(SQLITEFILE)
  db.execute sql,{ :name => (File.basename path) ,:path => path }
  db.close
  puts "insert #{path}"
end

def exists? path
  sql =<<-SQL
select * from crawler where name like :name
SQL
  db = SQLite3::Database.new(SQLITEFILE)
  result = db.execute sql,{ :name =>  "%" + (File.basename path) + "%" }
  db.close
  return result.size != 0
end

def glob_xxx
  filelist = []
  Dir.glob("/path/to/file/**/*.xxx").each {|f|
    filelist << f
  }
  filelist
end

def glob_ext
  filelist = []
  Dir.glob("/path/to/file/**/*.ext").each {|f|
    filelist << f
  }
  filelist
end

#---------------
# main

create_db

filelist = glob_ext + glob_xxx

filelist.sort.each{|f|
  insert f unless exists? f
}