hsbt / bakagaiku

Baka ga iku on rails

This URL has Read+Write access

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