wraith 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/wraith ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+
5
+ begin
6
+ require 'wraith/cli'
7
+ Wraith::CLI.start
8
+ rescue Interrupt => e
9
+ puts "\nQuitting..."
10
+ exit 1
11
+ rescue SystemExit => e
12
+ exit e.status
13
+ end
@@ -0,0 +1,43 @@
1
+ #Headless browser option
2
+ browser:
3
+ webkit: "phantomjs"
4
+ # gecko: "slimerjs"
5
+
6
+ #If you want to have multiple snapping files, set the file name here
7
+ snap_file: "lib/wraith/javascript/snap.js"
8
+
9
+ # Type the name of the directory that shots will be stored in
10
+ directory:
11
+ - 'shots'
12
+
13
+ # Add only 2 domains, key will act as a label
14
+ domains:
15
+ english: "http://www.live.bbc.co.uk/news"
16
+ russian: "http://www.live.bbc.co.uk/russian"
17
+
18
+ #Type screen widths below, here are a couple of examples
19
+ screen_widths:
20
+ - 320
21
+ - 600
22
+ - 768
23
+ - 1024
24
+ - 1280
25
+
26
+ #Type page URL paths below, here are a couple of examples
27
+ paths:
28
+ home: /
29
+ uk_index: /uk
30
+
31
+ # If you don't want to name the paths explicitly you can use a yaml
32
+ # collection as follows, and names will be derived by replacing / with _
33
+ #
34
+ # paths:
35
+ # - /imghp
36
+ # - /maps
37
+
38
+ #Amount of fuzz ImageMagick will use
39
+ fuzz: '20%'
40
+
41
+ #Set the number of days to keep the site spider file
42
+ spider_days:
43
+ - 10
@@ -0,0 +1,43 @@
1
+ #Headless browser option
2
+ browser:
3
+ webkit: "phantomjs"
4
+ # gecko: "slimerjs"
5
+
6
+ #If you want to have multiple snapping files, set the file name here
7
+ snap_file: "lib/wraith/javascript/nojs.js"
8
+
9
+ # Type the name of the directory that shots will be stored in
10
+ directory:
11
+ - 'shots_nojs'
12
+
13
+ # Add only 2 domains, key will act as a label
14
+ domains:
15
+ english: "http://www.live.bbc.co.uk/news"
16
+ russian: "http://www.live.bbc.co.uk/russian"
17
+
18
+ #Type screen widths below, here are a couple of examples
19
+ screen_widths:
20
+ - 320
21
+ - 600
22
+ - 768
23
+ - 1024
24
+ - 1280
25
+
26
+ #Type page URL paths below, here are a couple of examples
27
+ paths:
28
+ home: /
29
+ uk_index: /uk
30
+
31
+ # If you don't want to name the paths explicitly you can use a yaml
32
+ # collection as follows, and names will be derived by replacing / with _
33
+ #
34
+ # paths:
35
+ # - /imghp
36
+ # - /maps
37
+
38
+ #Amount of fuzz ImageMagick will use
39
+ fuzz: '20%'
40
+
41
+ #Set the number of days to keep the site spider file
42
+ spider_days:
43
+ - 10
@@ -0,0 +1,40 @@
1
+ #Headless browser option
2
+ browser:
3
+ webkit: "phantomjs"
4
+ # gecko: "./slimerjs"
5
+
6
+ #If you want to have multiple snapping files, set the file name here
7
+ snap_file: "lib/wraith/javascript/snap.js"
8
+
9
+ # Type the name of the directory that shots will be stored in
10
+ directory:
11
+ - 'shots'
12
+
13
+ # Add only 2 domains, key will act as a label
14
+ domains:
15
+ english: "http://www.live.bbc.co.uk/news"
16
+ russian: "http://www.live.bbc.co.uk/russian"
17
+
18
+ #Type screen widths below, here are a couple of examples
19
+ screen_widths:
20
+ - 320
21
+ - 600
22
+ - 768
23
+ - 1024
24
+ - 1280
25
+
26
+ #Type page URL paths below, here are a couple of examples
27
+ paths:
28
+ home: /
29
+ uk_index: /uk
30
+
31
+ # If you don't want to name the paths explicitly you can use a yaml
32
+ # collection as follows, and names will be derived by replacing / with _
33
+ #
34
+ # paths:
35
+ # - /imghp
36
+ # - /maps
37
+
38
+ #Amount of fuzz ImageMagick will use
39
+ fuzz: '20%'
40
+
data/lib/wraith.rb CHANGED
@@ -1,5 +1,6 @@
1
- class Wraith
2
- def self.hi
3
- puts "Hello world!"
4
- end
1
+ require "wraith/version"
2
+
3
+ module Wraith
4
+ autoload :CLI, 'wraith/cli'
5
+ autoload :Error, 'wraith/error'
5
6
  end
