subdb 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/subdb CHANGED
@@ -1,85 +1,46 @@
1
1
  #!/usr/bin/env ruby
2
+ # Copyright (c) 2011 Wilker Lúcio
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
2
21
 
3
22
  require "rubygems"
4
23
 
5
24
  $: << File.expand_path("../../lib", __FILE__)
25
+ $: << File.expand_path("../../vendor/multipart-post", __FILE__)
6
26
 
7
27
  require "subdb"
8
28
 
9
- files = []
10
- video_ext = Subdb::VIDEO_EXTENSIONS.join(",")
11
-
12
- $*.each do |path|
13
- if File.directory?(path)
14
- path = path.chomp("/")
15
- files = files.concat(Dir.glob("#{path}/**/*{#{video_ext}}"))
16
- else
17
- files << path if Subdb::VIDEO_EXTENSIONS.include?(File.extname(path))
18
- end
19
- end
20
-
21
- files = files.sort
22
-
23
- languages = ["pt", "en"]
24
- i = 1
25
-
26
- for path in files
27
- base = File.dirname(path) + "/" + File.basename(path, File.extname(path))
28
- sub = nil
29
-
30
- for subext in Subdb::SUB_EXTESNIONS
31
- subpath = base + subext
32
-
33
- if File.exists?(subpath)
34
- sub = subpath
35
- break
36
- end
37
- end
38
-
39
- puts "Scanning #{path} [#{i}/#{files.length}]"
40
-
41
- begin
42
- subdb = Subdb.new(path)
43
- puts "Hash: #{subdb.hash}"
44
-
45
- remote = subdb.search
46
- puts (remote ? "Found with languages: #{remote}" : "No subtitle found")
47
-
48
- if sub and !remote
49
- puts "Local subtitle found and none on server, uploading..."
50
-
51
- begin
52
- subdb.upload(sub)
53
- puts "Upload completed"
54
- rescue
55
- puts "Error on upload: #{$!}"
56
- end
57
- end
58
-
59
- if !sub and remote
60
- puts "downloading from remote"
61
-
62
- begin
63
- downloaded = subdb.download(languages)
64
-
65
- if downloaded
66
- File.open(base + ".srt", "w") do |f|
67
- f << downloaded
68
- end
69
-
70
- puts "Download done ok"
71
- else
72
- puts "No version for your languages"
73
- end
74
- rescue
75
- puts "Error on download: #{$!}"
76
- end
77
- end
78
- rescue
79
- puts "Can't open: #{$!}"
29
+ files = Subdb::ClientUtils.scan_paths($*)
30
+
31
+ Subdb::ClientUtils.sync files, ["pt", "en"] do |action, arg|
32
+ case action
33
+ when :scan then puts "Scanning #{arg[0]} [#{arg[1] + 1}/#{files.length}]"
34
+ when :scanned then puts "Hash: #{arg.hash}"
35
+ when :remote then puts arg ? "Found with languages: #{arg}" : "No subtitle found"
36
+ when :uploading then puts "Local subtitle found and none on server, uploading..."
37
+ when :upload_ok then puts "Upload completed"
38
+ when :upload_failed then puts "Error on upload: #{arg[1]}"
39
+ when :downloading then puts "Download from remote..."
40
+ when :download_ok then puts "Download done"
41
+ when :download_not_found then puts "No version found for your language"
42
+ when :download_failed then puts "Error on download: #{arg[1]}"
43
+ when :scan_failed then puts "Error opening: #{arg}"
44
+ when :file_done then puts
80
45
  end
