Class: MyJobAnisoku

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/job.rb

Overview

Job Class for Fetch Anisoku Function:

access "youtubeanisoku1.blog106.fc2.com" and crawl web site.
find a link to "say-move.org" and access "say-move.org".
finary find video link of Animetion,then fetch video file to your local.
save video directory is supplied by machine.

Notice:

job is automatically generated on after another.
This Class Needs to be handle by Machine Class

Examples:

# inside machine class     
  job = MyJobAnisoku.new(
   :machine => self
  )
  job.run

Instance Method Summary collapse

Constructor Details

#initialize(args = { }) ⇒ MyJobAnisoku

Returns a new instance of MyJobAnisoku.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/lib/job.rb', line 176

def initialize(args = { })
  require 'rubygems'
  require 'kconv'
  require 'mechanize'
  require 'net/http'
  @a = args
  @debug = args[:debug] ||= false
  @a[:url] ||= 'http://youtubeanisoku1.blog106.fc2.com/'
  @a[:url] = URI.parse @a[:url] unless @a[:url].class == URI::HTTP
  @agent = Mechanize.new
  @a[:status] ||= :new
  @a[:recent] ||= 7
  @a[:limit] ||= 4
  # make md5 with magicword '_gGddgPfeaf_gzyr'
  @FC2magick = @a[:fc2magick] ||='_gGddgPfeaf_gzyr'  #updated FC2 2011.7
  raise "job have no machine error"  unless @a[:machine]
  p @a if @debug && @a[:status] == :new
end

Instance Method Details

#fc2Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/lib/job.rb', line 335

def fc2
  print "fc2".yellow
  require 'digest'
  url = "http://video.fc2.com/ginfo.php?mimi=#{Digest::MD5.hexdigest(@a[:fc2] + @FC2magick)}&v=#{@a[:fc2]}&upid=#{@a[:fc2]}&otag=1"
  url = `curl -# -L -R "#{url} "`
  url =  url.split('&')[0].split('=')[1] + '?' + url.split('&')[1]
  puts url.red.bold
  job = MyJobAnisoku.new(
                         @a.merge({
                                    :url => url,
                                    :status => :video
                                  }))
  @a[:machine].retry job
end

#kobetuObject

access say-move and make video job



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/lib/job.rb', line 262

def kobetu
  print "Kobetu".yellow
  @agent.get @a[:url]
  title = @agent.page.title.gsub(' ★ You Tube アニ速 ★','')
  # acume url
  htmlA = @agent.page/"/html/body/table/tr[2]/td/table/tr/td[2]/div[4]/div[@class='kijisub']"
  require 'pp'
  targsHTMLs = htmlA.inner_html.toutf8.split(/ランキング/)[0].split(/\n第/).reverse!
  #http://posterous.com/getfile/files.posterous.com/temp-2011-08-21/eolunzlwwwFopCnhszaBwJlFEJEnHcloqkoyaFuhdezmdgipcyyiyzdpqcpG/cro08nyoutube.doc
  require 'digest' 
  targsHTMLs.each_with_index do |html,i|
    break if i >= @a[:limit]
    key = title + html.to_s
    unless @a[:machine].episode_exists?( Digest::MD5.hexdigest(key)  )
      #        puts "NOW 2 PROCEED FETCH".green.bold + html[0..20].yellow.bold
      indi = Nokogiri::HTML.fragment(html).css("a")
      indi.each do |va|
        p va[:href] if @debug
        if va[:href] =~ /(http:\/\/say-move\.org\/comeplay\.php.*)/
          job = MyJobAnisoku.new(
                                 @a.merge({
                                            :url => $1,
                                            :title => title + '' + html.split('<').first.gsub(' ','').gsub(' ',''),
                                            :status => :third}))
          @a[:machine].retry job
        end
      end
    else
      puts "ALREADY REGISTED CANCELL FETCH".cyan.bold + html[0..20].yellow.bold if @debug
    end
    key = nil
  end
end

#runObject

run in thread



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/lib/job.rb', line 379

def run
  t = Thread.new do
    case @a[:status]
    when :new then
      tokkakari
    when :second then
      second
    when :kobetu then
      kobetu
    when :third then
      third
    when :fc2 then
      fc2
    when :video then
      video
    end
  end
