myimdb 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.3
1
+ 0.3.4
data/bin/myimdb-catalogue CHANGED
@@ -1,34 +1,151 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- # point this file to a folder and all the folder inside it will be renamed
4
- require 'myimdb.rb'
3
+ require 'open-uri'
4
+ require 'optparse'
5
+ require 'myimdb'
5
6
 
6
- unless ARGV[0]
7
- p "Source directory required - exiting"
8
- exit(0)
7
+ options = {}
8
+
9
+ OptionParser.new do |opts|
10
+ opts.banner = "Usage: #{File.basename($0)} [movie name]"
11
+
12
+ opts.on("-h", "--help", "Displays this help info") do
13
+ puts opts
14
+ exit 0
15
+ end
16
+
17
+ opts.on("-f", "--force", "Force generate data even if already present") do
18
+ options[:force] = true
19
+ end
20
+
21
+ opts.on("-m", "--metadata", "Generates metadata (renames folders)") do
22
+ options[:metadata] = true
23
+ end
24
+
25
+ opts.on("-i", "--images", "Generates images (only applies to windows)") do
26
+ options[:images] = true
27
+ end
28
+
29
+ begin
30
+ opts.parse!(ARGV)
31
+ rescue OptionParser::ParseError => e
32
+ warn e.message
33
+ puts opts
34
+ exit 1
35
+ end
9
36
  end
10
37
 
11
- file_paths = Dir["#{ARGV[0]}/*"]
12
- search_scope = "imdb.com"
38
+ # add imdb as default
39
+ options.merge!(:metadata=> true) if options.empty?
40
+
41
+ # need a movie name
42
+ if ARGV.empty?
43
+ abort "Source directory required - exiting"
44
+ end
45
+
46
+ class Platform
47
+ class << self
48
+ def windows?
49
+ RUBY_PLATFORM =~ /mswin|windows/i
50
+ end
51
+
52
+ def mac?
53
+ RUBY_PLATFORM =~ /darwin/i
54
+ end
55
+
56
+ def linux?
57
+ RUBY_PLATFORM =~ /linux/i
58
+ end
59
+ end
60
+ end
61
+
62
+ file_paths = Dir["#{ARGV[0]}/*"]
63
+
64
+ def save_image(target_dir, name, image_index=0)
65
+ image_data = Myimdb::Search::Google.search_images(name, :size=> 'medium')[image_index]
66
+ image_url = image_data["url"] if image_data
67
+ if image_url
68
+ image_file_path = File.join(target_dir, 'movie.jpg')
69
+ conf_file_path = File.join(target_dir, 'desktop.ini')
70
+ open(image_url) do |file|
71
+ File.open(image_file_path, 'wb') do |image_file|
72
+ image_file.puts file.read
73
+ end
74
+ end
75
+
76
+ `convert "#{image_file_path}" -thumbnail 256x256 -gravity center -crop 256x256+0+0! -background transparent -flatten "#{File.join(target_dir, 'movie.ico')}"`
77
+
78
+ if Platform.windows?
79
+ File.delete(conf_file_path) if File.exists?(conf_file_path)
80
+ File.open(conf_file_path, 'wb') do |conf|
81
+ conf.puts "[.ShellClassInfo]"
82
+ conf.puts "IconResource=movie.ico,0"
83
+ end
84
+ `attrib -s +h "#{conf_file_path}"`
85
+ `attrib -r "#{target_dir}"`
86
+ `attrib +r "#{target_dir}"`
87
+ end
88
+
89
+ File.delete(image_file_path)
90
+ end
91
+ end
92
+
93
+ def generate_metadata(path, name)
94
+ search_scope = "imdb.com"
95
+
96
+ search_result = Myimdb::Search::Google.search_text(name, :restrict_to=> search_scope)[0]
97
+ imdb = Myimdb::Scraper::Imdb.new(search_result["url"])
98
+ new_name = name.gsub(/\[\S+\]/, "").strip
99
+ new_name << " [#{imdb.year}] [#{imdb.rating},#{imdb.votes}] [#{imdb.directors.join(',')}]"
100
+ p "Renaming: #{name} to: #{new_name}"
101
+ new_path = File.join(File.dirname(path), new_name)
102
+ File.rename(path, new_path)
103
+ new_path
104
+ end
105
+
106
+ def filtered_movie_name(name)
107
+ name.gsub(/\[.*?\]/,'').strip
108
+ end
109
+
110
+ def filtered_movie_year(name)
111
+ name.scan(/\[(\d+)\]/).flatten.first
112
+ end
113
+
114
+
115
+ image_retry_limit = 2
13
116
 
14
117
  file_paths.each do |path|
118
+ retry_count = 0
119
+ metadata_present = false
120
+
15
121
  next if !File.directory?(path)
