media-renamer 0.0.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/lib/media-renamer.rb +4 -0
- data/lib/renamer.rb +162 -0
- metadata +51 -0
data/lib/renamer.rb
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
#renamer.rb
|
|
2
|
+
#Currently configured to only rename JPG and TIF files (using EXIFR to extract metadata)
|
|
3
|
+
|
|
4
|
+
require 'exifr'
|
|
5
|
+
#require 'scrapers/jpg_tif.rb'
|
|
6
|
+
#require 'scrapers/mp3_m4a_wav_flac_aiff.rb'
|
|
7
|
+
|
|
8
|
+
class FileNotValidError < StandardError ; end
|
|
9
|
+
class InvalidArgumentError < StandardError ; end
|
|
10
|
+
class UnsupportedFileTypeError < StandardError ; end
|
|
11
|
+
|
|
12
|
+
class Renamer
|
|
13
|
+
|
|
14
|
+
attr_accessor :naming_scheme # => array of strings and literals used to construct filenames
|
|
15
|
+
|
|
16
|
+
def initialize
|
|
17
|
+
@naming_scheme = ["Renamed-default-"]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def setNamingScheme(arr = [])
|
|
21
|
+
@naming_scheme = setScheme(arr)
|
|
22
|
+
end
|
|
23
|
+
#Input: list of URIs in the form of an array
|
|
24
|
+
#Output: hash of "file name pairs." old_file => new_file
|
|
25
|
+
#Accepts optional arguments: :scheme (array of strings and symbols specifying file naming convention)
|
|
26
|
+
def generateRenameList(uri_list = [], args = {})
|
|
27
|
+
if args[:scheme] != nil && args[:scheme].is_a?(Array) && !args[:scheme].empty?
|
|
28
|
+
scheme = setScheme(args[:scheme])
|
|
29
|
+
else
|
|
30
|
+
scheme = @naming_scheme
|
|
31
|
+
end
|
|
32
|
+
unless !uri_list.nil? && uri_list.is_a?(Array)
|
|
33
|
+
raise InvalidArgumentError
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
filename_pairs = {}
|
|
38
|
+
uri_list.each do |i|
|
|
39
|
+
new_string = handleFile(i, scheme)
|
|
40
|
+
#If this is a valid file path, add it to the filename_pairs
|
|
41
|
+
#puts "New file rename added: #{new_string}"
|
|
42
|
+
if new_string != nil && new_string != ""
|
|
43
|
+
filename_pairs[i] = new_string
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
return filename_pairs
|
|
48
|
+
|
|
49
|
+
rescue InvalidArgumentError => arg_e
|
|
50
|
+
puts arg_e
|
|
51
|
+
puts "Invalid arguments provided. Expected: uri_list = [], args = {}"
|
|
52
|
+
puts arg_e.backtrace.inspect
|
|
53
|
+
rescue => e
|
|
54
|
+
puts e
|
|
55
|
+
puts e.message
|
|
56
|
+
puts e.backtrace.inspect
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def overwrite(renames_hash = {})
|
|
60
|
+
renames_hash.each do |old_name, new_name|
|
|
61
|
+
begin
|
|
62
|
+
#error/integrity checking on old_name and new_name
|
|
63
|
+
raise FileNotValidError, "Could not access specified source file: #{i}." unless old_name.is_a?(String) && File.exists?(old_name)
|
|
64
|
+
raise FileNotValidError, "New file name provided is not a string" unless new_name.is_a?(String)
|
|
65
|
+
|
|
66
|
+
File.rename(File.absolute_path(old_name),File.dirname(File.absolute_path(old_name)) + "/" + new_name)
|
|
67
|
+
|
|
68
|
+
#check that renamed file exists
|
|
69
|
+
unless new_name.is_a?(String) && File.exists?(new_name)
|
|
70
|
+
raise RenameFailedError, "Could not successfuly rename file: #{old_name} => #{new_name}."
|
|
71
|
+
end
|
|
72
|
+
rescue => e
|
|
73
|
+
puts "Ignoring rename for #{old_name} => #{new_name}"
|
|
74
|
+
puts e
|
|
75
|
+
puts e.backtrace.inspect
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
#Routes metadata scrape based on file type (currently relies on extension - future version should use MIME)
|
|
81
|
+
#currently assumes file was checked for validity in calling code
|
|
82
|
+
def getFileMetadata(file)
|
|
83
|
+
|
|
84
|
+
#LOAD EXIF DATA
|
|
85
|
+
case File.extname(file)
|
|
86
|
+
when '.jpg'
|
|
87
|
+
getJpegData(file)
|
|
88
|
+
when '.tif'
|
|
89
|
+
getTiffData(file)
|
|
90
|
+
else
|
|
91
|
+
raise UnsupportedFileTypeError, "Error processing #{file}"
|
|
92
|
+
end
|
|
93
|
+
#otherwise, outsource
|
|
94
|
+
rescue UnsupportedFileTypeError => e
|
|
95
|
+
puts "Could not process file: Extension #{File.extname(file)} is not supported."
|
|
96
|
+
puts e.backtrace.inspect
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def getJpegData(file)
|
|
100
|
+
meta = EXIFR::JPEG.new(file)
|
|
101
|
+
return meta.to_hash
|
|
102
|
+
#!!! Rescue from common file-related and exifr-related errors here
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def getTiffData(file)
|
|
106
|
+
meta = EXIFR::TIFF.new(file)
|
|
107
|
+
return meta.to_hash
|
|
108
|
+
#!!! Rescue from common file-related and exifr-related errors here
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
private
|
|
113
|
+
def handleFile(file, scheme)
|
|
114
|
+
#Check file is real
|
|
115
|
+
unless file.is_a?(String) && File.exists?(file)
|
|
116
|
+
raise FileNotValidError, "Could not access specified file file: #{file}."
|
|
117
|
+
end
|
|
118
|
+
#convert URI (i) to absolute path
|
|
119
|
+
|
|
120
|
+
#get metadata hash for this file (i)
|
|
121
|
+
metadata = getFileMetadata(File.absolute_path(file))
|
|
122
|
+
#build URI string
|
|
123
|
+
new_string = ""
|
|
124
|
+
scheme.each do |j|
|
|
125
|
+
if j.is_a?(String) then new_string += j
|
|
126
|
+
elsif j.is_a?(Symbol)
|
|
127
|
+
begin
|
|
128
|
+
raise EmptyMetadataError unless metadata[j] != nil
|
|
129
|
+
new_string += metadata[j].to_s
|
|
130
|
+
rescue => e
|
|
131
|
+
puts "Could not get string for metadata tag provided in scheme: #{j} for file #{file}."
|
|
132
|
+
puts "Ignoring file #{file}"
|
|
133
|
+
puts e.backtrace.inspect
|
|
134
|
+
return nil
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
#puts "Found file metadata: #{metadata[:date_time]}"
|
|
139
|
+
return new_string + File.extname(file)
|
|
140
|
+
rescue FileNotValidError => e
|
|
141
|
+
puts ("Ignoring file #{file}")
|
|
142
|
+
puts e
|
|
143
|
+
puts e.backtrace
|
|
144
|
+
return nil
|
|
145
|
+
rescue => e
|
|
146
|
+
puts e.message
|
|
147
|
+
puts e.backtrace.inspect
|
|
148
|
+
return nil
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def setScheme(input_arr = [])
|
|
152
|
+
clean_scheme = []
|
|
153
|
+
input_arr.each do |i|
|
|
154
|
+
if i.is_a?(String) || i.is_a?(Symbol)
|
|
155
|
+
clean_scheme << i
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
return clean_scheme
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
|
metadata
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: media-renamer
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Stephen Johnson
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2015-01-29 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: ! 'Provides a set of functions for dynamically renaming files using their
|
|
15
|
+
metadata, according to a customizable taxonomy. For example, use bulk-renamer to
|
|
16
|
+
set filenames for a directory of photos to a standard such as: "<date-taken> - Ski
|
|
17
|
+
Vacation.jpg". Currently supports only JPEG and TIFF files. Future releases will
|
|
18
|
+
include support for music and additional image files.'
|
|
19
|
+
email: djeserkare@gmail.com
|
|
20
|
+
executables: []
|
|
21
|
+
extensions: []
|
|
22
|
+
extra_rdoc_files: []
|
|
23
|
+
files:
|
|
24
|
+
- lib/renamer.rb
|
|
25
|
+
- lib/media-renamer.rb
|
|
26
|
+
homepage: http://rubygems.org/gems/bulk-renamer
|
|
27
|
+
licenses:
|
|
28
|
+
- MIT
|
|
29
|
+
post_install_message:
|
|
30
|
+
rdoc_options: []
|
|
31
|
+
require_paths:
|
|
32
|
+
- lib
|
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
34
|
+
none: false
|
|
35
|
+
requirements:
|
|
36
|
+
- - ! '>='
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '0'
|
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
|
+
none: false
|
|
41
|
+
requirements:
|
|
42
|
+
- - ! '>='
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: '0'
|
|
45
|
+
requirements: []
|
|
46
|
+
rubyforge_project:
|
|
47
|
+
rubygems_version: 1.8.23
|
|
48
|
+
signing_key:
|
|
49
|
+
specification_version: 3
|
|
50
|
+
summary: Rename files in bulk based on file metadata.
|
|
51
|
+
test_files: []
|