royw-tmdb 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/tmdb/tmdb_image.rb +25 -14
- data/lib/tmdb/tmdb_profile.rb +1 -1
- data/spec/tmdb_image_spec.rb +13 -0
- metadata +1 -1
data/VERSION.yml
CHANGED
data/lib/tmdb/tmdb_image.rb
CHANGED
@@ -4,10 +4,11 @@ class TmdbImage
|
|
4
4
|
# imdb_id => String IMDB ID either with or without the 'tt' prefix
|
5
5
|
# api_key => String containing the themovieDb.com API key
|
6
6
|
# logger => nil or logger instance
|
7
|
-
def initialize(ident, api_key, logger)
|
7
|
+
def initialize(ident, api_key, logger, filespec=nil)
|
8
8
|
@imdb_id = 'tt' + ident.gsub(/^tt/, '') unless ident.blank?
|
9
9
|
@api_key = api_key
|
10
10
|
@logger = OptionalLogger.new(logger)
|
11
|
+
@filespec = filespec
|
11
12
|
end
|
12
13
|
|
13
14
|
# return an Array of fanart sizes as Strings
|
@@ -26,7 +27,7 @@ class TmdbImage
|
|
26
27
|
src_url = nil
|
27
28
|
if fanart_sizes.include?(size)
|
28
29
|
src_url = image_url(@imdb_id, 'fanarts', size)
|
29
|
-
copy_image(src_url, dest_filespec)
|
30
|
+
copy_image(src_url, dest_filespec)
|
30
31
|
end
|
31
32
|
src_url
|
32
33
|
end
|
@@ -37,7 +38,7 @@ class TmdbImage
|
|
37
38
|
src_url = nil
|
38
39
|
if poster_sizes.include?(size)
|
39
40
|
src_url = image_url(@imdb_id, 'posters', size)
|
40
|
-
copy_image(src_url, dest_filespec)
|
41
|
+
copy_image(src_url, dest_filespec)
|
41
42
|
end
|
42
43
|
src_url
|
43
44
|
end
|
@@ -92,7 +93,7 @@ class TmdbImage
|
|
92
93
|
# size => member of either fanart_sizes or poster_sizes
|
93
94
|
def image_url(imdb_id, type, size)
|
94
95
|
src_url = nil
|
95
|
-
profile = TmdbProfile.first(:imdb_id => imdb_id, :api_key => @api_key, :filespec =>
|
96
|
+
profile = TmdbProfile.first(:imdb_id => imdb_id, :api_key => @api_key, :filespec => @filespec, :logger => @logger)
|
96
97
|
indexes = {}
|
97
98
|
unless profile.nil? || profile.movie.blank?
|
98
99
|
movie = profile.movie
|
@@ -110,19 +111,29 @@ class TmdbImage
|
|
110
111
|
end
|
111
112
|
|
112
113
|
# download the fanart
|
114
|
+
# returns nil if no attempt to copy was made, 0 on error, or the image size in bytes on success
|
113
115
|
def copy_image(src_url, dest_filespec)
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
116
|
+
image_size = nil
|
117
|
+
unless src_url.blank? || dest_filespec.blank?
|
118
|
+
begin
|
119
|
+
image_size = 0
|
120
|
+
extension = File.extname(src_url)
|
121
|
+
unless extension.blank?
|
122
|
+
dest_filespec += extension
|
123
|
+
end
|
124
|
+
unless File.exist?(dest_filespec) && (File.size(dest_filespec) > 0)
|
125
|
+
@logger.info { "Downloading: #{src_url}" }
|
126
|
+
data = fetch(src_url.escape_unicode)
|
127
|
+
File.open(dest_filespec, 'w') do |file|
|
128
|
+
file.print(data)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
image_size = File.size(dest_filespec)
|
132
|
+
rescue Exception => e
|
133
|
+
@logger.error { "Error fetching image.\n src_url => #{src_url},\n dest_filespec => #{dest_filespec}\n #{e.to_s}" }
|
122
134
|
end
|
123
|
-
rescue Exception => e
|
124
|
-
@logger.error { "Error fetching image.\n src_url => #{src_url},\n dest_filespec => #{dest_filespec}\n #{e.to_s}" }
|
125
135
|
end
|
136
|
+
image_size
|
126
137
|
end
|
127
138
|
|
128
139
|
MAX_ATTEMPTS = 3
|
data/lib/tmdb/tmdb_profile.rb
CHANGED
data/spec/tmdb_image_spec.rb
CHANGED
@@ -107,6 +107,19 @@ describe "TmdbImage" do
|
|
107
107
|
(File.exist?(filespec).should be_true) && (File.size(filespec).should > 0)
|
108
108
|
end
|
109
109
|
|
110
|
+
# these are a couple of white box tests to show a bug where
|
111
|
+
# src_url was nil but copy_image was trying to copy from it anyway.
|
112
|
+
|
113
|
+
it "should not attempt to copy if src_url is blank" do
|
114
|
+
dest_filespec = get_temp_filename
|
115
|
+
@image.send('copy_image', nil, dest_filespec).should be_nil
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should not attempt to copy if dest_filespec is blank" do
|
119
|
+
src_url = @image.send('image_url', 'tt0465234', 'fanarts', 'original')
|
120
|
+
@image.send('copy_image', src_url, nil).should be_nil
|
121
|
+
end
|
122
|
+
|
110
123
|
def get_temp_filename
|
111
124
|
outfile = Tempfile.new('tmdb_image_spec', TMPDIR)
|
112
125
|
filespec = outfile.path
|