subdb-cli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a54548ab495d4c67e40297b3edd7d33510f58ba
4
+ data.tar.gz: 3ea6467e2dd3fdc3fdc9bc4add1d1ff06b03c3c3
5
+ SHA512:
6
+ metadata.gz: c8c8fd5a059aa0c1419f336dbb29044396446c091f90f9f2a8d1e97dd025f4051f074c2227dad4d5afa08a536eafc906260b6ad9576c3a52e57924ce7ba8fafb
7
+ data.tar.gz: 8cfde11528a3ae3a60339d221d5d959c81f8359694da2dfa8fca0e9f8d0fe21ef9065124cc94e87ed5ac72bf43818501832533fc9cc4f616fdb10cdbeb62d231
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in subdb-cli.gemspec
4
+ gemspec
@@ -0,0 +1,12 @@
1
+ # subdb-cli
2
+
3
+ Library and client to download subtitles from http://thesubdb.com.
4
+
5
+ ## Usage
6
+
7
+ subdb [file]
8
+ subdb [file] -l [lang]
9
+
10
+ ## Contributing
11
+
12
+ Bug reports and pull requests are welcome on GitHub at https://github.com/carlosparamio/subdb-cli. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "subdb/cli"
5
+
6
+ require "irb"
7
+ IRB.start(__FILE__)
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "subdb/cli"
5
+ require "optparse"
6
+
7
+ ARGV << '-h' if ARGV[0].nil?
8
+
9
+ options = {
10
+ language: "en"
11
+ }
12
+
13
+ OptionParser.new do |opts|
14
+ opts.banner = "Usage: subdb [options] file_path"
15
+
16
+ opts.on("-l", "--language LANG", "Select language to download (default: en)") do |language|
17
+ options[:language] = language
18
+ end
19
+
20
+ opts.on_tail("-h", "--help", "Show this message") do
21
+ puts opts
22
+ exit
23
+ end
24
+ end.parse!
25
+
26
+ begin
27
+ Subdb::Cli.new.download(ARGV[0], options[:language])
28
+ rescue Subdb::Cli::FileNotFound
29
+ puts "File not found"
30
+ exit
31
+ rescue Subdb::Cli::SubtitlesNotFound
32
+ puts "Subtitles file not found at Subdb database for the given language"
33
+ exit
34
+ end
35
+
36
+ puts "Subtitles file downloaded"
@@ -0,0 +1,64 @@
1
+ require 'subdb/cli/version'
2
+ require 'digest'
3
+ require 'faraday'
4
+
5
+ module Subdb
6
+ class Cli
7
+
8
+ BASE_URL = 'http://api.thesubdb.com/'
9
+ READSIZE = 64 * 1024
10
+
11
+ FileNotFound = Class.new(StandardError)
12
+ SubtitlesNotFound = Class.new(StandardError)
13
+
14
+ def download(file_path, language)
15
+ response = client.get('/', {
16
+ action: 'download',
17
+ language: language,
18
+ hash: hash_for(file_path)
19
+ })
20
+
21
+ raise SubtitleNotFound unless response.status == 200
22
+
23
+ File.open(srt_file_for(file_path), "w") do |file|
24
+ file.write(response.body)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def srt_file_for(file_path)
31
+ dir = File.dirname(file_path)
32
+ basename = File.basename(file_path, File.extname(file_path))
33
+ File.join(dir, "#{basename}.srt")
34
+ end
35
+
36
+ def hash_for(file_path)
37
+ file = File.open(file_path)
38
+ buffer = file.read(READSIZE)
39
+ file.seek(-READSIZE, IO::SEEK_END)
40
+ buffer += file.read(READSIZE)
41
+ file.close
42
+ md5_for(buffer)
43
+ rescue Errno::ENOENT
44
+ raise FileNotFound
45
+ end
46
+
47
+ def md5_for(buffer)
48
+ md5 = Digest::MD5.new
49
+ md5.update(buffer)
50
+ md5.hexdigest
51
+ end
52
+
53
+ def client
54
+ @_client ||= Faraday.new(url: BASE_URL).tap do |client|
55
+ client.headers[:user_agent] = user_agent
56
+ end
57
+ end
58
+
59
+ def user_agent
60
+ "SubDB/1.0 (subdb-cli/0.1; https://github.com/carlosparamio/subdb-cli)"
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ module Subdb
2
+ class Cli
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'subdb/cli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "subdb-cli"
8
+ spec.version = Subdb::Cli::VERSION
9
+ spec.authors = ["Carlos Paramio"]
10
+ spec.email = ["hola@carlosparamio.com"]
11
+
12
+ spec.summary = %q{SubDB client in Ruby}
13
+ spec.description = %q{SubDB client in Ruby}
14
+ spec.homepage = "https://github.com/carlosparamio/subdb-cli"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "faraday", "~> 0.13.1"
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subdb-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Carlos Paramio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.13.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.13.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: SubDB client in Ruby
70
+ email:
71
+ - hola@carlosparamio.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - bin/subdb
85
+ - lib/subdb/cli.rb
86
+ - lib/subdb/cli/version.rb
87
+ - subdb-cli.gemspec
88
+ homepage: https://github.com/carlosparamio/subdb-cli
89
+ licenses: []
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.5.1
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: SubDB client in Ruby
111
+ test_files: []