bt-tools 0.3.0

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/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ script: rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bt-tools.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Andrew Page
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # bt-tools
2
+
3
+ [![Build Status](https://travis-ci.org/andrewpage/tools.cd.svg?branch=master)](https://travis-ci.org/andrewpage/tools.cd) [![Code Climate](https://codeclimate.com/github/andrewpage/tools.cd/badges/gpa.svg)](https://codeclimate.com/github/andrewpage/tools.cd) [![Test Coverage](https://codeclimate.com/github/andrewpage/tools.cd/badges/coverage.svg)](https://codeclimate.com/github/andrewpage/tools.cd/coverage)
4
+
5
+ Collection of tools for managing a seedbox.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'bt-tools'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bt-tools
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bt-tools.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/bt-tools ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'tools'
4
+
5
+ # Pass command line options to Tools::CLI for parsing
6
+ cli = Tools::CLI.new(ARGV)
7
+
8
+ # Run the program
9
+ cli.execute
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'tools'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ # require 'irb'
14
+ # IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/bt-tools.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'tools/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'bt-tools'
8
+ spec.version = Tools::VERSION
9
+ spec.authors = ['Andrew Page']
10
+ spec.email = %w(andrew@andrewpage.me)
11
+
12
+ spec.summary = 'Collection of tools for automating BitTorrent.'
13
+ spec.description = 'Collection of tools for provisioning, managing, and automating BitTorrent seedboxes.'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.executables = %w(bt-tools)
18
+ spec.require_paths = %w(lib)
19
+
20
+ spec.add_dependency 'activesupport', '~> 4.2.0'
21
+
22
+ # General Development Dependencies
23
+ spec.add_development_dependency 'bundler'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'pry'
26
+
27
+ # Test Dependencies
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'aruba'
30
+ spec.add_development_dependency 'codeclimate-test-reporter'
31
+ end
@@ -0,0 +1,19 @@
1
+ {
2
+ "create": {
3
+ "manifest": "create.json",
4
+ "source": "rtorrent/data",
5
+ "output": "rtorrent/torrents",
6
+ "announceURL": "http://tracker.what.cd:34000/TEST/announce",
7
+ "private": true,
8
+ "pieceSize": 18
9
+ },
10
+ "transcode": {
11
+ "formats": {
12
+ "320": "transcode-320.json",
13
+ "V0": "transcode-V0.json",
14
+ "V2": "transcode-V2.json"
15
+ },
16
+ "source": "rtorrent/data",
17
+ "output": "rtorrent/data"
18
+ }
19
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "files": []
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "files": []
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "files": []
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "files": []
3
+ }
@@ -0,0 +1,103 @@
1
+ require 'json'
2
+ require 'fileutils'
3
+
4
+ module Tools
5
+ module Actions
6
+ class Create
7
+ include Support::Execution
8
+ include Support::Manifest
9
+
10
+ attr_reader :files, :announce, :source, :output, :piece_size, :privacy
11
+
12
+ # Create creates .torrent files for all files found in its manifest.
13
+ # @param manifest [String] Path to JSON configuration file that lists all files to be proceesed.
14
+ # @param announce [String] Announce URL of the primary tracker for this torrent.
15
+ # @param source [String] Path to the directory where source files will be located.
16
+ # @param output [String] Path to the directory where completed .torrent files will be saved.
17
+ # @param piece_size [Fixnum] BitTorrent piece size, as a power of 2. e.g. 18 = piece size of 256kb because 2**18 is 256kb
18
+ # @param privacy [Boolean] Should this torrent be private?
19
+ def initialize(manifest:, announce:, source:, output:, piece_size:, privacy:)
20
+ @announce = announce
21
+ @source = source
22
+ @output = output
23
+ @piece_size = piece_size
24
+ @privacy = privacy
25
+
26
+ @files = extract_files(manifest)
27
+ end
28
+
29
+ # Create a .torrent file for every file in the manifest
30
+ def execute
31
+ create_output_directory
32
+
33
+ files.each do |file_name|
34
+ # Full relative path to the source file
35
+ infile_path = source_path(file_name)
36
+ # File name of output file
37
+ outfile_name = torrent_name(file_name)
38
+ # Full relative path to output file
39
+ outfile_path = output_path(outfile_name)
40
+
41
+ if can_create_torrent?(infile_path)
42
+ make_torrent(infile_path, outfile_path)
43
+
44
+ puts "√ #{outfile_name}"
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ # Creates .torrent output directory if it does not yet exist.
52
+ def create_output_directory
53
+ FileUtils.mkdir_p(output) unless File.exists?(output)
54
+ end
55
+
56
+ # Generates a path to the source file within the source directory
57
+ # @param file_name [String] Name of the source file, relative to the source directory.
58
+ # @return [String] Path to the source file, relative to the calling location.
59
+ def source_path(file_name)
60
+ File.join(source, file_name)
61
+ end
62
+
63
+ # Generates a path to the output file within the output directory
64
+ # @param file_name [String] Name of the output file, relative to the output directory.
65
+ # @return [String] Path to the output file, relative to the calling location.
66
+ def output_path(file_name)
67
+ File.join(output, file_name)
68
+ end
69
+
70
+ # Generates the name for the finished .torrent file
71
+ # @param file_name [String] Name of the source file
72
+ # @return [String] .torrent name
73
+ def torrent_name(file_name)
74
+ basename = File.basename(file_name)
75
+
76
+ [basename, 'torrent'].join('.')
77
+ end
78
+
79
+ # @param infile_path [String] Path to the source file.
80
+ # @return [Boolean] Can we create the torrent for this source file?
81
+ def can_create_torrent?(infile_path)
82
+ File.exist?(infile_path)
83
+ end
84
+
85
+ # Create the .torrent file
86
+ # @param name [String] Name of the completed .torrent file.
87
+ # @param source [String] Path to the source directory for this torrent.
88
+ # @return [String] Result of the command execution.
89
+ def make_torrent(infile_path, outfile_path)
90
+ cmd = Array.new
91
+ cmd << %(mktorrent)
92
+ cmd << %(-l #{piece_size}) # piece_size is an exponent of two
93
+ cmd << %(-p) if privacy
94
+ cmd << %(-a #{announce})
95
+ cmd << %("#{infile_path}")
96
+ cmd << %(-o "#{outfile_path}")
97
+ cmd = cmd.join(' ')
98
+
99
+ execute_command(cmd)
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,65 @@
1
+ require 'fileutils'
2
+
3
+ module Tools
4
+ module Actions
5
+ class Transcode
6
+ include Support::Execution
7
+
8
+ def initialize(directory, formats = %w(V0 V2 320))
9
+ @directory = directory
10
+ @formats = formats
11
+ end
12
+
13
+ def execute
14
+ directory = @directory
15
+
16
+ @formats.each do |bit|
17
+ new_directory = copy_directory(directory, bit)
18
+
19
+ Dir.chdir(new_directory) do
20
+ Dir['*.flac'].each do |file|
21
+ transcode(file, bit)
22
+ FileUtils.rm(file)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def copy_directory(directory, bit)
31
+ new_directory = directory.gsub(%r{(flac|FLAC)}, bit)
32
+
33
+ FileUtils.cp_r(directory, new_directory)
34
+
35
+ new_directory
36
+ end
37
+
38
+ def transcode(file, bitrate)
39
+ preset = vbr?(bitrate) ? 'V' : 'b'
40
+
41
+ cmd = Array.new
42
+ cmd << %(flac)
43
+ cmd << %(-cd)
44
+ cmd << %("#{file}")
45
+ cmd << %(|)
46
+ cmd << %(lame)
47
+ cmd << %(-#{preset})
48
+ cmd << cmd_bitrate(bitrate)
49
+ cmd << %(-)
50
+ cmd << %("#{File.basename(file).split('.')[0]}.mp3")
51
+ cmd = cmd.join(' ')
52
+
53
+ execute_command(cmd)
54
+ end
55
+
56
+ def vbr?(bitrate)
57
+ bitrate.start_with?('V')
58
+ end
59
+
60
+ def cmd_bitrate(bitrate)
61
+ vbr?(bitrate) ? bitrate[1..-1] : bitrate
62
+ end
63
+ end
64
+ end
65
+ end
data/lib/tools/api.rb ADDED
@@ -0,0 +1,102 @@
1
+ require 'json'
2
+
3
+ module Tools
4
+ module API
5
+ CREATE_REQUIRED_KEYS = %i(announceURL source output pieceSize)
6
+ TRANSCODE_REQUIRED_KEYS = %i(formats source output)
7
+
8
+ # Create .torrent files for each entry in the config manifest
9
+ # @param config_path [String] Path to the primary configuration file.
10
+ def create(config_path)
11
+ # Get a Hash from the config JSON
12
+ config = read_config(config_path, :create)
13
+
14
+ # Get path for the manifest (secondary) file
15
+ manifest = relative_config_path(config_path, config['manifest'])
16
+
17
+ # Initialize .torrent Creator
18
+ creator = Actions::Create.new(
19
+ manifest: manifest,
20
+ announce: config['announceURL'],
21
+ source: config['source'],
22
+ output: config['output'],
23
+ piece_size: config['pieceSize'],
24
+ privacy: config['private']
25
+ )
26
+
27
+ # Create torrents
28
+ creator.execute
29
+ end
30
+
31
+ # Transcode all FLAC files to the desired formats
32
+ # @param config_path [String] Path to the primary configuration file.
33
+ def transcode(config_path)
34
+ config = read_config(config_path, :transcode)
35
+
36
+ # Format keys and their manifest files
37
+ format_configuration = config['formats']
38
+
39
+ # Relativize all paths
40
+ relative_configuration = format_configuration.map do |key, manifest_path|
41
+ [key, relative_config_path(config_path, manifest_path)]
42
+ end.to_h
43
+
44
+ # Format objects
45
+ formats = Tools::Formats::Format.create_from_configuration(relative_configuration)
46
+
47
+ # Initialize file transcoder
48
+ transcoder = Actions::Transcode.new(
49
+ manifests: formats,
50
+ source: config['source'],
51
+ output: config['source']
52
+ )
53
+
54
+ # Transcode all formats
55
+ transcoder.execute
56
+ end
57
+
58
+ private
59
+
60
+ # Validates that all required keys are present in the configuration
61
+ # @param config [Hash] Configuration hash to validate.
62
+ # @param required_keys [Array] Keys that must be present in the configuration.
63
+ def validate_configuration(config, required_keys)
64
+ required = required_keys.map(&:to_s)
65
+ existing = config.keys.map(&:to_s)
66
+
67
+ # The difference between these two arrays is the list of keys that are missing
68
+ missing = required - existing
69
+
70
+ raise Tools::MissingKeysException.new(missing) unless missing.empty?
71
+ end
72
+
73
+ # All required keys for the given action.
74
+ # @param action [Symbol] The action to retreive required keys for.
75
+ # @return [Array] Keys that are required to be present in the configuration for a given action.
76
+ def required_keys(action)
77
+
78
+ end
79
+
80
+ # Read configuration and parse JSON
81
+ # @param path [String] Path of the configuration file to parse.
82
+ # @param action [String] Select inner hash corresponding to this key.
83
+ # @return [Hash] Hash representing the JSON contained within the specified file.
84
+ def read_config(path, action = nil)
85
+ contents = File.read(path)
86
+ json = JSON.parse(contents)
87
+
88
+ action ? json[action.to_s] : json
89
+ end
90
+
91
+ # Generate a path for the secondary config file that is relative
92
+ # to the primary config file
93
+ # @param config [String] Path to the secondary configuration file.
94
+ # @param main [String] Path to the primary configuration file.
95
+ # @return [String] Path to the secondary configuration file, relative to the directory of the primary configuration file.
96
+ def relative_config_path(primary, secondary)
97
+ config_directory = File.dirname(primary)
98
+
99
+ File.join(config_directory, secondary)
100
+ end
101
+ end
102
+ end
data/lib/tools/cli.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'ostruct'
2
+ require 'optionparser'
3
+
4
+ module Tools
5
+ class CLI
6
+ AVAILABLE_COMMANDS = %i(create transcode)
7
+
8
+ attr_reader :options
9
+
10
+ # CLI will parse all command line options
11
+ # @param args [Array] Arguments, typically from ARGV.
12
+ def initialize(args)
13
+ @args = args
14
+ @options = OpenStruct.new
15
+
16
+ parse
17
+ validate
18
+ end
19
+
20
+ # Perform actions based on command & options
21
+ def execute
22
+ Tools.public_send(command, options.configuration_path)
23
+ end
24
+
25
+ private
26
+
27
+ # Extract command + arguments from ARGV
28
+ def parse
29
+ # Parse all arguments first
30
+ option_parser.parse!(arguments)
31
+
32
+ # Get the first argument, this is our command
33
+ cmd = arguments.pop
34
+ raise OptionParser::MissingArgument, 'command' unless cmd
35
+
36
+ # Set the command if it's present
37
+ options.command = cmd.to_sym
38
+ end
39
+
40
+ # @return [Array] Command line arguments passed into this program.
41
+ def arguments
42
+ @args
43
+ end
44
+
45
+ # @return [Symbol] Command that was specified to this program
46
+ def command
47
+ options.command
48
+ end
49
+
50
+ # Ensure we have valid data
51
+ def validate
52
+ raise Tools::InvalidCommandException.new(command, AVAILABLE_COMMANDS) unless valid_command?
53
+ raise Tools::MissingConfigurationException.new('You must specify a valid configuration file.') unless options.configuration_path
54
+ end
55
+
56
+ # @return [Boolean] Is this command supported?
57
+ def valid_command?
58
+ AVAILABLE_COMMANDS.include?(command)
59
+ end
60
+
61
+ # CLI option parsing singleton
62
+ # @return [OptionParser] Object that can parse command line objects according to our specifications.
63
+ def option_parser
64
+ @parser ||= OptionParser.new do |opts|
65
+ opts.banner = "Usage: ./tools [#{AVAILABLE_COMMANDS.join('|')}] <options>"
66
+
67
+ # Allow user to choose a different configuration file
68
+ opts.on('-c', '--config [path]', 'Change configuration file from default.') do |path|
69
+ options.configuration_path = path
70
+ end
71
+
72
+ # View program help
73
+ opts.on_tail('-h', '--help', 'See program help.') do
74
+ puts opts
75
+ exit
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,15 @@
1
+ module Tools
2
+ module Formats
3
+ class F320 < Format
4
+ # Transcode to 320kbps CBR
5
+ def transcode_command
6
+ 'Top Kek 320'
7
+ end
8
+
9
+ # Locate configuration under key 320
10
+ def key
11
+ '320'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Tools
2
+ module Formats
3
+ class FV0 < Format
4
+ # Transcode to 320kbps CBR
5
+ def transcode_command
6
+ 'Top Kek V0'
7
+ end
8
+
9
+ # Locate configuration under key 320
10
+ def key
11
+ 'V0'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Tools
2
+ module Formats
3
+ class FV2 < Format
4
+ # Transcode to 320kbps CBR
5
+ def transcode_command
6
+ 'Top Kek V2'
7
+ end
8
+
9
+ # Locate configuration under key 320
10
+ def key
11
+ 'V2'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,71 @@
1
+ require 'active_support/core_ext/string'
2
+
3
+ module Tools
4
+ module Formats
5
+ class Format
6
+ include Support::Manifest
7
+
8
+ attr_reader :files
9
+
10
+ # Generate Format objects for all objects in the `formats` hash
11
+ # @param formats [Hash] K: format name, V: path to manifest file
12
+ # @return [Array<Format>] Returns an array of Format objects
13
+ def self.create_from_configuration(formats)
14
+ initialized_formats = []
15
+
16
+ formats.each do |format_str, manifest_path|
17
+ # Format prefixed with an F
18
+ format_name = format_class_name(format_str)
19
+
20
+ # Constantized name, namespaced to the current namespace
21
+ format_class = namespaced_class(format_name)
22
+
23
+ # Initialize a new Format object with the corresponding manifest
24
+ format_object = format_class.new(manifest_path)
25
+
26
+ # Add it to the array to be returned
27
+ initialized_formats << format_object
28
+ end
29
+
30
+ initialized_formats
31
+ end
32
+
33
+ def transcode_command
34
+ require_subclass
35
+ end
36
+
37
+ def key
38
+ require_subclass
39
+ end
40
+
41
+ private
42
+
43
+ # Generate the proper convention for a Format class name
44
+ # @param format_name [String] Name of the format.
45
+ # @return [String] format_name prefixed with "F"
46
+ def self.format_class_name(format_name)
47
+ "F#{format_name}"
48
+ end
49
+
50
+ # Generate a constantized class name under the same namespace
51
+ # @param klass [String] Name of class to constantize.
52
+ # @return [Class] Constantized new class name.
53
+ def self.namespaced_class(klass)
54
+ class_string = self.to_s
55
+ namespace = class_string.deconstantize
56
+
57
+ [namespace, klass].join('::').constantize
58
+ end
59
+
60
+ # Initialize a Format with a manifest file
61
+ # @param manifest [String] Path to manifest file.
62
+ def initialize(manifest_path)
63
+ @files = extract_files(manifest_path)
64
+ end
65
+
66
+ def require_subclass
67
+ fail 'Must be implemented by subclass.'
68
+ end
69
+ end
70
+ end
71
+ end