Class: Bookscan::Agent

Inherits:
Mechanize
  • Object
show all
Defined in:
lib/bookscan/agent.rb

Instance Method Summary collapse

Constructor Details

#initializeAgent

Returns a new instance of Agent.



14
15
16
17
# File 'lib/bookscan/agent.rb', line 14

def initialize
  super
  max_history = 0
end

Instance Method Details

#allbooksObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/bookscan/agent.rb', line 153

def allbooks
  books = Books.new
  group_urls.each do |url|
    getr(url)
    page.search("a[@class=downloading]").each do |u|
      next if u.text == "PDFダウンロード" or 
        u.text == "変換"
      book = Book.new
      book.title = u.text.to_s
      book.url = "/" + u.attributes["href"].value.to_s
      book.group_url = url
      books[book.book_id] = book
    end
  end
  books
end

#books(group) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/bookscan/agent.rb', line 138

def books group
  bs = Books.new
  getr(group.url)
  page.search("a[@class=downloading]").each do |u|
    next if u.text == "PDFダウンロード" or 
      u.text == "変換"
    book = Book.new
    book.title = u.text.to_s
    book.url = "/" + u.attributes["href"].value.to_s
    book.group_url = group.url
    bs << book
  end
  bs
end

#download(url, path) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/bookscan/agent.rb', line 170

def download(url,path)
  url = URI.parse(url)
  url.scheme = "https"
  url.host = "system.bookscan.co.jp"

  cli = HTTPClient.new
  cookie_jar && cookie_jar.cookies(url).each do |cookie|
    cli.cookie_manager.parse(cookie.to_s,url)
  end

  length = 0;total = 0
  res = cli.head(url.to_s)

  if res.status == 302
    url = URI.parse(res.header["Location"].to_s)
    res = cli.head(url.to_s)
  end

  total = res.header["Content-Length"].to_s.to_i
  t = Thread.new {
    conn = cli.get_async(url.to_s)
    io = conn.pop.content
    ::File::open(path, "wb") { |f|
      while str = io.read(40)
        f.write str
        length += str.length
      end
    }
  }
  pbar = ProgressBar.new("Loading",total)
  while  total > length
    sleep 1
    pbar.set(length)
  end
  pbar.finish
  t.join
end

#getr(path) ⇒ Object



19
20
21
# File 'lib/bookscan/agent.rb', line 19

def getr path
  get(BSURL + path)
end

#group_urlsObject



129
130
131
132
133
134
135
136
# File 'lib/bookscan/agent.rb', line 129

def group_urls
  getr("/history.php")
  urls = Array.new
  page.search("a/@href").each do |url|
    urls << "/" + url if(/bookdetail/ =~ url)
  end
  urls
end

#groupsObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/bookscan/agent.rb', line 112

def groups
  r = Groups.new
  getr("/history.php")
  page.search("table.table5").each do |table|
    if /依頼したもの一覧/ =~  table.to_s
      table.search("tr").each do |tr|
        if /決済完了/ =~ tr.to_s
          g = Group.new
          g.import(tr)
          r << g
        end
      end
    end
  end
  r
end

#login(id, password) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/bookscan/agent.rb', line 30

def  id,password
  getr("/login.php")
  form = page.forms[0]
  form.fields.find{|f| f.name == "email" }.value = id
  form.fields.find{|f| f.name == "password" }.value = password
  submit(form)
end

#login?Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/bookscan/agent.rb', line 38

def login?
  getr("/mypage.php")
  /さんのおかげです/ =~ page_body # => OK
end

#logoutObject



43
44
45
# File 'lib/bookscan/agent.rb', line 43

def logout
  getr("/history.php?logout=1")
end

#page_body(enc = 'utf-8') ⇒ Object



24
25
26
27
28
# File 'lib/bookscan/agent.rb', line 24

def page_body enc='utf-8'
  body = page.body
  body.force_encoding(enc) if body.respond_to?(:force_encoding)
  body
end

#tune(book, options) ⇒ Object

def tune(book,type,is_premium = true)

Raises:

  • (SystemExit)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bookscan/agent.rb', line 86

def tune(book,options)
  type = options[:type]
  is_premium = true
  is_premium = options[:is_premium]
  max_queue = 1
  max_queue = 10 if is_premium

  @tuning = tuning unless @tuning
  # チューニングいっぱい
  raise SystemExit.new(0,"tune queue is full") if @tuning.length >= max_queue
  # チューニング
  return false if tuning?(book,type)
  # tune
  getr(book.tune_url)
  page.forms.first["optimize_type"] = type;
  page.forms.first["cover_flg"] = "1";
  if options[:to] == "dropbox"
    page.forms.first["dropbox_flg"] = "1";
  end
  page.forms.first.submit
  tuned = book.clone
  tuned.title = type +"_"+book.title
  @tuning << tuned
  tuned
end

#tunedObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bookscan/agent.rb', line 47

def tuned
  bs = Books.new
  getr("/tunelablist.php")
  page.search("td").each do |td|
    if TUNED_PATTERN =~ td.to_s
      a = td.at("a") 
      book = Book.new
      book.title = URI.decode($1)
      book.url = a.attributes["href"].value.to_s
      book.group_url = "/tunelablist.php"
      bs << book
    end
  end
  bs
end

#tuningObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bookscan/agent.rb', line 63

def tuning
  bs = Books.new
  getr("/tunelabnowlist.php")
  page.search("td").each do |td|
    if TUNED_PATTERN =~ td.to_s
      book = Book.new
      book.title = $1
      book.group_url = "/tunelabnowlist.php"
      bs << book
    end
  end
  bs
end

#tuning?(book, type) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# File 'lib/bookscan/agent.rb', line 77

def tuning?(book,type)
  @tuning = tuning unless @tuning
  @tuning.each { |b|
    return true if b.title == type+"_"+book.title
  }
  false
end