Binary file
data/lib/wraith/cli.rb ADDED
@@ -0,0 +1,82 @@
1
+ require 'thor'
2
+ require 'wraith'
3
+ require 'wraith/save_images'
4
+ require 'wraith/crop'
5
+ require 'wraith/spider'
6
+ require 'wraith/folder'
7
+ require 'wraith/thumbnails'
8
+ require 'wraith/compare_images'
9
+ require 'wraith/images'
10
+
11
+ class Wraith::CLI < Thor
12
+
13
+ attr_accessor :config_name
14
+
15
+ desc "reset_shots", "removes all the files in the shots folder"
16
+ def reset_shots(config_name)
17
+ reset = Wraith::FolderManager.new(config_name)
18
+ reset.clear_shots_folder
19
+ end
20
+
21
+ desc "setup_folders", "create folders for images"
22
+ def setup_folders(config_name)
23
+ create = Wraith::FolderManager.new(config_name)
24
+ create.create_folders
25
+ end
26
+
27
+ no_commands do
28
+ def check_for_paths(config_name)
29
+ spider = Wraith::Spidering.new(config_name)
30
+ spider.check_for_paths
31
+ end
32
+
33
+ def check_images(config_name)
34
+ image = Wraith::Images.new(config_name)
35
+ image.files
36
+ end
37
+ end
38
+
39
+ desc "save_images", "captures screenshots"
40
+ def save_images(config_name)
41
+ save_images = Wraith::SaveImages.new(config_name)
42
+ save_images.save_images
43
+ end
44
+
45
+ desc "crop_images", "crops images to the same height"
46
+ def crop_images(config_name)
47
+ crop = Wraith::CropImages.new(config_name)
48
+ crop.crop_images
49
+ end
50
+
51
+ desc "compare_images", "compares images to generate diffs"
52
+ def compare_images(config_name)
53
+ compare = Wraith::CompareImages.new(config_name)
54
+ compare.compare_images
55
+ end
56
+
57
+ desc "generate_thumbnails", "create thumbnails for gallery"
58
+ def generate_thumbnails(config_name)
59
+ thumbs = Wraith::Thumbnails.new(config_name)
60
+ thumbs.generate_thumbnails
61
+ end
62
+
63
+ desc "generate_gallery", "create page for viewing images"
64
+ def generate_gallery(config_name)
65
+ dir = Wraith::Wraith.new(config_name)
66
+ puts 'Generating gallery'
67
+ `ruby lib/wraith/gallery.rb "#{dir.directory}"`
68
+ end
69
+
70
+ desc "capture config_name", "A full Wraith job"
71
+ def capture(config)
72
+ reset_shots(config)
73
+ setup_folders(config)
74
+ check_for_paths(config)
75
+ save_images(config)
76
+ check_images(config)
77
+ crop_images(config)
78
+ compare_images(config)
79
+ generate_thumbnails(config)
80
+ generate_gallery(config)
81
+ end
82
+ end
@@ -0,0 +1,21 @@
1
+ require 'wraith'
2
+
3
+ class Wraith::CompareImages
4
+ attr_reader :wraith
5
+
6
+ def initialize(config)
7
+ @wraith = Wraith::Wraith.new(config)
8
+ end
9
+
10
+ def compare_images
11
+ files = Dir.glob("#{wraith.directory}/*/*.png").sort
12
+ until files.empty?
13
+ base, compare = files.slice!(0, 2)
14
+ diff = base.gsub(/([a-z0-9]+).png$/, 'diff.png')
15
+ info = base.gsub(/([a-z0-9]+).png$/, 'data.txt')
16
+ wraith.compare_images(base, compare, diff, info)
17
+ Dir.glob("#{wraith.directory}/*/*.txt").map { |f| "\n#{f}\n#{File.read(f)}" }
18
+ puts 'Saved diff'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,50 @@
1
+ require 'wraith'
2
+ require 'image_size'
3
+
4
+ class Wraith::CropImages
5
+ attr_reader :wraith
6
+
7
+ def initialize(config)
8
+ @wraith = Wraith::Wraith.new(config)
9
+ end
10
+
11
+ def crop
12
+ crop_value(base_height, compare_height, @compare, @base)
13
+ end
14
+
15
+ def height
16
+ crop_value(base_height, compare_height, base_height, compare_height)
17
+ end
18
+
19
+ def base_height
20
+ find_heights(@base)
21
+ end
22
+
23
+ def compare_height
24
+ find_heights(@compare)
25
+ end
26
+
27
+ def crop_images
28
+ files = Dir.glob("#{wraith.directory}/*/*.png").sort
29
+ until files.empty?
30
+ @base, @compare = files.slice!(0, 2)
31
+ puts 'cropping images'
32
+ Wraith::Wraith.crop_images(crop, height)
33
+ end
34
+ end
35
+
36
+ def crop_value(base_height, compare_height, arg3, arg4)
37
+ if base_height > compare_height
38
+ arg3
39
+ else
40
+ arg4
41
+ end
42
+ end
43
+
44
+ def find_heights(height)
45
+ File.open(height, 'rb') do |fh|
46
+ size = ImageSize.new(fh.read).size
47
+ height = size[1]
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module Wraith::Error
2
+
3
+ end
@@ -0,0 +1,40 @@
1
+ require 'wraith'
2
+
3
+ class Wraith::FolderManager
4
+ attr_reader :wraith
5
+ attr_accessor :paths
6
+
7
+ def initialize(config)
8
+ @wraith = Wraith::Wraith.new(config)
9
+ end
10
+
11
+ def dir
12
+ wraith.directory
13
+ end
14
+
15
+ def paths
16
+ wraith.paths
17
+ end
18
+
19
+ def spider_paths
20
+ if !paths
21
+ paths = File.read('spider.txt')
22
+ eval(paths)
23
+ else
24
+ wraith.paths
25
+ end
26
+ end
27
+
28
+ def clear_shots_folder
29
+ FileUtils.rm_rf("./#{dir}")
30
+ FileUtils.mkdir("#{dir}")
31
+ end
32
+
33
+ def create_folders
34
+ spider_paths.each do |folder_label, path|
35
+ FileUtils.mkdir_p("#{dir}/thumbnails/#{folder_label}")
36
+ FileUtils.mkdir("#{dir}/#{folder_label}")
37
+ end
38
+ puts "Creating Folders"
39
+ end
40
+ end
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Given all the screenshots in the shots/ directory, produces a nice looking
4
+ # gallery
5
+
6
+ require 'erb'
7
+ require 'pp'
8
+ require 'fileutils'
9
+
10
+ MATCH_FILENAME = /(\S+)_(\S+)\.\S+/
11
+ TEMPLATE_LOCATION = "lib/wraith/gallery_template/gallery_template.erb"
12
+ TEMPLATE_BY_DOMAIN_LOCATION = "lib/wraith/gallery_template/gallery_template.erb"
13
+ BOOTSTRAP_LOCATION = "lib/wraith/gallery_template/bootstrap.min.css"
14
+
15
+ def parse_directories(dirname)
16
+ dirs = {}
17
+ categories = Dir.foreach(dirname).select do |category|
18
+ if ['.', '..', 'thumbnails'].include? category
19
+ # Ignore special dirs
20
+ false
21
+ elsif File.directory? "#{dirname}/#{category}" then
22
+ # Ignore stray files
23
+ true
24
+ else
25
+ false
26
+ end
27
+ end
28
+
29
+ categories.each do |category|
30
+ dirs[category] = {}
31
+ Dir.foreach("#{dirname}/#{category}") do |filename|
32
+ match = MATCH_FILENAME.match(filename)
33
+ if not match.nil? then
34
+ size = match[1].to_i
35
+ group = match[2]
36
+ filepath = category + "/" + filename
37
+ thumbnail = "thumbnails/#{category}/#{filename}"
38
+
39
+ if dirs[category][size].nil? then
40
+ dirs[category][size] = {:variants => []}
41
+ end
42
+ size_dict = dirs[category][size]
43
+
44
+ case group
45
+ when 'diff'
46
+ size_dict[:diff] = {
47
+ :filename => filepath, :thumb => thumbnail
48
+ }
49
+ when 'data'
50
+ size_dict[:data] = File.read("#{dirname}/#{filepath}")
51
+ else
52
+ size_dict[:variants] << {
53
+ :name => group,
54
+ :filename => filepath,
55
+ :thumb => thumbnail
56
+ }
57
+
58
+ end
59
+ size_dict[:variants].sort! {|a, b| a[:name] <=> b[:name] }
60
+ end
61
+ end
62
+ end
63
+
64
+ return dirs
65
+ end
66
+
67
+ def generate_html(domain, directories, template, destination)
68
+ template = File.read(template)
69
+ html = ERB.new(template).result
70
+ File.open(destination, 'w') do |outf|
71
+ outf.write(html)
72
+ end
73
+ end
74
+
75
+ if ARGV.length < 1 then
76
+ puts "USAGE: create_gallery.rb <directory>"
77
+ exit 1
78
+ end
79
+
80
+ location = ARGV[0]
81
+ domain = location
82
+ directories = parse_directories(location)
83
+ dest = "#{location}/gallery.html"
84
+
85
+ generate_html(domain, directories, TEMPLATE_BY_DOMAIN_LOCATION, dest)
86
+ FileUtils.cp(BOOTSTRAP_LOCATION, "#{location}/bootstrap.min.css")