imagebackup 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 19904efc55e1c01e746024dc5918741eab0639d7b9cc50dacc1cfec0223c5532
4
- data.tar.gz: abc4d7dc6e5a97d83149e6808ed92efca7949cfa5019bf6331698abb01cb4a8c
3
+ metadata.gz: 19afaa9b557d9f7f975671cbac434cc122ba66c45530b151ca1e3f2927fa721e
4
+ data.tar.gz: 99c50850195ec05e5689736a0e382596ab5513e5246e8e63abf8b15f03137527
5
5
  SHA512:
6
- metadata.gz: 626ad2725257da8b7aaa53942613b2a3a14baa698ba150c84e71a9e0d34029531f0bd74e50fd9f2bbbac5ae05d6a1fa60eca0d981622f82df22911f8a96b5fba
7
- data.tar.gz: a3268bfb3094c872553589d0f7cfaf0272fface7e6e2e3e3f31b4180024a08b8b69904b7c4579b3781258a915e73f97e26c957d0651f794d26a1cf946b3027d8
6
+ metadata.gz: 67bb185227e95535d45675263420321bdc42ae3c3616d446d4ef3592c301087f9a82f89216d9249f280f06762b492efdb234c0154892d668a3faeca2791d15d3
7
+ data.tar.gz: bfe84709a6a03bbcf7135683dd352cd6a0a70e2430a91b4276d53ff6980d12c15a3e70036b640f3e2a672eb327cf8b2493a6d09bbdc9dfe7fefe386012aad9b3
data/README.md CHANGED
@@ -7,13 +7,18 @@
7
7
 
8
8
  ## Dependencies:
9
9
 
10
- - **Ruby**, plus gems:
10
+ **Ruby** v2.3.0 or greater, plus gems:
11
11
  - exiv2
12
- - ffprober
13
- - Ruby modules:
12
+ - ffprober
13
+
14
+ Ruby modules:
14
15
  - fileutils.rb
15
- - json
16
+ - json
16
17
 
18
+ Bundler should handle all these dependencies. If for some reason it doesn't you can run this in terminal:
19
+ ```bash
20
+ $ gem install exiv2 ffprober
21
+ ```
17
22
 
18
23
  ## Usage:
19
24
 
