andhapp-players 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,8 @@
1
+ # Players: Download images from a webpage and convert them into a .psd file with layers
2
+
3
+ Players (when complete) would allow to download images from a webpage and create a photoshop layered file from the images. Players basically means "Photoshop Layers"
4
+
5
+ ## Installing
6
+
7
+ # Currently being worked on
8
+
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # players-rspec
2
+
3
+ %w{rake rake/testtask rake/rdoctask rcov/rcovtask}.each{|x| require x}
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "players"
9
+ gem.summary = "Its a simple gem that enables the image download from a webpage and creation of a photoshop layered file from those images"
10
+ gem.email = "anuj@andhapp.com"
11
+ gem.homepage = "http://github.com/andhapp/players"
12
+ gem.description = "Players - Photoshop Layers"
13
+ gem.authors = ["Anuj Dutta"]
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 0
3
+ :major: 1
4
+ :minor: 0
data/bin/players ADDED
@@ -0,0 +1,8 @@
1
+ app_path = ARGV.first
2
+
3
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", 'lib')
4
+ require 'pscript'
5
+
6
+ player = Players::Pscript.new
7
+ player.init
8
+ player.run
@@ -0,0 +1,44 @@
1
+
2
+ Dim appRef
3
+ Dim originalRulerUnits
4
+ Dim docRef, artLayerRef, imageLayerRef, currentDoc
5
+ Dim fso, folder, files
6
+
7
+ Set appRef = CreateObject("Photoshop.Application")
8
+
9
+ ' Remember current unit settings and then set units to the value expected by this script
10
+ originalRulerUnits = appRef.Preferences.RulerUnits
11
+ appRef.Preferences.RulerUnits = 2
12
+
13
+ Set fso = CreateObject("Scripting.FileSystemObject")
14
+ Set folder = fso.GetFolder(Wscript.Arguments(0))
15
+ Set files = folder.Files
16
+
17
+ Set docRef = appRef.Documents.Add(2, 4)
18
+
19
+ For each file In files
20
+ Set imageLayerRef = docRef.ArtLayers.Add
21
+ imageLayerRef.Kind = 1
22
+
23
+ Set currentDoc = appRef.Open(folder + "\" + file.Name)
24
+ currentDoc.ArtLayers("Background").Copy
25
+ currentDoc.Close()
26
+ docRef.Paste
27
+
28
+ Next
29
+
30
+ ' Restore unit setting
31
+ appRef.Preferences.RulerUnits = originalRulerUnits
32
+
33
+ Dim photoshopSaveOptions
34
+ Set photoshopSaveOptions = CreateObject("Photoshop.PhotoshopSaveOptions")
35
+ photoshopSaveOptions.Layers = True
36
+ photoshopSaveOptions.AlphaChannels = True
37
+
38
+ appRef.ActiveDocument.SaveAs folder + "\" + "collated_psd_version", _
39
+ photoshopSaveOptions, True, 2
40
+
41
+ ' don�t save anything we did
42
+ appRef.ActiveDocument.Close(2)
43
+
44
+ appRef.Quit
data/lib/options.yaml ADDED
@@ -0,0 +1,5 @@
1
+ website_address: 'http://geekandpoke.smugmug.com/gallery/5297189_ToNCK#323502743_kns4p'
2
+ destination_folder: final
3
+ destination_filename: final.psd
4
+ extension_of_files_to_download: jpg # default jpg
5
+ close_photoshop_after_processing: 2 # no(0) | yes(2) # default yes(2)
data/lib/pconstants.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Players
2
+ module Pconstants
3
+ OPTIONS_FILE = 'options.yaml'
4
+ HTTP = 'http://'
5
+ URL_SEPERATOR = '/'
6
+ INIT_DIR = 'players-init'
7
+ end
8
+ end
data/lib/players.rb ADDED
@@ -0,0 +1,106 @@
1
+ %w(net/http hpricot ftools yaml).each {|x| require x}
2
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'pconstants')
3
+
4
+ module Players
5
+
6
+ class Pscript
7
+
8
+ include Players::Pconstants
9
+
10
+ attr_reader :options
11
+
12
+ def sinit
13
+ create_player_init_directory
14
+ copy_options_file_in_init_dir
15
+ load_options
16
+ end
17
+
18
+ def run
19
+ response = make_http_request(@options["website_address"])
20
+ all_image_elements = collect_source_of_image_element(reponse)
21
+ required_image_elements = select_the_supplied_extension_elements(all_image_elements, @options["extension_of_files_to_download"])
22
+ create_destination_directory("#{Pconstants::INIT_DIR}#{File::SEPARATOR}#{@options['destination_folder']}")
23
+ elements_with_full_url = build_full_url(get_host_name_from_protocol_sanitised_url(get_protocol_sanitised_url(@options["website_address"])), required_image_elements)
24
+ download_the_images("#{Pconstants::INIT_DIR}#{File::SEPARATOR}#{@options['destination_folder']}", elements_with_full_url)
25
+ make_call_to_external_vb_script("#{Pconstants::INIT_DIR}#{File::SEPARATOR}#{@options['destination_folder']}")
26
+ end
27
+
28
+ def create_player_init_directory
29
+ FileUtils.mkdir_p("#{Pconstants::INIT_DIR}")
30
+ end
31
+
32
+ def copy_options_file_in_init_dir
33
+ File.copy(File.join(File.dirname(__FILE__), '..', 'lib', "#{Pconstants::OPTIONS_FILE}"), "#{Pconstants::INIT_DIR}#{File::SEPARATOR}#{Pconstants::OPTIONS_FILE}")
34
+ end
35
+
36
+ def load_options(name = nil)
37
+ fail "no options file to load" if !files_in_a_folder(Pconstants::INIT_DIR).index("#{name || Pconstants::OPTIONS_FILE}")
38
+ @options = YAML::load( File.open( File.join(Pconstants::INIT_DIR, "#{Pconstants::OPTIONS_FILE}") ) )
39
+ end
40
+
41
+ def files_in_a_folder(name)
42
+ Dir.entries(name).each {|x| [] << x if !File.directory?(x)}
43
+ end
44
+
45
+ def get_protocol_sanitised_url(url)
46
+ url.split("#{Pconstants::HTTP}").last
47
+ end
48
+
49
+ def get_host_name_from_protocol_sanitised_url(url)
50
+ url.split("#{Pconstants::URL_SEPERATOR}").first
51
+ end
52
+
53
+ def get_remaining_url_from_protocol_sanitised_url(url)
54
+ url.sub(/#{get_host_name_from_protocol_sanitised_url(url)}/, '')
55
+ end
56
+
57
+ def make_http_request(url)
58
+ url = get_protocol_sanitised_url(url)
59
+ main_host_name = get_host_name_from_protocol_sanitised_url(url)
60
+ remaining_address = get_remaining_url_from_protocol_sanitised_url(url)
61
+ result = Net::HTTP.get_response(main_host_name, remaining_address)
62
+ fail "could not connect to the site" if result.nil?
63
+ result.body
64
+ end
65
+
66
+ def collect_source_of_image_element(response)
67
+ doc = Hpricot(response)
68
+ all_elements = doc.search("img")
69
+ all_elements.each {|elem| [] << elem.get_attribute("src")}
70
+ end
71
+
72
+ def select_the_supplied_extension_elements(elements, extension)
73
+ elements.delete_if {|src| !src.include?(".#{extension}")}
74
+ end
75
+
76
+ def create_destination_directory(name)
77
+ FileUtils.mkdir_p(name)
78
+ end
79
+
80
+ def build_full_url(host_name, elements)
81
+ elements.map! {|elem| elem = host_name + elem}
82
+ end
83
+
84
+ def get_image_name(value)
85
+ value[value.rindex("#{Pconstants::URL_SEPERATOR}")+1..value.length]
86
+ end
87
+
88
+ def download_the_images(destination_dir, elements)
89
+ elements.each {|elem|
90
+ host = get_host_name_from_protocol_sanitised_url(elem)
91
+ query_string = get_remaining_url_from_protocol_sanitised_url(elem)
92
+ image_name = get_image_name(query_string)
93
+ Net::HTTP.start(host) { |http|
94
+ resp = http.get(query_string)
95
+ open(destination_dir + "#{File::SEPARATOR}" + image_name, "wb") { |file| file.write(resp.body) }
96
+ }
97
+ }
98
+ end
99
+
100
+ def make_call_to_external_vb_script(directory_name)
101
+ system "cscript convert_images_to_photoshop_layers.vbs #{directory_name}"
102
+ end
103
+
104
+ end
105
+
106
+ end
data/players.gemspec ADDED
@@ -0,0 +1,48 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{players}
5
+ s.version = "1.0.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Anuj Dutta"]
9
+ s.date = %q{2009-05-04}
10
+ s.default_executable = %q{players}
11
+ s.description = %q{Players - Photoshop Layers}
12
+ s.email = %q{anuj@andhapp.com}
13
+ s.executables = ["players"]
14
+ s.extra_rdoc_files = [
15
+ "README.markdown"
16
+ ]
17
+ s.files = [
18
+ "README.markdown",
19
+ "Rakefile",
20
+ "VERSION.yml",
21
+ "bin/players",
22
+ "lib/convert_images_to_photoshop_layers.vbs",
23
+ "lib/options.yaml",
24
+ "lib/pconstants.rb",
25
+ "lib/players.rb",
26
+ "players.gemspec",
27
+ "test/ts_players.rb"
28
+ ]
29
+ s.has_rdoc = true
30
+ s.homepage = %q{http://github.com/andhapp/players}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.1}
34
+ s.summary = %q{Its a simple gem that enables the image download from a webpage and creation of a photoshop layered file from those images}
35
+ s.test_files = [
36
+ "test/ts_players.rb"
37
+ ]
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 2
42
+
43
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
+ else
45
+ end
46
+ else
47
+ end
48
+ end
@@ -0,0 +1,111 @@
1
+ %w(test/unit redgreen shoulda rr fileutils ftools).each{|x| require x}
2
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'players')
3
+
4
+ class TestPlayers < Test::Unit::TestCase
5
+
6
+ include RR::Adapters::TestUnit unless include?(RR::Adapters::TestUnit)
7
+
8
+ def files_in_a_folder(name)
9
+ Dir.entries(name).each {|x| [] << x if !File.directory?(x)}
10
+ end
11
+
12
+ def dir_in_a_folder(name)
13
+ Dir.entries(name).each {|x| [] << x if File.directory?(x)}
14
+ end
15
+
16
+ context "" do
17
+ setup do
18
+ @player = Players::Pscript.new
19
+ @player.create_player_init_directory
20
+ @player.copy_options_file_in_init_dir
21
+ @options = @player.load_options
22
+
23
+ @response = '<div id="photos">
24
+ <div id="thumbnails" class="leftColumn">
25
+ <div class="photo" style="visibility: hidden" id="TinyPhotoSample"><a href="#"><img src="/img/spacer.gif" width="100" height="100" border="0" class="imgBorder" /></a></div>
26
+ <div class="photo size_Thumb" style="visibility: hidden" id="ThumbPhotoSample"><a href="#"><img src="/img/spacer.jpg" width="150" height="150" border="0" class="imgBorder" /></a></div>
27
+ </div>
28
+ </div>'
29
+
30
+ end
31
+
32
+ should "initialise the player script in a folder called players in the directory where it is run" do
33
+ dir_in_a_folder(".").include?("players-init")
34
+ end
35
+
36
+ should "copy the options file in this folder and use this options file" do
37
+ files_in_a_folder(".").include?("options.yaml")
38
+ end
39
+
40
+ should "raise exception if the options file is not present" do
41
+ assert_raises(RuntimeError) {@player.load_options('some_options.yaml')}
42
+ end
43
+
44
+ should "load options file and populate the options" do
45
+ assert_not_nil @options
46
+ assert_equal @options["website_address"], 'http://geekandpoke.smugmug.com/gallery/5297189_ToNCK#323502743_kns4p'
47
+ assert_equal @options["destination_folder"], 'final'
48
+ assert_equal @options["destination_filename"], 'final.psd'
49
+ assert_equal @options["extension_of_files_to_download"], 'jpg'
50
+ assert_equal @options["close_photoshop_after_processing"], 2
51
+ end
52
+
53
+ should "get protocal sanitised url" do
54
+ result_url = @player.get_protocol_sanitised_url("http://geekandpoke.smugmug.com/gallery/5297189_ToNCK#323502743_kns4p")
55
+ assert !result_url.include?("http://")
56
+ end
57
+
58
+ should "get the host name from protocol sanitised url" do
59
+ actual_host_name = @player.get_host_name_from_protocol_sanitised_url("geekandpoke.smugmug.com/gallery/5297189_ToNCK#323502743_kns4p")
60
+ assert_equal actual_host_name, "geekandpoke.smugmug.com"
61
+ end
62
+
63
+ should "get the remaining url and the querystrings" do
64
+ remaining_url = @player.get_remaining_url_from_protocol_sanitised_url("geekandpoke.smugmug.com/gallery/5297189_ToNCK#323502743_kns4p")
65
+ assert_equal remaining_url, "/gallery/5297189_ToNCK#323502743_kns4p"
66
+ end
67
+
68
+ should "make net/http request and get response" do
69
+ response = @player.make_http_request('http://geekandpoke.smugmug.com/gallery/5297189_ToNCK#323502743_kns4p')
70
+ assert_not_nil response
71
+ end
72
+
73
+ should "collect all image (img) elements" do
74
+ elements = @player.collect_source_of_image_element(@response)
75
+ assert_equal elements.size(), 2
76
+ end
77
+
78
+ should "select the supplied extension elements" do
79
+ refined_elements = @player.select_the_supplied_extension_elements(["/img/spacer.gif", "/img/spacer.jpg"], "jpg")
80
+ assert_equal refined_elements.size(), 1
81
+ end
82
+
83
+ should "create destination directory" do
84
+ @player.create_destination_directory("players-init#{File::SEPARATOR}test_final")
85
+ assert dir_in_a_folder("players-init").include?("test_final")
86
+ end
87
+
88
+ should "build required images full url" do
89
+ @elements_with_full_url = @player.build_full_url("geekandpoke.smugmug.com", ["/img/spacer.gif", "/img/spacer.jpg"])
90
+ assert_equal @elements_with_full_url[0], "geekandpoke.smugmug.com/img/spacer.gif"
91
+ assert_equal @elements_with_full_url[1], "geekandpoke.smugmug.com/img/spacer.jpg"
92
+ end
93
+
94
+ should "get the image name" do
95
+ image_name = @player.get_image_name("/img/spacer.jpg")
96
+ assert_equal "spacer.jpg", image_name
97
+ end
98
+
99
+ should "download the images" do
100
+ @player.download_the_images(".", ["geekandpoke.smugmug.com/photos/323503251_vdoAH-Th.jpg"])
101
+ files_in_a_folder(".").include?("323503251_vdoAH-Th.jpg")
102
+ end
103
+
104
+ teardown do
105
+ FileUtils.rmtree("players-init") if test(?d, "players-init")
106
+ FileUtils.rm("323503251_vdoAH-Th.jpg") if File.exists?("323503251_vdoAH-Th.jpg")
107
+ end
108
+
109
+ end
110
+
111
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: andhapp-players
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Anuj Dutta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-04 00:00:00 -07:00
13
+ default_executable: players
14
+ dependencies: []
15
+
16
+ description: Players - Photoshop Layers
17
+ email: anuj@andhapp.com
18
+ executables:
19
+ - players
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - README.markdown
26
+ - Rakefile
27
+ - VERSION.yml
28
+ - bin/players
29
+ - lib/convert_images_to_photoshop_layers.vbs
30
+ - lib/options.yaml
31
+ - lib/pconstants.rb
32
+ - lib/players.rb
33
+ - players.gemspec
34
+ - test/ts_players.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/andhapp/players
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: Its a simple gem that enables the image download from a webpage and creation of a photoshop layered file from those images
61
+ test_files:
62
+ - test/ts_players.rb