Sha256: b9b1c27eb942ac01c6ef6afd1404b3f8e688c762d87e0d20043afd021ecdb9b7

Contents?: true

Size: 1.92 KB

Versions: 5

Compression:

Stored size: 1.92 KB

Contents

# == Synopsis
# Used to fetch thumbnail images from
#
# Usage:
#  controller = ThumbnailController.new(media)
#  controller.update
# or
#  ThumbnailController.update(media)
class ThumbnailController

  def self.update(media)
    ThumbnailController.new(media).update
  end

  def initialize(media)
    @media = media
  end

  # update the movie's thumbnail (.tbn) image
  def update
    result = false
    if @media.isbn.blank?
      unless @media.imdb_id.blank?
        if @media.image_files.empty?
          fetch_imdb_thumbnail(@media.imdb_id)
          result = true
        end
      end
    else
      copy_thumbnail(@media.isbn)
      result = true
    end
    result
  end

  protected

  # fetch the thumbnail from IMDB and save as path_to('tbn')
  def fetch_imdb_thumbnail(imdb_id)
    imdb_movie = ImdbMovie.new(imdb_id.gsub(/^tt/, ''))
    source_uri = imdb_movie.poster.image
    dest_image_filespec = @media.path_to(:thumbnail)
    puts "fetch_imdb_thumbnail(#{imdb_id}) => #{source_uri}"
    begin
      File.open(dest_image_filespec, "wb") do |f|
        f.write(read_page(source_uri.escape_unicode))
      end
    rescue Exception => e
      AppConfig[:logger].error { "Error downloading image from \"#{source_uri}\" to \"#{dest_image_filespec}\" - #{e.to_s}" }
    end
  end

  # copy images from .../isbn.jpg to .../basename.jpg
  def copy_thumbnail(isbn)
    src_image_filespec = File.join(AppConfig[:images_dir], "#{isbn}f.jpg")
    if File.exist?(src_image_filespec)
      dest_image_filespec = @media.path_to(:thumbnail)
      do_copy = true
      if File.exist?(dest_image_filespec)
        if File.mtime(src_image_filespec) <= File.mtime(dest_image_filespec)
          do_copy = false
        end
      end
      begin
        File.copy(src_image_filespec, dest_image_filespec) if do_copy
      rescue Exception => e
        AppConfig[:logger].error {e.to_s}
      end
    end
  end

  def read_page(page)
    open(page).read
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
royw-dvdprofiler2xbmc-0.0.19 lib/dvdprofiler2xbmc/controllers/thumbnail_controller.rb
royw-dvdprofiler2xbmc-0.1.0 lib/dvdprofiler2xbmc/controllers/thumbnail_controller.rb
royw-dvdprofiler2xbmc-0.1.2 lib/dvdprofiler2xbmc/controllers/thumbnail_controller.rb
royw-dvdprofiler2xbmc-0.1.4 lib/dvdprofiler2xbmc/controllers/thumbnail_controller.rb
royw-dvdprofiler2xbmc-0.1.5 lib/dvdprofiler2xbmc/controllers/thumbnail_controller.rb