tvshow_renamer 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/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2012 François Klingler
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ tvshow_renamer is a simple command line utility to rename automagically the tv show files you downloaded to a good format.
2
+
3
+ Thanks to [skamlet](https://github.com/skamlet) for his work on [tv-show-renamer](https://github.com/skamlet/tv-show-renamer), the java utility upon which I have based my work.
4
+
5
+
6
+ Installation
7
+ ------------
8
+
9
+ `gem install tvshow_renamer`
10
+
11
+
12
+ Usage
13
+ -----
14
+
15
+ `tvshow_renamer [options] file ...`
16
+
17
+ You can specify many files to rename.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'tvshow_renamer'
4
+
5
+ TVShowRenamer::CLI.start
@@ -0,0 +1,29 @@
1
+ require 'optparse'
2
+
3
+ module TVShowRenamer
4
+ class CLI
5
+ def self.start
6
+ options = {}
7
+ opts = OptionParser.new do |opts|
8
+ opts.banner = "Usage: tvshow_renamer [options] <tvshow_name> file|directory ..."
9
+
10
+ opts.on("-v", "--verbose", "Run verbosely") do |v|
11
+ options[:verbose] = v
12
+ end
13
+
14
+ opts.on_tail("-h", "--help", "Show this message") do
15
+ puts opts
16
+ exit
17
+ end
18
+ end
19
+ opts.parse!
20
+
21
+ if ARGV.length <= 1
22
+ puts opts
23
+ else
24
+ tvshow_name = ARGV.shift
25
+ Renamer.new(tvshow_name, options).rename(ARGV)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+ require 'fileutils'
3
+
4
+ module TVShowRenamer
5
+ class Renamer
6
+ attr_accessor :tvshow_name
7
+
8
+ EXTENSIONS = %w( .mkv .avi .mp4 .srt )
9
+
10
+ def initialize(tvshow_name, options = {})
11
+ @tvshow_name = tvshow_name
12
+ @options = options
13
+ end
14
+
15
+ def rename(entries = [])
16
+ entries.each do |entry|
17
+ if File.exists?(entry)
18
+ if File.directory?(entry)
19
+ rename_dir(entry)
20
+ else
21
+ rename_file(entry)
22
+ end
23
+ else
24
+ $stderr.puts "Warning -- #{entry} does not exist!"
25
+ end
26
+ end
27
+ end
28
+
29
+ def rename_dir(dirname)
30
+ puts "Processing directory #{dirname}" if @options[:verbose]
31
+ Dir.glob(dirname + '/**').each do |filename|
32
+ rename_file(filename)
33
+ end
34
+ end
35
+
36
+ def rename_file(filename)
37
+ filename = File.expand_path(filename)
38
+ extension = File.extname(filename)
39
+
40
+ if EXTENSIONS.include?(extension.downcase)
41
+ dirname = File.dirname(filename)
42
+ basename = File.basename(filename)
43
+
44
+ FileUtils.mv filename, File.join(dirname, new_basename(basename))
45
+ end
46
+ end
47
+
48
+ def new_basename(basename)
49
+ regex = /(?<season>\d{1,2})(e|x|\.)?(?<episode>\d{2,})/i
50
+ match = regex.match basename
51
+ "#{@tvshow_name} - %02ix%02i#{File.extname(basename)}" % [match[:season], match[:episode]]
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module TVShowRenamer
2
+ Version = VERSION = '0.1'
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'tvshow_renamer/cli'
2
+ require 'tvshow_renamer/renamer'
@@ -0,0 +1,22 @@
1
+ require 'test/unit'
2
+ require 'tvshow_renamer'
3
+
4
+ class TVShowRenamerTest < Test::Unit::TestCase
5
+ TVSHOW_NAME = 'Famous Show'
6
+
7
+ def setup
8
+ @renamer = TVShowRenamer::Renamer.new(TVSHOW_NAME)
9
+ end
10
+
11
+ def test_new_basename
12
+ tests = {
13
+ 'FamousShow.S05E01.DVDRip.XviD-BLAH.avi' => 'Famous Show - 05x01.avi',
14
+ 'FamousShow.12x03.DVDRip.XviD-BLAH.avi' => 'Famous Show - 12x03.avi',
15
+ 'FamousShow.0103.720p.mkv' => 'Famous Show - 01x03.mkv',
16
+ 'FS.503.avi' => 'Famous Show - 05x03.avi',
17
+ 'Famous.Show.03.04.avi' => 'Famous Show - 03x04.avi'
18
+ }.each do |k,v|
19
+ assert_equal v, @renamer.new_basename(k)
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tvshow_renamer
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - François Klingler
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Utility to rename TV Show files to a correct format
15
+ email: francois@fklingler.com
16
+ executables:
17
+ - tvshow_renamer
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - Rakefile
24
+ - lib/tvshow_renamer/cli.rb
25
+ - lib/tvshow_renamer/renamer.rb
26
+ - lib/tvshow_renamer/version.rb
27
+ - lib/tvshow_renamer.rb
28
+ - test/test_tvshow_renamer.rb
29
+ - bin/tvshow_renamer
30
+ homepage: http://github.com/fklingler/tvshow_renamer
31
+ licenses:
32
+ - MIT
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.23
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: TV Show files renamer
55
+ test_files: []