@@ -0,0 +1,19 @@
1
+ mov,movie
2
+ mp4,movie
3
+ avi,movie
4
+ mpg,movie
5
+ vob,movie
6
+ 3gp,movie
7
+ mxf,movie
8
+ mlv,movie
9
+ dng,pic
10
+ tif,pic
11
+ jpg,pic
12
+ pef,pic
13
+ crw,pic
14
+ cr2,pic
15
+ arw,pic
16
+ raw,pic
17
+ gif,pic
18
+ gif,pic
19
+ gif,pic
@@ -0,0 +1,30 @@
1
+ require 'csv'
2
+
3
+ class FileTypes
4
+ def self.list
5
+ file_list = []
6
+ CSV.foreach("#{File.dirname(__FILE__)}/filetypes.csv") do |csv|
7
+ file_list << "**/*.#{csv[0]}"
8
+ file_list << "**/*.#{csv[0].upcase}"
9
+ end
10
+ return file_list
11
+ end
12
+
13
+ def self.add(ext=nil,type=nil)
14
+ unless ext
15
+ puts 'please enter the file type\'s extension, with or without the dot.'
16
+ ext = gets.strip
17
+ end
18
+ unless type
19
+ puts 'please enter "movie" or "pic" so we know what we\'re working with.'
20
+ type = gets.strip
21
+ end
22
+ ext = ext.to_s.gsub(/[*."]/,'')
23
+ p type
24
+ type = (type.downcase.include?('m')) ? "movie" : "pic"
25
+ puts "opening file \"#{File.dirname(__FILE__)}/filetypes.csv\""
26
+ CSV.open("#{File.dirname(__FILE__)}/filetypes.csv","a") do |csv|
27
+ p csv << [ext,type]
28
+ end
29
+ end
30
+ end
data/lib/imagebackup.rb CHANGED
@@ -1,48 +1,62 @@
1
1
  #!/usr/bin/env ruby
2
2
  require_relative 'imagebackup/version'
3
3
 
4
+ require_relative 'classes/filetypes'
5
+ require_relative 'methods/build_paths'
6
+ require_relative 'methods/get_dates'
7
+ require_relative 'methods/copy_pic'
8
+
4
9
  require 'exiv2'
5
10
  require 'fileutils'
6
11
  require 'ffprober'
7
12
  require 'json'
13
+ require 'getoptlong'
14
+
15
+ def main_loop(dest,dryrun=true)
16
+ file_types = FileTypes.list
17
+ Dir.glob(file_types).reverse_each do |f|
18
+
19
+ file = "#{Dir.pwd}/#{f}"
8
20
 
9
- def get_dates(file)
10
- begin
11
- image = Exiv2::ImageFactory.open(file)
12
- image.read_metadata
13
- date = image.exif_data.find { |v| v[0] == 'Exif.Image.DateTime' }
14
- date = date[1].split[0].gsub(':','-')
15
- rescue => exiv2_no_exifdata
16
- probe = Ffprober::Parser.from_file(file)
17
- date = probe.format.tags[:creation_time].split('T')[0]
21
+ parms = build_paths(dest,file,get_dates(file))
22
+ outfile = parms[0]
23
+ destpath = parms[1]
24
+
25
+ if File.exist?(outfile)
26
+ puts "\"#{outfile}\" already exists."
27
+ else
28
+ copy_pic(file,outfile,destpath,dryrun)
29
+ end
18
30
  end
19
- return date
20
31
  end
21
32
 
22
- dest = ARGV[0]
23
-
24
- file_types = [
25
- '**/*.CR2',
26
- '**/*.DNG',
27
- '**/*.MOV',
28
- '**/*.MP4',
29
- '**/*.JPG'
30
- ]
31
-
32
- Dir.glob(file_types).reverse_each do |f|
33
-
34
- file = "#{Dir.pwd}/#{f}"
35
-
36
- date = get_dates(file)
37
- destpath = "#{dest}/#{date}"
38
- destpath = destpath.gsub('//','/')
39
- outfile = "#{destpath}/#{File.basename(file)}"
40
- if File.exist?(outfile)
41
- puts "\"#{outfile}\" already exists."
42
- else
43
- puts "copying \"#{file}\" to \"#{outfile}\""
44
- FileUtils.mkdir_p(destpath)
45
- FileUtils.cp(file,outfile)
46
- end
47
- # sleep 0.1
33
+ # main_loop(ARGV[0])
34
+
35
+ dryrun = true
36
+
37
+ if (ARGV & ['-n','--dry-run']).any?
38
+ dryrun = true
39
+ ARGV.delete('-n')
40
+ ARGV.delete('--dry-run')
41
+ else
42
+ # FIXME: THIS IS REVERSED TO ALWAYS BE TRUE WHILE I'M TESTING. CHANGE BELOW TO false BEFORE DEPLOYMENT
43
+ dryrun = true
44
+ puts "dry run is disabled! i'm really going to mess with your files!"
45
+ end
46
+ if ARGV.include?('-a')#,'--add-filetype']).any?
47
+ ext=ARGV[ARGV.index('-a')+1]
48
+ type=ARGV[ARGV.index('-a')+2]
49
+ ARGV.slice!(ARGV.index('-a')..ARGV.index('-a')+2)
50
+ end
51
+ if ARGV.include?('--add-filetype')
52
+ ext=ARGV[ARGV.index('--add-filetype')+1]
53
+ type=ARGV[ARGV.index('--add-filetype')+2]
54
+ ARGV.slice!(ARGV.index('--add-filetype')..ARGV.index('--add-filetype')+2)
55
+ end
56
+ if ext
57
+ FileTypes.add(ext,type)
58
+ end
59
+
60
+ if ARGV
61
+ main_loop(ARGV[0])
48
62
  end
@@ -1,3 +1,3 @@
1
1
  module Imagebackup
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,7 @@
1
+ def build_paths(dest,file,date)
2
+ date = get_dates(file)
3
+ destpath = "#{dest}/#{date}"
4
+ destpath = destpath.gsub('//','/')
5
+ outfile = "#{destpath}/#{File.basename(file)}"
6
+ return [outfile,destpath]
7
+ end
@@ -0,0 +1,11 @@
1
+ def copy_pic(file,outfile,destpath,dryrun)
2
+ if dryrun
3
+ puts "pretending to copy \"#{file}\" to \"#{outfile}\""
4
+ puts "FileUtils.mkdir_p(#{destpath})"
5
+ puts "FileUtils.cp(#{file},#{outfile})"
6
+ else
7
+ puts "copying \"#{file}\" to \"#{outfile}\""
8
+ FileUtils.mkdir_p(destpath)
9
+ FileUtils.cp(file,outfile)
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ def get_dates(file)
2
+ begin
3
+ image = Exiv2::ImageFactory.open(file)
4
+ image.read_metadata
5
+ date = image.exif_data.find { |v| v[0] == 'Exif.Image.DateTime' }
6
+ date = date[1].split[0].gsub(':','-')
7
+ rescue => exiv2_no_exifdata
8
+ begin
9
+ probe = Ffprober::Parser.from_file(file)
10
+ date = probe.format.tags[:creation_time].split('T')[0]
11
+ rescue => invalid_or_corrupt_file
12
+ fileobj = File.new(file)
13
+ date = "#{fileobj.stat.ctime.year}-#{fileobj.stat.ctime.month}-#{fileobj.stat.ctime.day}"
14
+ end
15
+ end
16
+ return date
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imagebackup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - adrian-sal-kennedy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-10 00:00:00.000000000 Z
11
+ date: 2020-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: exiv2
@@ -55,8 +55,13 @@ files:
55
55
  - bin/console
56
56
  - bin/setup
57
57
  - imagebackup.gemspec
58
+ - lib/classes/filetypes.csv
59
+ - lib/classes/filetypes.rb
58
60
  - lib/imagebackup.rb
59
61
  - lib/imagebackup/version.rb
62
+ - lib/methods/build_paths.rb
63
+ - lib/methods/copy_pic.rb
64
+ - lib/methods/get_dates.rb
60
65
  homepage: https://github.com/adrian-sal-kennedy/imagebackup
61
66
  licenses:
62
67
  - MIT