suby 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/suby.rb CHANGED
@@ -1,25 +1,26 @@
1
- require_relative 'suby/downloader_error'
2
- require_relative 'suby/not_found_error'
1
+ require 'epath'
2
+ require 'zip/zip'
3
+
3
4
  require_relative 'suby/filename_parser'
4
5
  require_relative 'suby/downloader'
5
6
  require_relative 'suby/interface'
6
7
 
7
- gem 'rubyzip2'
8
- require 'zip'
9
-
10
8
  module Suby
11
- SUB_EXTENSIONS = %w[srt sub].map { |ext| ".#{ext}" }
12
- TEMP_ARCHIVE_NAME = '__archive__'
9
+ NotFoundError = Class.new StandardError
10
+ DownloaderError = Class.new StandardError
11
+
12
+ SUB_EXTENSIONS = %w[srt sub]
13
+ TEMP_ARCHIVE = Path('__archive__')
13
14
 
14
15
  class << self
15
16
  include Interface
16
17
 
17
18
  def download_subtitles(files, options = {})
18
19
  files.each { |file|
19
- next if File.directory?(file) or SUB_EXTENSIONS.include?(File.extname(file))
20
+ file = Path(file)
21
+ next if file.directory? or SUB_EXTENSIONS.include?(file.ext)
20
22
  next puts "Skipping: #{file}" if SUB_EXTENSIONS.any? { |ext|
21
- st = File.basename(file, File.extname(file)) + ext
22
- File.exist? st and File.size(st) > 0
23
+ f = file.sub_ext(ext) and f.exist? and !f.empty?
23
24
  }
24
25
  download_subtitles_for_file(file, options)
25
26
  }
@@ -56,23 +57,22 @@ module Suby
56
57
  end
57
58
  end
58
59
 
59
- def extract_sub_from_archive(archive, format, basename)
60
+ def extract_sub_from_archive(archive, format, file)
60
61
  case format
61
62
  when :zip
