imagut 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rdoc", "~> 3.12"
10
+ gem "bundler", "~> 1.1.3"
11
+ gem "jeweler", "~> 1.8.3"
12
+ gem "simplecov", ">= 0"
13
+ gem "nokogiri", ">= 1.5.4"
14
+ gem "filerenamer", ">= 0.0.0"
15
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 ippei94da
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = imagut
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to imagut
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 ippei94da. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "imagut"
18
+ gem.homepage = "http://github.com/ippei94da/imagut"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{IMAGe UTilities. File renamer, downloader, etc.}
21
+ gem.description = %Q{Library to deal image files. This includes file renamer with NIKON NEF file, downloader for images on a web page.}
22
+ gem.email = "ippei94da@gmail.com"
23
+ gem.authors = ["ippei94da"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ #require 'rcov/rcovtask'
36
+ #Rcov::RcovTask.new do |test|
37
+ # test.libs << 'test'
38
+ # test.pattern = 'test/**/test_*.rb'
39
+ # test.verbose = true
40
+ # test.rcov_opts << '--exclude "gems/*"'
41
+ #end
42
+
43
+ task :default => :test
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "imagut #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,69 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ STORAGE_DIR = ENV["HOME"] + "/image/download"
5
+ LOG_DIR = STORAGE_DIR + "/log"
6
+
7
+ require "pp"
8
+ require "date"
9
+
10
+ require "rubygems"
11
+ gem "nokogiri"
12
+ require "nokogiri"
13
+ require "open-uri"
14
+
15
+ gem "imagut"
16
+ require "imagut/urllogger.rb"
17
+
18
+ def anchored_images(url)
19
+ doc = Nokogiri::HTML(open(url))
20
+
21
+ #pp doc
22
+ #pp doc.methods.sort
23
+ #pp doc.children
24
+ #pp doc.xpath("//a").each { |i| pp i }
25
+ #pp doc.xpath("//a")[10]
26
+ #pp doc.xpath("//a")[10].attributes["href"].value
27
+ results = doc.xpath("//a").map{|a|
28
+ next unless a.attributes.has_key?("href")
29
+ a.attributes["href"].value
30
+ }.compact.select{|str| str =~ /(jpg|png|gif)$/}
31
+ results
32
+ end
33
+
34
+ #常に log に追記する。
35
+ # log に url が含まれていても、
36
+ # 最後にアクセスしようとした日を更新するため。
37
+ # 古いのを削除とかはしない。
38
+ #
39
+ #log を確認する。
40
+ # url が含まれていれば false を返す。
41
+ # url が含まれていなければ、log に書き込んで取得。
42
+ def storage(url)
43
+ command = "wget --directory-prefix=#{STORAGE_DIR} #{url}"
44
+ puts command
45
+ system command
46
+ end
47
+
48
+ #anchors = anchored_images('/home/ippei/tmp/getimages/1457652.html')
49
+ #doc = Nokogiri::HTML(open('http://blog.livedoor.jp/wakusoku/archives/1457652.html'))
50
+ ul = UrlLogger.new(LOG_DIR)
51
+
52
+ ARGV.each do |url|
53
+ if ul.include?(url)
54
+ puts "Already downloaded: #{url}"
55
+ else
56
+ anchors = anchored_images(url)
57
+ anchors.each do |img_url|
58
+ if ul.include?(img_url)
59
+ puts "Already exist: #{img_url}"
60
+ else
61
+ puts "Getting: #{img_url}"
62
+ storage(img_url)
63
+ sleep rand(3)
64
+ end
65
+ ul.write img_url
66
+ end
67
+ end
68
+ ul.write url
69
+ end
@@ -0,0 +1,42 @@
1
+ #! /usr/bin/ruby -w
2
+ # last-modified: 2012/06/13 17:00:21.
3
+
4
+ # Nikon NEF ファイルの撮影日時をファイル名の先頭に付加する。
5
+
6
+
7
+ NEF_DIR_PREFIX = "#{ENV["HOME"]}/image/photo/undone"
8
+
9
+
10
+ require "pp"
11
+ require "rubygems"
12
+
13
+ gem "filerenamer"
14
+ require "filerenamer/filerenamer.rb"
15
+ require "filerenamer/filerenameroptionparser.rb"
16
+
17
+ gem "imagut"
18
+ require "imagut/nefparser.rb"
19
+
20
+ op = FileRenamerOptionParser.new
21
+ op.parse!(ARGV)
22
+
23
+ fr = FileRenamer.new(op.options)
24
+ np = NefParser.new
25
+ fr.execute(ARGV) do |filename|
26
+ #pp filename
27
+ new_name = ""
28
+ begin
29
+ str = File.open(filename, "r").read(391)
30
+ date = np.taken_date(str)
31
+ new_name = (NEF_DIR_PREFIX + date.strftime("/%Y/%m/%d/%H%M%S_") + filename)
32
+ new_name.sub!(/\.NEF$/, ".nef")
33
+ #new_name = date.strftime("%Y/%m/%d/") + filename
34
+ # 秒までだと、連写したときに重複ファイル名になりうる。
35
+ # 何枚目かだけだと、1日の途中でメモリカードを交換したときに
36
+ # 重複して順番が滅茶苦茶になる。
37
+ # このプログラムを使用した後、更に適当にリネームすることを想定。
38
+ rescue NefParserNotNefError
39
+ new_name = nil
40
+ end
41
+ new_name
42
+ end
@@ -0,0 +1,20 @@
1
+ #! /usr/bin/ruby
2
+
3
+ #IMAGEMAGIC_PATH = '/usr/local/bin/convert'
4
+ IMAGEMAGIC_PATH = '/usr/bin/convert'
5
+ TARGET_SIZE = '"1200x960>"' # '>' means to shrink but not to expand.
6
+
7
+ oldFileNameAry = ARGV
8
+
9
+ oldFileNameAry.each do |oldFileName|
10
+ #extention = File.extname(oldFileName)
11
+ #p newFileName = oldFileName.sub(/#{extention}$/, "-s#{extention}")
12
+ newFileName = oldFileName
13
+
14
+ commandLine = IMAGEMAGIC_PATH + ' ' + oldFileName + ' -resize ' + TARGET_SIZE +
15
+ ' ' + newFileName
16
+ print commandLine + "\n"
17
+ system(commandLine)
18
+ end
19
+
20
+ print "\a"
File without changes
@@ -0,0 +1,43 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ class NefParserNotNefError < StandardError; end
5
+ class NefParserTypeError < StandardError; end
6
+
7
+ #
8
+ # Nikon NEF ファイルのなんちゃってパーサ。
9
+ #
10
+ class NefParser
11
+ #
12
+ def initialize
13
+ end
14
+
15
+ # 撮影日時を返す(つもり)。
16
+ #
17
+ # 頑張って解析はしてない。
18
+ # 日付と思われる部分だけ抽出している。
19
+ # 解析には 372+19 = 391 バイトあれば足りる。
20
+ # サンプル NEF ファイルに撮影日時と思しき文字列は 4つあるけど、
21
+ # 詳細が分かるまではとりあえず最初の1個のみ Time object にして返す。
22
+ # a19 の部分が時刻の文字列。
23
+ # 4つあるんだけど、それぞれの差異 or 冗長なのかが分からない。
24
+ # 引数 data は File.read で読まれるような文字列
25
+ def taken_date(data)
26
+ unless data.is_a?(String)
27
+ message = "Not string data. Use File#read, not File#readlines."
28
+ raise NefParserTypeError, message
29
+ end
30
+
31
+ datetime_str = data.unpack("a372a19a57a19a397a19a1a19")[1]
32
+ #p datetime_str
33
+ #p datetime_str.split(/[ :]/)
34
+ begin
35
+ @date = Time.mktime( * datetime_str.split(/[ :]/) )
36
+ rescue
37
+ raise NefParserNotNefError, "Cannot find taken time."
38
+ end
39
+ return @date
40
+ end
41
+
42
+ end
43
+
@@ -0,0 +1,34 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "date"
5
+
6
+ #
7
+ #
8
+ #
9
+ class UrlLogger
10
+ #LOG_DIR = ENV["HOME"] + "/image/download/log"
11
+
12
+ class InitializeError < Exception; end
13
+
14
+ # dir is a directory to storage logs.
15
+ def initialize(dir)
16
+ raise InitializeError, "#{dir} not exist" unless File.directory?(dir)
17
+ @dir = dir
18
+ end
19
+
20
+ # return true if entry is included in logdir.
21
+ def include?(entry)
22
+ Dir.glob(@dir + "/*.log").each do |file|
23
+ return true if File.open(file, "r").readlines.map{|i|i.chomp}.include?(entry)
24
+ end
25
+ return false
26
+ end
27
+
28
+ def write(entry,
29
+ filename = @dir + (DateTime.now.strftime("/%C%g%m%d.log")))
30
+ File.open(filename, "a") { |io| io.puts entry }
31
+ end
32
+
33
+ end
34
+
Binary file
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ require 'imagut'
15
+
16
+ class Test::Unit::TestCase
17
+ end
@@ -0,0 +1,31 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require 'helper'
5
+ require "test/unit"
6
+ require "imageutils/nefparser.rb"
7
+
8
+ class TC_NefParser < Test::Unit::TestCase
9
+ def setup
10
+ @np00 = NefParser.new
11
+ end
12
+
13
+ def test_taken_date
14
+
15
+ data = File.open("test/001.nef", "r").read
16
+ assert_equal(Time.mktime(2011, 8, 12, 5, 50, 33), @np00.taken_date(data))
17
+
18
+ data = File.open("test/001.nef", "r").read(372+19)
19
+ assert_equal(Time.mktime(2011, 8, 12, 5, 50, 33), @np00.taken_date(data))
20
+
21
+ # String でないときに例外。
22
+ data = File.open("test/001.nef", "r").readlines
23
+ assert_raise(NefParserTypeError){@np00.taken_date(data)}
24
+
25
+ # NEF と異なるときに例外のテスト
26
+ assert_raise(NefParserNotNefError){@np00.taken_date("Not NEF data")}
27
+
28
+ end
29
+
30
+ end
31
+
@@ -0,0 +1,57 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require 'helper'
5
+ require "fileutils"
6
+ require "test/unit"
7
+ require "imageutils/urllogger.rb"
8
+
9
+ class TC_UrlLogger < Test::Unit::TestCase
10
+ def setup
11
+ @ul00 = UrlLogger.new("test/urllogger")
12
+ end
13
+
14
+ def test_initialize
15
+ assert_raises(UrlLogger::InitializeError){ UrlLogger.new("") }
16
+ end
17
+
18
+ def test_include?
19
+ assert_equal(true , @ul00.include?("http://a.b.c/d0.html"))
20
+ assert_equal(true , @ul00.include?("http://a.b.c/d1.html"))
21
+ assert_equal(true , @ul00.include?("http://a.b.c/d2.html"))
22
+ assert_equal(true , @ul00.include?("http://a.b.c/d3.html"))
23
+ assert_equal(false, @ul00.include?("http://a.b.c/d4.html"))
24
+ end
25
+
26
+ def test_write_with_filename
27
+ log_file = "test/urllogger/dummy.log"
28
+ FileUtils.rm log_file if File.exist? log_file
29
+
30
+ assert_equal(false, @ul00.include?("a"))
31
+ assert_equal(false, @ul00.include?("b"))
32
+ @ul00.write("a", log_file)
33
+ @ul00.write("b", log_file)
34
+ assert_equal(true , File.exist?(log_file))
35
+ assert_equal(true , @ul00.include?("a"))
36
+ assert_equal(true , @ul00.include?("b"))
37
+
38
+ FileUtils.rm log_file if File.exist? log_file
39
+ end
40
+
41
+ def test_write_without_filename
42
+ log_file = DateTime.now.strftime("test/urllogger/%C%g%m%d.log")
43
+ FileUtils.rm log_file if File.exist? log_file
44
+
45
+ assert_equal(false, @ul00.include?("a"))
46
+ assert_equal(false, @ul00.include?("b"))
47
+ @ul00.write("a")
48
+ @ul00.write("b")
49
+ assert_equal(true , File.exist?(log_file))
50
+ assert_equal(true , @ul00.include?("a"))
51
+ assert_equal(true , @ul00.include?("b"))
52
+ FileUtils.rm log_file if File.exist? log_file
53
+ end
54
+
55
+
56
+ end
57
+
@@ -0,0 +1,2 @@
1
+ http://a.b.c/d0.html
2
+ http://a.b.c/d1.html
@@ -0,0 +1,2 @@
1
+ http://a.b.c/d2.html
2
+ http://a.b.c/d3.html
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imagut
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ippei94da
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: &80109500 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.12'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *80109500
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &80137950 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.1.3
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *80137950
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &80137560 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.8.3
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *80137560
47
+ - !ruby/object:Gem::Dependency
48
+ name: simplecov
49
+ requirement: &80137250 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *80137250
58
+ - !ruby/object:Gem::Dependency
59
+ name: nokogiri
60
+ requirement: &80136960 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: 1.5.4
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *80136960
69
+ - !ruby/object:Gem::Dependency
70
+ name: filerenamer
71
+ requirement: &80136470 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: 0.0.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *80136470
80
+ description: Library to deal image files. This includes file renamer with NIKON NEF
81
+ file, downloader for images on a web page.
82
+ email: ippei94da@gmail.com
83
+ executables:
84
+ - getimage
85
+ - importnef
86
+ - resizeimage
87
+ extensions: []
88
+ extra_rdoc_files:
89
+ - LICENSE.txt
90
+ - README.rdoc
91
+ files:
92
+ - .document
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.rdoc
96
+ - Rakefile
97
+ - VERSION
98
+ - bin/getimage
99
+ - bin/importnef
100
+ - bin/resizeimage
101
+ - lib/imagut.rb
102
+ - lib/imagut/nefparser.rb
103
+ - lib/imagut/urllogger.rb
104
+ - test/001.nef
105
+ - test/helper.rb
106
+ - test/test_nefparser.rb
107
+ - test/test_urllogger.rb
108
+ - test/urllogger/20120523.log
109
+ - test/urllogger/20120524.log
110
+ homepage: http://github.com/ippei94da/imagut
111
+ licenses:
112
+ - MIT
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ segments:
124
+ - 0
125
+ hash: -266147949
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.11
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: IMAGe UTilities. File renamer, downloader, etc.
138
+ test_files: []