OSDN Git Service

load arcs bowl
[tdcgexplorer/nimono-crawlers.git] / lib / bowl.rb
1 require 'net/http'
2
3 class Bowl
4   attr_reader :code
5
6   def src_path
7     "/Volumes/uploader/src/mmdbowl/#{code}.html"
8   end
9
10   def save_src_0
11     http = Net::HTTP.new('bowlroll.net')
12     http.start do
13       http.request_get("/up/#{code}") do |response|
14         open(src_path(code), 'wb') { |f|
15           response.read_body { |buf| f.write buf }
16         }
17       end
18     end
19   end
20
21   def save_src
22     if File.exist? src_path
23       return false
24     end
25     save_src_0
26     true
27   end
28
29   def initialize(code)
30     @code = code
31   end
32
33   def load
34     @source = IO.read(src_path)
35   end
36
37   def removed?
38     !!/削除され/.match(@source)
39   end
40
41   def locked?
42     !!/認証キー/.match(@source)
43   end
44
45   def summary
46     description.split(/,/)[0]
47   end
48
49   def tabs
50     description.split(/,/)[1..-1]
51   end
52
53   def description
54     %r(<meta name="description" content="(.+?)">).match(@source)[1]
55   end
56
57   def extname
58     File.extname(origname)[1..-1]
59   end
60
61   def origname
62     %r(<p>ファイル名&nbsp;:&nbsp;(.+?)</p>).match(@source)[1]
63   end
64
65   def boundary
66     "myboundary"
67   end
68
69   def request_header
70     header = {}
71     header["user-agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.0; ja; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)"
72     header["accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
73     header["accept-language"] = "ja,en-us;q=0.7,en;q=0.3"
74     header["accept-charset"] = "Shift_JIS,utf-8;q=0.7,*;q=0.7"
75     header["referer"] = "http://bowlroll.net/up/#{code}"
76     header["content-type"] = "multipart/form-data; boundary=#{boundary}"
77     header
78   end
79
80   def request_body
81     body = ''
82     body.concat "--#{boundary}\r\n"
83     body.concat "content-disposition: form-data; name=\"upDlBrowser\";\r\n"
84     body.concat "\r\n"
85     body.concat "OTHER\r\n"
86     body.concat "--#{boundary}--\r\n"
87     body
88   end
89
90   def name
91     "#{code}.#{extname}"
92   end
93
94   def arc_path
95     "/Volumes/uploader/arc/mmdbowl/#{name}"
96   end
97
98   def save_arc_0
99     http = Net::HTTP.new('bowlroll.net')
100     http.start do
101       request = Net::HTTP::Post.new('/Php/upDlDownload.php')
102
103       request_header.each do |key, value|
104         request[key] = value
105       end
106       request.body = request_body
107
108       http.request(request) do |response|
109         p response.code
110         p response['content-type']
111         open(arc_path, 'wb') { |f|
112           response.read_body { |buf| f.write buf }
113         }
114       end
115     end
116   end
117
118   def save_arc
119     if removed?
120       return false
121     end
122     if locked?
123       return false
124     end
125     if File.exist? arc_path
126       return false
127     end
128     save_arc_0
129     true
130   end
131 end