end

#secondObject

check shoukai page



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/lib/job.rb', line 239

def second
  print "Second".yellow
  @agent.get @a[:url]
  links_kousin =  @agent.page/"/html/body/table/tr[2]/td/table/tr/td[2]/div[4]/ul/li/a/@href"
  # links_kobetu 
  links_kobetu = []
  links_kousin.each do |link|
    links_kobetu << $1  if link.value =~ /(http:\/\/youtubeanisoku.*)/
  end

  # make job for each links_kobetu
  links_kobetu.each do |link|
    p link if @debug
    job = MyJobAnisoku.new(
                           @a.merge({
                                      :url => link,
                                      :status => :kobetu
                                    } ))
    @a[:machine].retry job
  end
end

#thirdObject

access say-move and make video job



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/lib/job.rb', line 297

def third
  print "Third".yellow
  #sm has title and url

  sm = { :title => @a[:title],:url => @a[:url]}
  # debug fc2 video sm[:url] = "http://say-move.org/comeplay.php?comeid=217953"
  @agent.get(sm[:url])
  sm[:title] += @agent.page.title.gsub!('FC2 SayMove!','') 
  set =  @agent.page/"/html/body/div/div[2]/div[7]/div[2]/input/@value"
  if !set.empty?
    sm[:videourl] = set[0].value 
  else
    set =  @agent.page/"/html/body/div/div[2]/div[3]/object/param[5]/@value"
    fc2 = set[0].value.split('&')[1].split('=')[1]
    unless fc2.nil?
      p sm[:url] if @debug
      job = MyJobAnisoku.new(
                             @a.merge({
                                        :url => sm[:url],
                                        :fc2 => fc2,
                                        :title => sm[:title],
                                        :status => :fc2
                                      }))
      @a[:machine].retry job
      return
    else
    end
  end
  
  job = MyJobAnisoku.new(
                         @a.merge({
                                    :url => sm[:videourl],
                                    :title => sm[:title],
                                    :status => :video
                                  }))
  @a[:machine].retry job
end

#tokkakariObject

check kousin page



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/lib/job.rb', line 196

def tokkakari
  print "Tokkakari".yellow
  @agent.get @a[:url]
  links_kousins = @agent.page.links_with(:text => /#{"更新状況"}/)
  links_kousins2 = @agent.page.links_with(:href => /blog\-entry/)
  targs = []
  targs2 = []

  links_kousins.each do |link|
    targs << link.uri
  end
  
  links_kousins2.each do |link|
    targs2 << link.uri
  end

  targs.each_with_index do |link,i|
    break if i >= @a[:recent]
    p link if @debug
    job = MyJobAnisoku
      .new(
           @a.merge({
                      :url => link,
                      :status => :second,
                    }) )
    @a[:machine].retry job
  end
  
  targs2.each_with_index do |link,i|
    p link if @debug
    job = MyJobAnisoku
      .new(
           @a.merge({
                      :url => link,
                      :status => :kobetu,
                    }) )
    @a[:machine].retry job
  end

  
end

#videoObject

fetch video



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/lib/job.rb', line 351

def video
  print "video".yellow
  # save video directory is supplied by machine.
  savedir = @a[:machine].savedir
  Dir.chdir savedir
  filename = "#{@a[:title].gsub(' ','').gsub(' ','')}.mp4"
  savepath = "#{savedir}/#{filename}"
  # check fetch candidate had been already saved?
  if File.exist?(savepath) && File.size(savepath) > 1024 * 1024 * 3
    puts "File Already Saved ".yellow.bold  + savepath
    return
  else
    puts "Fetching ".green.bold + savepath
    MyLogger.ln "Fetch Attempt Start ".green.bold  + savepath
  end

  # use curl command
  # no need UA...
  uri = "http://#{@a[:url].host}#{@a[:url].path}"
  uri += "?#{@a[:url].query}" if @a[:url].query
  command = "curl -# -L -R -o '#{filename}' '#{uri}' >/dev/null 2>&1"
  #    command += "&& growlnotify -t '#{filename}' -m '#{uri}' "

  puts command if @debug
  system command unless @debug
end