81
-
82
- puts
83
-
84
- i += 1
85
46
  end
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ require 'java'
4
+ require File.expand_path("../../javalib/filedrop.jar", __FILE__)
5
+
6
+ import java.awt.BorderLayout
7
+ import java.awt.Color
8
+ import javax.swing.BorderFactory
9
+ import javax.swing.JLabel
10
+ import javax.swing.JFrame
11
+ import javax.swing.JPanel
12
+ import javax.swing.JProgressBar
13
+ import javax.swing.JScrollPane
14
+ import javax.swing.JTextArea
15
+
16
+ include_class java.lang.System
17
+ include_class Java::FileDrop
18
+
19
+ class Example < JFrame
20
+ include FileDrop::Listener
21
+
22
+ def initialize
23
+ super "Simple"
24
+
25
+ @uploading = false
26
+
27
+ self.init_ui
28
+ end
29
+
30
+ def init_ui
31
+ @dropper = JPanel.new
32
+ @dropper.set_border BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10), BorderFactory.createLineBorder(Color.black))
33
+
34
+ @progress = JProgressBar.new(0, 100)
35
+
36
+ hint = JLabel.new("Arraste suas pastas ou arquivos aqui.")
37
+
38
+ @dropper.add(hint, BorderLayout::CENTER)
39
+
40
+ @log = JTextArea.new
41
+
42
+ outer_panel = JPanel.new
43
+
44
+ content_pane.add(@dropper, BorderLayout::CENTER)
45
+ content_pane.add(@progress, BorderLayout::SOUTH)
46
+
47
+ # @progress.set_indeterminate true
48
+
49
+ FileDrop.new(nil, @dropper, self)
50
+
51
+ set_size 500, 300
52
+ set_resizable false
53
+ set_default_close_operation JFrame::EXIT_ON_CLOSE
54
+ set_location_relative_to nil
55
+ set_visible true
56
+
57
+ @progress.set_string_painted true
58
+ @progress.set_string "enviando"
59
+
60
+ Thread.new do
61
+ i = 0
62
+
63
+ while true
64
+ @progress.set_value i
65
+ i += 1
66
+ sleep(5)
67
+ end
68
+ end
69
+ end
70
+
71
+ def filesDropped(files)
72
+ return if @uploading
73
+
74
+ @uploading = true
75
+
76
+ for file in files
77
+ puts file.to_s
78
+ end
79
+ end
80
+ end
81
+
82
+ Example.new
@@ -26,11 +26,9 @@ require 'digest/md5'
26
26
  require 'net/http/post/multipart'
27
27
 
28
28
  require 'subdb/version'
29
+ require 'subdb/client_utils'
29
30
 
30
31
  class Subdb
31
- VIDEO_EXTENSIONS = ['.avi', '.mkv', '.mp4', '.mov', '.mpg', '.wmv', '.rm', '.rmvb', '.divx']
32
- SUB_EXTESNIONS = ['.srt', '.sub']
33
-
34
32
  API = "http://api.thesubdb.com/"
35
33
  SANDBOX = "http://sandbox.thesubdb.com/"
36
34
 
@@ -0,0 +1,111 @@
1
+ # Copyright (c) 2011 Wilker Lúcio
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all 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,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Subdb::ClientUtils
22
+ VIDEO_EXTENSIONS = ['.avi', '.mkv', '.mp4', '.mov', '.mpg', '.wmv', '.rm', '.rmvb', '.divx']
23
+ SUB_EXTESNIONS = ['.srt', '.sub']
24
+
25
+ class << self
26
+ def scan_paths(paths)
27
+ video_ext = VIDEO_EXTENSIONS.join(",")
28
+
29
+ paths.inject([]) do |files, path|
30
+ if File.directory?(path)
31
+ path = path.chomp("/")
32
+ files = files.concat(Dir.glob("#{path}/**/*{#{video_ext}}"))
33
+ else
34
+ files << path if VIDEO_EXTENSIONS.include?(File.extname(path))
35
+ end
36
+
37
+ files
38
+ end.sort
39
+ end
40
+
41
+ def sync(paths, languages = ["en"])
42
+ i = 0
43
+
44
+ for path in paths
45
+ base = File.dirname(path) + "/" + File.basename(path, File.extname(path))
46
+ sub = find_subtitle(path)
47
+
48
+ yield :scan, [path, i]
49
+
50
+ begin
51
+ subdb = Subdb.new(path)
52
+
53
+ yield :scanned, subdb
54
+
55
+ remote = subdb.search
56
+
57
+ yield :remote, remote
58
+
59
+ if sub and !remote
60
+ yield :uploading, subdb
61
+
62
+ begin
63
+ subdb.upload(sub)
64
+ yield :upload_ok, subdb
65
+ rescue
66
+ yield :upload_failed, [subdb, $!]
67
+ end
68
+ end
69
+
70
+ if !sub and remote
71
+ yield :downloading, subdb
72
+
73
+ begin
74
+ downloaded = subdb.download(languages)
75
+
76
+ if downloaded
77
+ File.open(base + ".srt", "w") do |f|
78
+ f << downloaded
79
+ end
80
+
81
+ yield :download_ok, subdb
82
+ else
83
+ yield :download_not_found, subdb
84
+ end
85
+ rescue
86
+ yield :download_failed, [subdb, $!]
87
+ end
88
+ end
89
+ rescue
90
+ yield :scan_failed, $!
91
+ end
92
+
93
+ i += 1
94
+
95
+ yield :file_done, [subdb, i]
96
+ end
97
+ end
98
+
99
+ def find_subtitle(path)
100
+ base = File.dirname(path) + "/" + File.basename(path, File.extname(path))
101
+
102
+ for subext in SUB_EXTESNIONS
103
+ subpath = base + subext
104
+
105
+ return subpath if File.exists?(subpath)
106
+ end
107
+
108
+ nil
109
+ end
110
+ end
111
+ end
@@ -20,5 +20,5 @@
20
20
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
22
  class Subdb
