Created
January 28, 2014 11:41
-
-
Save ys/8666242 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'transmission_api' | |
require 'pp' | |
class TorrentCli | |
def initialize(conf_file = "#{ENV['HOME']}/.torrent.conf") | |
@conf_file = conf_file | |
unless File.exist?(conf_file) | |
$stderr.puts "Config file does not exists. Default is : ~/.torrent.conf" | |
exit 1 | |
end | |
end | |
def run(args) | |
check_args(args) | |
command = args.shift | |
params = args | |
send(command, params) | |
end | |
def add(params = []) | |
client.create params.shift | |
end | |
def check_args(args) | |
unless args.count == 2 | |
$stderr.puts "Usage: torrent add <torrent_url>" | |
exit 1 | |
end | |
end | |
def check_config(config) | |
unless config[:username] && config[:password] && config[:url] | |
$stderr.puts "Config file should contains: username, password and url in json format" | |
exit 1 | |
end | |
end | |
def client | |
@client ||= TransmissionApi::Client.new(configuration) | |
end | |
def configuration | |
@conf ||= JSON.parse(File.read(@conf_file)).inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}.tap do |conf| | |
check_config(conf) | |
end | |
end | |
end | |
TorrentCli.new.run(ARGV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment