moviesort 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'jeweler'
2
+ require 'rake/testtask'
2
3
 
3
4
  Jeweler::Tasks.new do |gemspec|
4
5
  gemspec.name = "moviesort"
@@ -6,6 +7,7 @@ Jeweler::Tasks.new do |gemspec|
6
7
  gemspec.email = "luke@lukeredpath.co.uk"
7
8
  gemspec.authors = ["Luke Redpath"]
8
9
  gemspec.executables << 'moviesort'
10
+ gemspec.add_dependency('trollop')
9
11
  end
10
12
 
11
13
  Jeweler::GemcutterTasks.new
@@ -13,3 +15,9 @@ Jeweler::GemcutterTasks.new
13
15
  task :push_tiny => ['version:bump:patch', 'gemcutter:release']
14
16
  task :push_minor => ['version:bump:minor', 'gemcutter:release']
15
17
  task :push_major => ['version:bump:major', 'gemcutter:release']
18
+
19
+ Rake::TestTask.new('test') do |t|
20
+ t.libs << "test" << "lib"
21
+ t.pattern = "test/**/*_test.rb"
22
+ end
23
+ task :default => :test
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -11,12 +11,6 @@ Given /^a folder "([^\"]*)" containing the folder "([^\"]*)"$/ do |parent, folde
11
11
  FileUtils.mkdir_p(File.join(parent, folder))
12
12
  end
13
13
 
14
- ## whens
15
-
16
- When /^I run Moviesort with the options "([^\"]*)"$/ do |options|
17
- Moviesort::CommandLine.parse(options).run
18
- end
19
-
20
14
  ## thens
21
15
 
22
16
  Then /^the movie should be moved to "([^\"]*)"$/ do |path|
@@ -1,13 +1,21 @@
1
1
  require 'test/unit/assertions'
2
2
  require 'fakefs/safe'
3
+ require 'mocha'
3
4
 
4
5
  World(Test::Unit::Assertions)
6
+ World(Mocha::API)
5
7
 
6
8
  Before do
7
9
  FakeFS.activate!
10
+ mocha_setup
8
11
  end
9
12
 
10
13
  After do
14
+ begin
15
+ mocha_verify
16
+ ensure
17
+ mocha_teardown
18
+ end
11
19
  FakeFS::FileSystem.clear
12
20
  FakeFS.deactivate!
13
21
  end
@@ -1,14 +1,15 @@
1
1
  require 'trollop'
2
2
  require 'moviesort/sorter'
3
+ require 'moviesort/notifiers/twitter'
3
4
 
4
5
  module Moviesort
5
6
  class CommandLine
7
+ attr_reader :options
8
+
6
9
  def self.parse(options)
7
10
  new(options.split)
8
11
  end
9
12
 
10
- MOVIE_EXTENSIONS = %w{avi mkv mov mp4}
11
-
12
13
  def initialize(argv = ARGV)
13
14
  @options = Trollop.options(argv) do
14
15
  banner <<-EOS.gsub(/^ {8}/, '')
@@ -19,17 +20,24 @@ module Moviesort
19
20
 
20
21
  opt :source, "Source directory", :default => '.'
21
22
  opt :target, "Target directory", :default => '.'
23
+ opt :notifiers, "Notifiers", :type => :string, :multi => true
24
+ opt :twitteruser
25
+ opt :twitterpass
22
26
  end
23
27
  end
24
28
 
25
29
  def run
26
30
  sorter = Sorter::TV.new(@options[:target])
27
- movie_files = Dir[File.join(@options[:source], '*')].select do |file|
28
- MOVIE_EXTENSIONS.include?(File.extname(file)[1..-1])
29
- end
30
- movie_files.each do |file|
31
- sorter.sort_file(file)
31
+ notifiers.each { |note| sorter.add_notifier(note) }
32
+ sorter.sort_directory(@options[:source])
33
+ end
34
+
35
+ def notifiers
36
+ notifiers = []
37
+ if @options[:notifiers].include?('twitter')
38
+ notifiers << Moviesort::Notifiers::Twitter.new(@options[:twitteruser], @options[:twitterpass])
32
39
  end
40
+ return notifiers
33
41
  end
34
42
  end
35
43
  end
@@ -3,8 +3,22 @@ require 'moviesort/tv_show'
3
3
  module Moviesort
4
4
  module Sorter
5
5
  class TV
6
+ VALID_FILE_TYPES = %w{avi mkv mov mp4}
7
+
6
8
  def initialize(output_dir)
7
9
  @output_dir = output_dir
10
+ @notifiers = []
11
+ end
12
+
13
+ def add_notifier(notifier)
14
+ raise "Notifier object must respond to notify" unless notifier.respond_to?(:notify)
15
+ @notifiers << notifier
16
+ end
17
+
18
+ def sort_directory(path)
19
+ movie_files_in_directory(path).each do |file|
20
+ sort_file(file)
21
+ end
8
22
  end
9
23
 
10
24
  def sort_file(path)
@@ -12,6 +26,19 @@ module Moviesort
12
26
  target_path = File.join(@output_dir, tv_show.target_path)
13
27
  FileUtils.mkdir_p(target_path)
