OSDN Git Service

8c68e86455e3142ed962fce22c458f51de3ab60e
[tdcgexplorer/nimono-crawlers.git] / bin / load-arc_thumbs.rb
1 #!ruby
2 # encoding: utf-8
3 $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
4 $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../app/models')
5 require 'connection'
6 require 'site'
7 require 'arc'
8 require 'thumb'
9
10 def link(thumb, code)
11   arc = Arc.find(code)
12   unless arc
13     puts "not found arc.code: #{code}"
14     return
15   end
16   unless arc.thumbs.include?(thumb)
17     arc.thumbs << { :key => thumb.key }
18     arc.save
19   end
20   unless thumb.arcs.include?(arc)
21     thumb.arcs << { :key => arc.key }
22     thumb.save
23   end
24 end
25
26 sites = Site.all
27 thumbs = Thumb.all
28 thumbs.each do |thumb|
29   # puts "# #{thumb.video_id}"
30   sites.each do |site|
31     path_re = Regexp.new(Regexp.escape(site.url) + site.path_pattern)
32     if md = path_re.match(thumb.description)
33       name = md[1]
34       code, extname = site.generate_arc_code_and_extname(name)
35       puts [ thumb.video_id, "", code ].join("\t")
36       link(thumb, code)
37     end
38
39     next unless site.locked_path_pattern
40     locked_path_re = Regexp.new(Regexp.escape(site.url) + site.locked_path_pattern)
41     if md = locked_path_re.match(thumb.description)
42       name = md[1]
43       code = site.generate_locked_arc_code(name)
44       puts [ thumb.video_id, "locked", code ].join("\t")
45       link(thumb, code)
46     end
47   end
48 end