16
- p "============================================"
17
- name = File.basename(path)
18
- if name.scan(/\[.*?\]/).size == 3
19
- p "Skipping: #{name}"
20
- else
21
- p "Fetching details for: #{name}"
22
- begin
23
- search_result = Myimdb::Search::Google.search_text(name, :restrict_to=> search_scope)[0]
24
- imdb = Myimdb::Scraper::Imdb.new(search_result["url"])
25
- new_name = name.gsub(/\[\S+\]/, "").strip
26
- new_name << " [#{imdb.year}] [#{imdb.rating},#{imdb.votes}] [#{imdb.directors.join(',')}]"
27
- p "Renaming: #{name} to: #{new_name}"
28
- new_path = File.join(File.dirname(path), new_name)
29
- File.rename(path, new_path)
30
- rescue
31
- p "Unable to fetch details for: #{name}"
122
+ begin
123
+ name = File.basename(path)
124
+
125
+ metadata_present = (name.scan(/\[.*?\]/).size == 3)
126
+ # metadata exists and -f isn't supplied
127
+ if metadata_present and !options[:force]
128
+ p "Skipping: Metadata already exists for: #{name}"
129
+ elsif options[:metadata]
130
+ p "Fetching metadata for: #{name}"
131
+ path = generate_metadata(path, filtered_movie_name(name))
132
+ end
133
+
134
+ # movie icon exists and -f isn't supplied
135
+ if !metadata_present
136
+ p "Skipping: Metadata not present: #{name}"
137
+ elsif File.exists?(File.join(path, 'movie.ico')) and !options[:force]
138
+ p "Skipping: Image already exists for: #{name}"
139
+ elsif options[:images]
140
+ p "Fetching image for: #{name}"
141
+ begin
142
+ save_image(path, "#{filtered_movie_name(name)} #{filtered_movie_year(name)}", retry_count)
143
+ rescue
144
+ retry_count += 1
145
+ retry_count < image_retry_limit ? retry : raise
146
+ end
32
147
  end
148
+ rescue
149
+ p "Unable to fetch details for: #{name}"
33
150
  end
34
151
  end
@@ -31,9 +31,15 @@ module Myimdb
31
31
  include HandleExceptions
32
32
  def directors
33
33
  end
34
+
35
+ def directors_with_url
36
+ end
34
37
 
35
38
  def writers
36
39
  end
40
+
41
+ def writers_with_url
42
+ end
37
43
 
38
44
  def rating
39
45
  end
@@ -8,10 +8,18 @@ module Myimdb
8
8
  def directors
9
9
  document.css('.info h5:contains("Director") + .info-content a:not(.tn15more)').collect{ |a| a.text }
10
10
  end
11
+
12
+ def directors_with_url
13
+ document.css('.info h5:contains("Director") + .info-content a:not(.tn15more)').collect{ |a| {:name=> a.text, :url=> "http://www.imdb.com#{a['href']}" } }
14
+ end
11
15
 
12
16
  def writers
13
17
  document.css('.info h5:contains("Writer") + .info-content a:not(.tn15more)').collect{ |a| a.text }
14
18
  end
19
+
20
+ def writers_with_url
21
+ document.css('.info h5:contains("Writer") + .info-content a:not(.tn15more)').collect{ |a| {:name=> a.text, :url=> "http://www.imdb.com#{a['href']}" } }
22
+ end
15
23
 
16
24
  def rating
17
25
  document.css(".starbar-meta b").inner_text.strip.split('/').first.to_f
@@ -46,7 +54,7 @@ module Myimdb
46
54
  @document ||= Nokogiri::HTML(open(@url))
47
55
  end
48
56
 
49
- handle_exceptions_for :directors, :writers, :rating, :votes, :genres, :tagline, :plot, :year
57
+ handle_exceptions_for :directors, :directors_with_url, :writers, :writers_with_url, :rating, :votes, :genres, :tagline, :plot, :year
50
58
  end
51
59
  end
52
60
  end
data/myimdb.gemspec CHANGED
@@ -5,13 +5,13 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{myimdb}
8
- s.version = "0.3.3"
8
+ s.version = "0.3.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Gaurav"]
12
- s.date = %q{2010-01-19}
12
+ s.date = %q{2010-01-22}
13
13
  s.email = %q{gaurav@vinsol.com}
14
- s.executables = ["myimdb", "myimdb-catalogue", "myimdb-catalogue.bat", "myimdb.bat"]
14
+ s.executables = ["myimdb", "myimdb-catalogue"]
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
17
  "README.rdoc"
@@ -23,8 +23,6 @@ Gem::Specification.new do |s|
23
23
  "VERSION",
24
24
  "bin/myimdb",
25
25
  "bin/myimdb-catalogue",
26
- "bin/myimdb-catalogue.bat",
27
- "bin/myimdb.bat",
28
26
  "lib/myimdb.rb",
29
27
  "lib/myimdb/scraper.rb",
30
28
  "lib/myimdb/scraper/base.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myimdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gaurav
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-19 00:00:00 +05:30
12
+ date: 2010-01-22 00:00:00 +05:30
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -37,8 +37,6 @@ email: gaurav@vinsol.com
37
37
  executables:
38
38
  - myimdb
39
39
  - myimdb-catalogue
40
- - myimdb-catalogue.bat
41
- - myimdb.bat
42
40
  extensions: []
43
41
 
44
42
  extra_rdoc_files:
@@ -51,8 +49,6 @@ files:
51
49
  - VERSION
52
50
  - bin/myimdb
53
51
  - bin/myimdb-catalogue
54
- - bin/myimdb-catalogue.bat
55
- - bin/myimdb.bat
56
52
  - lib/myimdb.rb
57
53
  - lib/myimdb/scraper.rb
58
54
  - lib/myimdb/scraper/base.rb
@@ -1 +0,0 @@
1
- @"ruby.exe" "%~dpn0" %*
data/bin/myimdb.bat DELETED
File without changes