renmov 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/renmov +11 -0
- data/features/rename_files.feature +16 -0
- data/features/support/env.rb +1 -1
- data/lib/renmov/basic_renamer.rb +46 -0
- data/lib/renmov/cli.rb +40 -0
- data/lib/renmov/version.rb +1 -1
- data/lib/renmov.rb +2 -0
- data/spec/renmov/basic_renamer_spec.rb +78 -0
- data/spec/renmov/cli_spec.rb +102 -0
- metadata +11 -3
data/bin/renmov
CHANGED
@@ -0,0 +1,16 @@
|
|
1
|
+
Feature: Rename Files
|
2
|
+
As a digital video collector
|
3
|
+
In order to organize my video files
|
4
|
+
I want to consistently rename my files
|
5
|
+
|
6
|
+
Scenario Outline: one file
|
7
|
+
Given an empty file named "<old>"
|
8
|
+
When I successfully run `renmov <old>`
|
9
|
+
Then a file named "<old>" should not exist
|
10
|
+
And a file named "<new>" should exist
|
11
|
+
|
12
|
+
Scenarios: tv shows
|
13
|
+
| old | new |
|
14
|
+
| TV.Show.S01E02.HDTV.x264-LOL.[VTV].mp4 | tv.show.s01e02.mp4 |
|
15
|
+
| The.TV.Show.S01E02.[VTV].avi | tv.show.s01e02.avi |
|
16
|
+
| TV.Show.2012.S01E02.x264-LOL.mp4 | tv.show.s01e02.mp4 |
|
data/features/support/env.rb
CHANGED
@@ -0,0 +1,46 @@
|
|
1
|
+
module Renmov
|
2
|
+
class BasicRenamer
|
3
|
+
attr_reader :filename
|
4
|
+
def initialize(filename)
|
5
|
+
@filename = filename.downcase
|
6
|
+
end
|
7
|
+
|
8
|
+
def rename
|
9
|
+
title = get_title
|
10
|
+
season = get_season
|
11
|
+
episode = get_episode
|
12
|
+
format = get_format
|
13
|
+
|
14
|
+
"#{title}.s#{season}e#{episode}.#{format}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_title
|
18
|
+
title = filename.dup
|
19
|
+
title.gsub!(/\Athe\./, '')
|
20
|
+
title.gsub!(/(\.20\d\d)?\.s\d\de\d\d.*/, '')
|
21
|
+
|
22
|
+
title
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_season
|
26
|
+
season = filename.dup
|
27
|
+
season.gsub!(/.*\.s(\d\d).*/, '\1')
|
28
|
+
|
29
|
+
season
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_episode
|
33
|
+
episode = filename.dup
|
34
|
+
episode.gsub!(/.*\.s\d\de(\d\d).*/, '\1')
|
35
|
+
|
36
|
+
episode
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_format
|
40
|
+
format = filename.dup
|
41
|
+
format.gsub!(/.*\.(...)\z/, '\1')
|
42
|
+
|
43
|
+
format
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/renmov/cli.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'renmov/basic_renamer'
|
4
|
+
|
5
|
+
module Renmov
|
6
|
+
class CLI
|
7
|
+
attr_reader :args
|
8
|
+
attr_accessor :options
|
9
|
+
attr_accessor :filenames
|
10
|
+
|
11
|
+
def initialize(args = ARGV, renamer = BasicRenamer)
|
12
|
+
@args = args
|
13
|
+
@filenames = []
|
14
|
+
@options = { verbose: false }
|
15
|
+
@renamer = renamer
|
16
|
+
end
|
17
|
+
|
18
|
+
def parse_options
|
19
|
+
optparse = OptionParser.new do |opts|
|
20
|
+
opts.on('-v', '--verbose', 'Output more information') do
|
21
|
+
options[:verbose] = true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
self.filenames = optparse.parse! args
|
26
|
+
end
|
27
|
+
|
28
|
+
def run
|
29
|
+
@filenames.each do |filename|
|
30
|
+
dirname = File.dirname(filename)
|
31
|
+
filename = File.basename(filename)
|
32
|
+
renamer = @renamer.new(filename)
|
33
|
+
newname = renamer.rename
|
34
|
+
FileUtils.mv("#{dirname}/#{filename}",
|
35
|
+
"#{dirname}/#{newname}",
|
36
|
+
verbose: options[:verbose])
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/renmov/version.rb
CHANGED
data/lib/renmov.rb
CHANGED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Renmov
|
4
|
+
describe BasicRenamer do
|
5
|
+
let(:renamer) { BasicRenamer.new(filename) }
|
6
|
+
describe '#rename' do
|
7
|
+
let(:filename) { 'TV.Show.S01E02.HDTV.test.mp4' }
|
8
|
+
|
9
|
+
it 'returns new filename' do
|
10
|
+
renamer.rename.should == 'tv.show.s01e02.mp4'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#get_title' do
|
15
|
+
context 'title is a regular title' do
|
16
|
+
let(:filename) { 'TV.Show.S01E02.HDTV.test.mp4' }
|
17
|
+
|
18
|
+
it 'returns only the title' do
|
19
|
+
renamer.get_title.should == 'tv.show'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'title begins with "the"' do
|
24
|
+
let(:filename) { 'The.TV.Show.S01E02.HDTV.test.mp4' }
|
25
|
+
|
26
|
+
it 'returns title without "the"' do
|
27
|
+
renamer.get_title.should == 'tv.show'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'title contains the year at the end' do
|
32
|
+
let(:filename) { 'TV.Show.2012.S01E02.HDTV.test.mp4' }
|
33
|
+
|
34
|
+
it 'returns title without the year' do
|
35
|
+
renamer.get_title.should == 'tv.show'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#get_season' do
|
41
|
+
context 'season is in regular format' do
|
42
|
+
let(:filename) { 'TV.Show.S01E02.HDTV.test.mp4' }
|
43
|
+
|
44
|
+
it 'returns only the season number' do
|
45
|
+
renamer.get_season.should == '01'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#get_episode' do
|
51
|
+
context 'episode is in regular format' do
|
52
|
+
let(:filename) { 'TV.Show.S01E02.HDTV.test.mp4' }
|
53
|
+
|
54
|
+
it 'returns only the episode number' do
|
55
|
+
renamer.get_episode.should == '02'
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '#get_format' do
|
61
|
+
context 'format is "mp4"' do
|
62
|
+
let(:filename) { 'TV.Show.S01E02.HDTV.test.mp4' }
|
63
|
+
|
64
|
+
it 'returns only "mp4"' do
|
65
|
+
renamer.get_format.should == 'mp4'
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'format is "avi"' do
|
70
|
+
let(:filename) { 'TV.Show.S01E02.HDTV.test.avi' }
|
71
|
+
|
72
|
+
it 'returns only "avi"' do
|
73
|
+
renamer.get_format.should == 'avi'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module Renmov
|
5
|
+
describe CLI do
|
6
|
+
describe '#parse_options' do
|
7
|
+
let(:default_options) { { verbose: false } }
|
8
|
+
before(:each) do
|
9
|
+
@cli = CLI.new(options + filenames)
|
10
|
+
@cli.parse_options
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with no options provided' do
|
14
|
+
let(:options) { [] }
|
15
|
+
|
16
|
+
context 'with no filenames provided' do
|
17
|
+
let(:filenames) { [] }
|
18
|
+
|
19
|
+
it 'keeps default options' do
|
20
|
+
@cli.options.should == default_options
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'sets filenames to []' do
|
24
|
+
@cli.filenames.should == filenames
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'with one filename provided' do
|
29
|
+
let(:filenames) { ['TV.Show.S01E02.HDTV.test.mp4'] }
|
30
|
+
|
31
|
+
it 'keeps default options' do
|
32
|
+
@cli.options.should == default_options
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'sets filenames to filename provided' do
|
36
|
+
@cli.filenames.should == filenames
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with verbose option provided' do
|
42
|
+
let(:options) { ['-v'] }
|
43
|
+
|
44
|
+
context 'with no filenames provided' do
|
45
|
+
let(:filenames) { [] }
|
46
|
+
|
47
|
+
it 'merges verbose with default options' do
|
48
|
+
@cli.options.should == default_options.merge(verbose: true)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'sets filenames to []' do
|
52
|
+
@cli.filenames.should == filenames
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'with one filename provided' do
|
57
|
+
let(:filenames) { ['TV.Show.S01E02.HDTV.test.mp4'] }
|
58
|
+
|
59
|
+
it 'merges verbose with default options' do
|
60
|
+
@cli.options.should == default_options.merge(verbose: true)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'sets filenames to filename provided' do
|
64
|
+
@cli.filenames.should == filenames
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '#run' do
|
71
|
+
let(:tmpdir) { File.expand_path('../../../tmp', __FILE__) }
|
72
|
+
before(:each) do
|
73
|
+
FileUtils.touch filenames
|
74
|
+
cli = CLI.new(filenames)
|
75
|
+
cli.parse_options
|
76
|
+
cli.run
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'with one filename provided' do
|
80
|
+
let(:filenames) { ["#{tmpdir}/TV.Show.S01E02.HDTV.test.mp4"] }
|
81
|
+
let(:newnames) { ["#{tmpdir}/tv.show.s01e02.mp4"] }
|
82
|
+
|
83
|
+
it 'properly renames file' do
|
84
|
+
File.exists?(filenames[0]).should_not be_true
|
85
|
+
File.exists?(newnames[0]).should be_true
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'with multiple filenames provided' do
|
90
|
+
let(:filenames) { ["#{tmpdir}/TV.Show.S01E02.HDTV.test.mp4",
|
91
|
+
"#{tmpdir}/The.TV.Show.S02E03.LOL.avi"] }
|
92
|
+
let(:newnames) { ["#{tmpdir}/tv.show.s01e02.mp4",
|
93
|
+
"#{tmpdir}/tv.show.s02e03.avi"] }
|
94
|
+
|
95
|
+
it 'properly renames all files' do
|
96
|
+
filenames.each { |f| File.exists?(f).should_not be_true }
|
97
|
+
newnames.each { |n| File.exists?(n).should be_true }
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: renmov
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -68,10 +68,15 @@ extra_rdoc_files: []
|
|
68
68
|
files:
|
69
69
|
- Rakefile
|
70
70
|
- bin/renmov
|
71
|
+
- features/rename_files.feature
|
71
72
|
- features/support/env.rb
|
72
73
|
- lib/renmov.rb
|
74
|
+
- lib/renmov/basic_renamer.rb
|
75
|
+
- lib/renmov/cli.rb
|
73
76
|
- lib/renmov/version.rb
|
74
77
|
- renmov.gemspec
|
78
|
+
- spec/renmov/basic_renamer_spec.rb
|
79
|
+
- spec/renmov/cli_spec.rb
|
75
80
|
- spec/renmov/version_spec.rb
|
76
81
|
- spec/spec_helper.rb
|
77
82
|
homepage: http://github.com/blumbri/renmov
|
@@ -97,8 +102,11 @@ rubyforge_project:
|
|
97
102
|
rubygems_version: 1.8.24
|
98
103
|
signing_key:
|
99
104
|
specification_version: 3
|
100
|
-
summary: renmov-0.0.
|
105
|
+
summary: renmov-0.0.1
|
101
106
|
test_files:
|
107
|
+
- features/rename_files.feature
|
102
108
|
- features/support/env.rb
|
109
|
+
- spec/renmov/basic_renamer_spec.rb
|
110
|
+
- spec/renmov/cli_spec.rb
|
103
111
|
- spec/renmov/version_spec.rb
|
104
112
|
- spec/spec_helper.rb
|