23
- VERSION = "0.1.1"
23
+ VERSION = "0.1.2"
24
24
  end
@@ -0,0 +1,93 @@
1
+ #--
2
+ # (c) Copyright 2007-2011 Nick Sieger.
3
+ # See the file README.txt included with the distribution for
4
+ # software license details.
5
+ #++
6
+
7
+ # Concatenate together multiple IO objects into a single, composite IO object
8
+ # for purposes of reading as a single stream.
9
+ #
10
+ # Usage:
11
+ #
12
+ # crio = CompositeReadIO.new(StringIO.new('one'), StringIO.new('two'), StringIO.new('three'))
13
+ # puts crio.read # => "onetwothree"
14
+ #
15
+ class CompositeReadIO
16
+ # Create a new composite-read IO from the arguments, all of which should
17
+ # respond to #read in a manner consistent with IO.
18
+ def initialize(*ios)
19
+ @ios = ios.flatten
20
+ end
21
+
22
+ # Read from the IO object, overlapping across underlying streams as necessary.
23
+ def read(amount = nil, buf = nil)
24
+ buffer = buf || ''
25
+ done = if amount; nil; else ''; end
26
+ partial_amount = amount
27
+
28
+ loop do
29
+ result = done
30
+
31
+ while !@ios.empty? && (result = @ios.first.read(partial_amount)) == done
32
+ @ios.shift
33
+ end
34
+
35
+ result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
36
+ buffer << result if result
37
+ partial_amount -= result.length if partial_amount && result != done
38
+
39
+ break if partial_amount && partial_amount <= 0
40
+ break if result == done
41
+ end
42
+
43
+ if buffer.length > 0
44
+ buffer
45
+ else
46
+ done
47
+ end
48
+ end
49
+ end
50
+
51
+ # Convenience methods for dealing with files and IO that are to be uploaded.
52
+ class UploadIO
53
+ # Create an upload IO suitable for including in the params hash of a
54
+ # Net::HTTP::Post::Multipart.
55
+ #
56
+ # Can take two forms. The first accepts a filename and content type, and
57
+ # opens the file for reading (to be closed by finalizer). The second accepts
58
+ # an already-open IO, but also requires a third argument, the filename from
59
+ # which it was opened.
60
+ #
61
+ # UploadIO.new("file.txt", "text/plain")
62
+ # UploadIO.new(file_io, "text/plain", "file.txt")
63
+ attr_reader :content_type, :original_filename, :local_path, :io
64
+
65
+ def initialize(filename_or_io, content_type, filename = nil)
66
+ io = filename_or_io
67
+ local_path = ""
68
+ if io.respond_to? :read
69
+ local_path = filename_or_io.path
70
+ else
71
+ io = File.open(filename_or_io)
72
+ local_path = filename_or_io
73
+ end
74
+ filename ||= local_path
75
+
76
+ @content_type = content_type
77
+ @original_filename = File.basename(filename)
78
+ @local_path = local_path
79
+ @io = io
80
+ end
81
+
82
+ def self.convert!(io, content_type, original_filename, local_path)
83
+ raise ArgumentError, "convert! has been removed. You must now wrap IOs using:\nUploadIO.new(filename_or_io, content_type, filename=nil)\nPlease update your code."
84
+ end
85
+
86
+ def method_missing(*args)
87
+ @io.send(*args)
88
+ end
89
+
90
+ def respond_to?(meth)
91
+ @io.respond_to?(meth) || super(meth)
92
+ end
93
+ end
@@ -0,0 +1,3 @@
1
+ module MultipartPost
2
+ VERSION = "1.1.0"
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'parts'
2
+ module Multipartable
3
+ DEFAULT_BOUNDARY = "-----------RubyMultipartPost"
4
+ def initialize(path, params, headers={}, boundary = DEFAULT_BOUNDARY)
5
+ super(path, headers)
6
+ parts = params.map {|k,v| Parts::Part.new(boundary, k, v)}
7
+ parts << Parts::EpiloguePart.new(boundary)
8
+ ios = parts.map{|p| p.to_io }
9
+ self.set_content_type("multipart/form-data", { "boundary" => boundary })
10
+ self.content_length = parts.inject(0) {|sum,i| sum + i.length }
11
+ self.body_stream = CompositeReadIO.new(*ios)
12
+ end
13
+ end
@@ -0,0 +1,27 @@
1
+ #--
2
+ # (c) Copyright 2007-2008 Nick Sieger.
3
+ # See the file README.txt included with the distribution for
4
+ # software license details.
5
+ #++
6
+
7
+ require 'net/http'
8
+ require 'stringio'
9
+ require 'cgi'
10
+ require 'composite_io'
11
+ require 'multipartable'
12
+ require 'parts'
13
+
14
+ module Net #:nodoc:
15
+ class HTTP #:nodoc:
16
+ class Put
17
+ class Multipart < Put
18
+ include Multipartable
19
+ end
20
+ end
21
+ class Post #:nodoc:
22
+ class Multipart < Post
23
+ include Multipartable
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,66 @@
1
+ module Parts
2
+ module Part #:nodoc:
3
+ def self.new(boundary, name, value)
4
+ if value.respond_to? :content_type
5
+ FilePart.new(boundary, name, value)
6
+ else
7
+ ParamPart.new(boundary, name, value)
8
+ end
9
+ end
10
+
11
+ def length
12
+ @part.length
13
+ end
14
+
15
+ def to_io
16
+ @io
17
+ end
18
+ end
19
+
20
+ class ParamPart
21
+ include Part
22
+ def initialize(boundary, name, value)
23
+ @part = build_part(boundary, name, value)
24
+ @io = StringIO.new(@part)
25
+ end
26
+
27
+ def build_part(boundary, name, value)
28
+ part = ''
29
+ part << "--#{boundary}\r\n"
30
+ part << "Content-Disposition: form-data; name=\"#{name.to_s}\"\r\n"
31
+ part << "\r\n"
32
+ part << "#{value}\r\n"
33
+ end
34
+ end
35
+
36
+ # Represents a part to be filled from file IO.
37
+ class FilePart
38
+ include Part
39
+ attr_reader :length
40
+ def initialize(boundary, name, io)
41
+ file_length = io.respond_to?(:length) ? io.length : File.size(io.local_path)
42
+ @head = build_head(boundary, name, io.original_filename, io.content_type, file_length)
43
+ @length = @head.length + file_length
44
+ @io = CompositeReadIO.new(StringIO.new(@head), io, StringIO.new("\r\n"))
45
+ end
46
+
47
+ def build_head(boundary, name, filename, type, content_len)
48
+ part = ''
49
+ part << "--#{boundary}\r\n"
50
+ part << "Content-Disposition: form-data; name=\"#{name.to_s}\"; filename=\"#{filename}\"\r\n"
51
+ part << "Content-Length: #{content_len}\r\n"
52
+ part << "Content-Type: #{type}\r\n"
53
+ part << "Content-Transfer-Encoding: binary\r\n"
54
+ part << "\r\n"
55
+ end
56
+ end
57
+
58
+ # Represents the epilogue or closing boundary.
59
+ class EpiloguePart
60
+ include Part
61
+ def initialize(boundary)
62
+ @part = "--#{boundary}--\r\n"
63
+ @io = StringIO.new(@part)
64
+ end
65
+ end
66
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subdb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Wilker Lucio
@@ -15,25 +15,10 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-19 00:00:00 -03:00
18
+ date: 2011-04-20 00:00:00 -03:00
19
19
  default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- name: multipart-post
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 19
30
- segments:
31
- - 1
32
- - 1
33
- - 0
34
- version: 1.1.0
35
- type: :runtime
36
- version_requirements: *id001
20
+ dependencies: []
21
+
37
22
  description: API for SubDB
38
23
  email:
39
24
  - wilkerlucio@gmail.com
@@ -45,8 +30,15 @@ extra_rdoc_files: []
45
30
 
46
31
  files:
47
32
  - bin/subdb
33
+ - bin/subdb-gui
34
+ - lib/subdb/client_utils.rb
48
35
  - lib/subdb/version.rb
49
36
  - lib/subdb.rb
37
+ - vendor/multipart-post/composite_io.rb
38
+ - vendor/multipart-post/multipart_post.rb
39
+ - vendor/multipart-post/multipartable.rb
40
+ - vendor/multipart-post/net/http/post/multipart.rb
41
+ - vendor/multipart-post/parts.rb
50
42
  - LICENSE
51
43
  - README.textile
52
44
  has_rdoc: true
@@ -58,6 +50,7 @@ rdoc_options: []
58
50
 
59
51
  require_paths:
60
52
  - - lib
53
+ - vendor
61
54
  required_ruby_version: !ruby/object:Gem::Requirement
62
55
  none: false
63
56
  requirements: