Class: WebpageArchivist::Snapshoter

Inherits:
Object
  • Object
show all
Defined in:
lib/webpage-archivist/snapshoter.rb

Overview

Snapshot the pages and create thumbnails

Constant Summary collapse

SNAPSHOTS_PATH =
File.expand_path(ENV['ARCHIVIST_SNAPSHOTS_PATH'] || './archivist_snapshots')
GRAPHICS_MAGICK_PATH =
"#{ENV['GRAPHICS_MAGICK_PATH'] ? "#{ENV['GRAPHICS_MAGICK_PATH']}/" : ''}gm"
PHANTOMJS_PATH =
ENV['PHANTOMJS_PATH'] || 'phantomjs'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.formatObject

Returns the value of attribute format.



16
17
18
# File 'lib/webpage-archivist/snapshoter.rb', line 16

def format
  @format
end

.heightObject

Returns the value of attribute height.



16
17
18
# File 'lib/webpage-archivist/snapshoter.rb', line 16

def height
  @height
end

.qualityObject

Returns the value of attribute quality.



16
17
18
# File 'lib/webpage-archivist/snapshoter.rb', line 16

def quality
  @quality
end

.thumbnail_crop_heightObject

Returns the value of attribute thumbnail_crop_height.



16
17
18
# File 'lib/webpage-archivist/snapshoter.rb', line 16

def thumbnail_crop_height
  @thumbnail_crop_height
end

.thumbnail_crop_widthObject

Returns the value of attribute thumbnail_crop_width.



16
17
18
# File 'lib/webpage-archivist/snapshoter.rb', line 16

def thumbnail_crop_width
  @thumbnail_crop_width
end

.thumbnail_scaleObject

Returns the value of attribute thumbnail_scale.



16
17
18
# File 'lib/webpage-archivist/snapshoter.rb', line 16

def thumbnail_scale
  @thumbnail_scale
end

.widthObject

Returns the value of attribute width.



16
17
18
# File 'lib/webpage-archivist/snapshoter.rb', line 16

def width
  @width
end

Class Method Details

.snapshot(uri_or_file, snapshot_path, thumbnail_path = nil) ⇒ Object

Create a snapshot of a web page

uri_or_file

the uri of the file to snapshot

snapshot_path

path to the snapshot file

thumbnail_path: path to the thumbnail (can be nil for no thumbnail)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/webpage-archivist/snapshoter.rb', line 50

def self.snapshot uri_or_file, snapshot_path, thumbnail_path = nil
  ::WebpageArchivist.debug "Snapshot for [#{uri_or_file}] on [#{snapshot_path}]" if ::WebpageArchivist.log

  if File.exists? snapshot_path
    File.delete snapshot_path
  end

  # if the result is not a png we use an intermediate image as PhantomJS makes crappy jpeg images
  intermediate_image = (Snapshoter.format != 'png')
  real_snapshot_path = intermediate_image ? "#{snapshot_path[0..(-(File.extname(snapshot_path).length + 1))]}.png" : snapshot_path

  `#{PHANTOMJS_PATH} #{File.dirname(__FILE__)}/rasterize.js '#{uri_or_file}' #{real_snapshot_path} #{Snapshoter.width} #{Snapshoter.height}`

  unless File.exists?(real_snapshot_path)
    return false
  end

  if intermediate_image
    `#{GRAPHICS_MAGICK_PATH} convert -background white #{real_snapshot_path} #{snapshot_path}`
  end

  if thumbnail_path
    ::WebpageArchivist.debug "Thumbnail of [#{snapshot_path}] on [#{thumbnail_path}]" if ::WebpageArchivist.log

    if File.exists? thumbnail_path
      File.delete thumbnail_path
    end

    `#{GRAPHICS_MAGICK_PATH} convert -background white -scale #{Snapshoter.thumbnail_scale}% -crop #{Snapshoter.thumbnail_crop_width}x#{Snapshoter.thumbnail_crop_height}+0+0 #{real_snapshot_path} #{thumbnail_path}`
  end

  if intermediate_image
    File.delete real_snapshot_path
  end

  return true
end

.snapshot_instance(instance) ⇒ Object

Create a snapshot corresponding to an instance

instance

the instance



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/webpage-archivist/snapshoter.rb', line 33

def self.snapshot_instance instance
  dir_path = File.join(SNAPSHOTS_PATH, instance.webpage.id.to_s)
  unless Dir.exist? dir_path
    Dir.mkdir dir_path
  end

  snapshot_path = File.join(dir_path, "#{instance.id}.#{Snapshoter.format}")
  thumbnail_path = File.join(dir_path, "#{instance.id.to_s}-small.#{Snapshoter.format}")
  if snapshot(instance.webpage.index_path, snapshot_path, thumbnail_path)
    instance.update(:snapshot => true)
  end
end