14
28
  FileUtils.mv(path, File.join(target_path, tv_show.target_filename))
29
+ notify(tv_show)
30
+ end
31
+
32
+ private
33
+
34
+ def notify(tv_show)
35
+ @notifiers.each { |note| note.notify(tv_show) }
36
+ end
37
+
38
+ def movie_files_in_directory(path)
39
+ Dir[File.join(path, '*')].select do |file|
40
+ VALID_FILE_TYPES.include?(File.extname(file)[1..-1])
41
+ end
15
42
  end
16
43
  end
17
44
  end
@@ -13,7 +13,9 @@ module Moviesort
13
13
 
14
14
  def self.parse(filename)
15
15
  parser = Parser.new(filename)
16
- new(filename, *parser.parse)
16
+ if components = parser.parse
17
+ new(filename, *components)
18
+ end
17
19
  end
18
20
 
19
21
  def target_path
@@ -38,7 +40,7 @@ module Moviesort
38
40
  /(.*)\s(\d{1})(\d{2})/
39
41
  ]
40
42
  match = patterns.map { |regex| @filename.match(regex) }.compact.first
41
- extract_data_from_pattern_match(match)
43
+ extract_data_from_pattern_match(match) if match
42
44
  end
43
45
 
44
46
  def extract_data_from_pattern_match(match)
@@ -50,4 +52,4 @@ module Moviesort
50
52
  end
51
53
  end
52
54
  end
53
- end
55
+ end
data/test/sorter_test.rb CHANGED
@@ -4,11 +4,17 @@ require 'moviesort/sorter'
4
4
  class SorterTest < Test::Unit::TestCase
5
5
 
6
6
  def setup
7
+ FakeFS.activate!
7
8
  @output_path = "/output"
8
9
  @download_path = "/downloads"
9
10
  FileUtils.mkpath(@output_path)
10
11
  FileUtils.mkpath(@download_path)
11
12
  end
13
+
14
+ def teardown
15
+ FakeFS::FileSystem.clear
16
+ FakeFS.deactivate!
17
+ end
12
18
 
13
19
  context "TV show sorter" do
14
20
  setup do
@@ -20,6 +26,21 @@ class SorterTest < Test::Unit::TestCase
20
26
  @sorter.sort_file("/downloads/The Sopranos - S02E04.avi")
21
27
  assert File.exist?("/output/The Sopranos/Season 2/The.Sopranos.S02E04.avi")
22
28
  end
29
+
30
+ should "sort all valid movie files in a given directory" do
31
+ %w{avi mp4 mov mkv}.each { |ext| FileUtils.touch("/downloads/movie.#{ext}") }
32
+ @sorter.expects(:sort_file).with("/downloads/movie.avi")
33
+ @sorter.expects(:sort_file).with("/downloads/movie.mkv")
34
+ @sorter.expects(:sort_file).with("/downloads/movie.mp4")
35
+ @sorter.expects(:sort_file).with("/downloads/movie.mov")
36
+ @sorter.sort_directory(@download_path)
37
+ end
38
+
39
+ should "ignore non-valid movie files in a given directory" do
40
+ FileUtils.touch("/downloads/movie.mp3")
41
+ @sorter.expects(:sort_file).with("/downloads/movie.mp3").never
42
+ @sorter.sort_directory(@download_path)
43
+ end
23
44
  end
24
45
 
25
46
  end
data/test/test_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), *%w[.. lib]))
2
2
  require 'test/unit'
3
3
  require 'shoulda'
4
- require 'fakefs'
4
+ require 'mocha'
5
+ require 'fakefs/safe'
data/test/tv_show_test.rb CHANGED
@@ -45,6 +45,10 @@ class TvShowTest < Test::Unit::TestCase
45
45
  assert_equal 1, @tv_show.season
46
46
  assert_equal 2, @tv_show.episode
47
47
  end
48
+
49
+ should "return nil if format is unrecognized" do
50
+ assert_nil Moviesort::TVShow.parse("dont know what to do with this!.txt")
51
+ end
48
52
  end
49
53
 
50
54
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moviesort
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 5
9
+ version: 0.1.5
5
10
  platform: ruby
6
11
  authors:
7
12
  - Luke Redpath
@@ -9,10 +14,21 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-11-18 00:00:00 +00:00
17
+ date: 2010-03-08 00:00:00 +00:00
13
18
  default_executable:
14
- dependencies: []
15
-
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: trollop
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
16
32
  description:
17
33
  email: luke@lukeredpath.co.uk
18
34
  executables:
@@ -50,18 +66,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
50
66
  requirements:
51
67
  - - ">="
52
68
  - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
53
71
  version: "0"
54
- version:
55
72
  required_rubygems_version: !ruby/object:Gem::Requirement
56
73
  requirements:
57
74
  - - ">="
58
75
  - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
59
78
  version: "0"
60
- version:
61
79
  requirements: []
62
80
 
63
81
  rubyforge_project:
64
- rubygems_version: 1.3.5
82
+ rubygems_version: 1.3.6
65
83
  signing_key:
66
84
  specification_version: 3
67
85
  summary: Small command-line utility for sorting downloaded TV shows and movies