| 1 | module BakaReception |
|---|
| 2 | def wget_entry(bakaid) |
|---|
| 3 | require 'net/http' |
|---|
| 4 | Net::HTTP.version_1_2 |
|---|
| 5 | Net::HTTP.start(BakaAddress, BakaPort) do |http| |
|---|
| 6 | path = BakaPath + bakaid |
|---|
| 7 | http.request_get( path ) |
|---|
| 8 | end |
|---|
| 9 | end |
|---|
| 10 | |
|---|
| 11 | def create_or_update_entry(bakaid) |
|---|
| 12 | print bakaid |
|---|
| 13 | response = wget_entry(bakaid) |
|---|
| 14 | print " => #{response.code}" |
|---|
| 15 | unless response.code == "200" |
|---|
| 16 | puts |
|---|
| 17 | return nil |
|---|
| 18 | end |
|---|
| 19 | entry = Entry.find(:first, :conditions => ['bakaid = ?', bakaid]) |
|---|
| 20 | if entry |
|---|
| 21 | entry.body = response.body |
|---|
| 22 | entry.save |
|---|
| 23 | puts " => update" |
|---|
| 24 | else |
|---|
| 25 | entry = entries.create(:bakaid => bakaid, :body => response.body) |
|---|
| 26 | puts " => create" |
|---|
| 27 | end |
|---|
| 28 | entry |
|---|
| 29 | end |
|---|
| 30 | |
|---|
| 31 | def wget_entries(max = 30) |
|---|
| 32 | if last_entry = Entry.find(:first, :order => "bakaid DESC") |
|---|
| 33 | last_entry.bakaid =~ /^(\d\d\d\d)(\d\d)(\d\d)/ |
|---|
| 34 | first_year, first_month, first_day = $1.to_i, $2.to_i, $3.to_i |
|---|
| 35 | else |
|---|
| 36 | first_year, first_month, first_day = BeginningDate |
|---|
| 37 | end |
|---|
| 38 | t = Time.now |
|---|
| 39 | last_year, last_month, last_day = t.year, t.mon, t.day |
|---|
| 40 | |
|---|
| 41 | counter = 0 |
|---|
| 42 | (first_year..last_year).each do |y| |
|---|
| 43 | (1..12).each do |m| |
|---|
| 44 | next if y == first_year && m < first_month |
|---|
| 45 | break if y == last_year && m > last_month |
|---|
| 46 | (1..31).each do |d| |
|---|
| 47 | next if y == first_year && m == first_month && d < first_day |
|---|
| 48 | break if y == last_year && m == last_month && d > last_day |
|---|
| 49 | (0..100).each do |n| |
|---|
| 50 | return 0 if counter >= max |
|---|
| 51 | bakaid = sprintf "%04d%02d%02d", y, m, d |
|---|
| 52 | bakaid += n.to_s if n > 0 |
|---|
| 53 | #next if Entry.find(:first, :conditions => ['bakaid = ?', bakaid]) |
|---|
| 54 | counter += 1 |
|---|
| 55 | break unless create_or_update_entry(bakaid) |
|---|
| 56 | end |
|---|
| 57 | end |
|---|
| 58 | end |
|---|
| 59 | end |
|---|
| 60 | end |
|---|
| 61 | end |
|---|
| 62 | |
|---|
| 63 | def baka_reader(n = nil) |
|---|
| 64 | @reception = Reception.create |
|---|
| 65 | @reception.extend BakaReception |
|---|
| 66 | @reception.wget_entries(n) |
|---|
| 67 | @reception.destroy if 0 == @reception.entries.count |
|---|
| 68 | end |
|---|
| 69 | |
|---|
| 70 | def srcdir |
|---|
| 71 | File.dirname(File.expand_path(__FILE__)).untaint |
|---|
| 72 | end |
|---|
| 73 | |
|---|
| 74 | $LOAD_PATH.unshift srcdir |
|---|
| 75 | require 'myutil' |
|---|
| 76 | setup_environment |
|---|
| 77 | |
|---|
| 78 | n = ARGV[0].to_i |
|---|
| 79 | n = 20 if n <= 0 |
|---|
| 80 | |
|---|
| 81 | ActiveRecord::Base.transaction do |
|---|
| 82 | baka_reader n |
|---|
| 83 | end |
|---|