62
- Zip::ZipFile.open(archive) { |zip|
63
+ Zip::ZipFile.open(archive.to_s) { |zip|
63
64
  sub = zip.entries.find { |entry|
64
- entry.to_s =~ /#{Regexp.union SUB_EXTENSIONS}$/
65
+ entry.to_s =~ /\.#{Regexp.union SUB_EXTENSIONS}$/
65
66
  }
66
67
  raise "no subtitles in #{archive}" unless sub
67
- name = basename + File.extname(sub.to_s)
68
- sub.extract(name)
68
+ name = file.sub_ext(Path(sub).ext)
69
+ sub.extract(name.to_s)
69
70
  }
70
71
  else
71
72
  raise "unknown archive type (#{archive})"
72
73
  end
73
74
  ensure
74
- # Cleaning
75
- File.unlink archive if File.exist? archive
75
+ archive.unlink if archive.exist?
76
76
  end
77
77
  end
78
78
  end
@@ -5,7 +5,7 @@ require 'nokogiri'
5
5
  module Suby
6
6
  class Downloader
7
7
  DOWNLOADERS = []
8
- def self.add(downloader)
8
+ def self.inherited(downloader)
9
9
  DOWNLOADERS << downloader
10
10
  end
11
11
 
@@ -85,28 +85,24 @@ module Suby
85
85
  format = self.class::FORMAT
86
86
  case format
87
87
  when :file
88
- open(sub_name(contents), 'wb') { |f| f.write contents }
88
+ sub_name(contents).write contents
89
89
  when :zip
90
- open(TEMP_ARCHIVE_NAME, 'wb') { |f| f.write contents }
91
- Suby.extract_sub_from_archive(TEMP_ARCHIVE_NAME, format, basename)
90
+ TEMP_ARCHIVE.write contents
91
+ Suby.extract_sub_from_archive(TEMP_ARCHIVE, format, file)
92
92
  else
93
93
  raise "unknown subtitles format: #{format}"
94
94
  end
95
95
  end
96
96
 
97
- def basename
98
- File.basename(file, File.extname(file))
99
- end
100
-
101
97
  def sub_name(contents)
102
- basename + sub_extension(contents)
98
+ file.sub_ext sub_extension(contents)
103
99
  end
104
100
 
105
101
  def sub_extension(contents)
106
102
  if contents[0..10] =~ /1\r?\n/
107
- '.srt'
103
+ 'srt'
108
104
  else
109
- '.sub'
105
+ 'sub'
110
106
  end
111
107
  end
112
108
  end
@@ -1,7 +1,5 @@
1
1
  module Suby
2
2
  class Downloader::Addic7ed < Downloader
3
- Downloader.add(self)
4
-
5
3
  SITE = 'www.addic7ed.com'
6
4
  FORMAT = :file
7
5
  LANG_IDS = {
@@ -1,7 +1,5 @@
1
1
  module Suby
2
2
  class Downloader::TVSubtitles < Downloader
3
- Downloader.add(self)
4
-
5
3
  SITE = 'www.tvsubtitles.net'
6
4
  FORMAT = :zip
7
5
  SEARCH_URL = '/search.php'
@@ -78,7 +78,7 @@ module Suby
78
78
  ]
79
79
 
80
80
  def parse(file)
81
- filename = File.basename(file)
81
+ filename = file.basename.to_s
82
82
  found = FILENAME_PATTERNS.find { |pattern|
83
83
  pattern =~ filename
84
84
  }
data/suby.gemspec CHANGED
@@ -10,9 +10,10 @@ Gem::Specification.new do |s|
10
10
  s.executables = ['suby']
11
11
 
12
12
  s.required_ruby_version = '>= 1.9.2'
13
+ s.add_dependency 'epath', '>= 0.1.0'
13
14
  s.add_dependency 'nokogiri'
14
- s.add_dependency 'rubyzip2'
15
+ s.add_dependency 'rubyzip'
15
16
  s.add_dependency 'term-ansicolor'
16
17
 
17
- s.version = '0.1.3'
18
+ s.version = '0.1.4'
18
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: suby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-12 00:00:00.000000000 Z
12
+ date: 2012-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: epath
16
+ requirement: &2152477380 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2152477380
14
25
  - !ruby/object:Gem::Dependency
15
26
  name: nokogiri
16
- requirement: &2151909200 !ruby/object:Gem::Requirement
27
+ requirement: &2152476880 !ruby/object:Gem::Requirement
17
28
  none: false
18
29
  requirements:
19
30
  - - ! '>='
@@ -21,10 +32,10 @@ dependencies:
21
32
  version: '0'
22
33
  type: :runtime
23
34
  prerelease: false
24
- version_requirements: *2151909200
35
+ version_requirements: *2152476880
25
36
  - !ruby/object:Gem::Dependency
26
- name: rubyzip2
27
- requirement: &2151908380 !ruby/object:Gem::Requirement
37
+ name: rubyzip
38
+ requirement: &2152476000 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: '0'
33
44
  type: :runtime
34
45
  prerelease: false
35
- version_requirements: *2151908380
46
+ version_requirements: *2152476000
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: term-ansicolor
38
- requirement: &2151907540 !ruby/object:Gem::Requirement
49
+ requirement: &2152475540 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,7 +54,7 @@ dependencies:
43
54
  version: '0'
44
55
  type: :runtime
45
56
  prerelease: false
46
- version_requirements: *2151907540
57
+ version_requirements: *2152475540
47
58
  description: Find and download subtitles
48
59
  email: eregontp@gmail.com
49
60
  executables:
@@ -55,10 +66,8 @@ files:
55
66
  - lib/suby/downloader/addic7ed.rb
56
67
  - lib/suby/downloader/tvsubtitles.rb
57
68
  - lib/suby/downloader.rb
58
- - lib/suby/downloader_error.rb
59
69
  - lib/suby/filename_parser.rb
60
70
  - lib/suby/interface.rb
61
- - lib/suby/not_found_error.rb
62
71
  - lib/suby.rb
63
72
  - .gitignore
64
73
  - README.md
@@ -1,4 +0,0 @@
1
- module Suby
2
- class DownloaderError < StandardError
3
- end
4
- end
@@ -1,4 +0,0 @@
1
- module Suby
2
- class NotFoundError < StandardError
3
- end
4
- end