12/08/2018, 14:46

Application​ Torrent Cloud With Ruby On Rails

With this article, I want to show you guy about one application that It is called TORRENT CLOUD. It is made by Ruby On Rails. This application append by the torrent knowledge too. By the way, It helps you to download and easy to upload your file download into the cloud storage (Google Drive, One ...

With this article, I want to show you guy about one application that It is called TORRENT CLOUD. It is made by Ruby On Rails. This application append by the torrent knowledge too. By the way, It helps you to download and easy to upload your file download into the cloud storage (Google Drive, One Drive, DropBox).

We need to install console torrent to help our machine to have torrent download. It is called Transmission Command Line.

The BitTorrent protocol can be used to reduce the server and network impact of distributing large files. Rather than downloading a file from a single source server, the BitTorrent protocol allows users to join a "swarm" of hosts to download and upload from each other simultaneously.

Transmission is designed for easy, powerful use. We've set the defaults to Just Work and it only takes a few clicks to configure advanced features like watch directories, bad peer blocklists, and the web interface. When Ubuntu chose Transmission as its default BitTorrent client, one of the most-cited reasons was its easy learning curve.

This How-to focuses on CLI (Command Line Interface) and the Web interface

Transmission has been configured to work out of the box on a desktop. Because this how-to is designed for Ubuntu server we need to manually allow remote access.

Add Transmission PPA Repository

Transmission is available in Ubuntu repository. However if you want the latest then add the PPA repository

$ sudo add-apt-repository ppa:transmissionbt/ppa

Update repositories

$ sudo apt-get update

Install transmission

$ sudo apt-get install transmission-cli transmission-common transmission-daemon

Check your torrent version

$ transmission-daemon --version

Start your torrent application

$ sudo service transmission-daemon start

Configure

There are many settings which can be configured. This how-to focus on tweaking the default configuration file for use with Ubuntu server.

transmission-daemon will start automatically each time you start your server, with the settings defined in /var/lib/transmission-daemon/info/settings.json

Make sure the Transmission daemon is not running when changing the config file otherwise your changes will be over written.

$ sudo service transmission-daemon stop

edit /var/lib/transmission-daemon/info/settings.json

sudo nano /var/lib/transmission-daemon/info/settings.json

Disable authentication

We need to diasble authentication in torrent that is easy for us to connect our application with Rails application.

"rpc-authentication-required": false

With rails application, we will write the code to connect to our application torrent in our system (ubuntu).

Create the Rails App

Type the following at the command prompt:

$ rails new torrent-rails
$ cd torrent-rails
$ bundle

Create the Torrents Controller

Create the torrents controller using the Rails generator. add routes to config/routes.rb and add methods for show, create, list torrents.

$ rails g controller torrents

Then open config/routes.rb and add this code:

Rails.application.routes.draw do
  root "torrents#index"
  resources :torrents, only: [:index, :create]
end

Connect Rails with Torrent system

Because our torrent system can be Transmission Daemon Server, we can connect it by RestFul by itself with host http://localhost and port 9091 NOTE: You can config other host or port by your self by edit file /var/lib/transmission-daemon/info/settings.json. We recommend you to use gem rest-client, Let's start add gem rest-client to GemFile, after that run bundle install.

Create fuctions control the torrent

We start to creat some functions that allow us:

  • Add file torrent download.
  • Start download.
  • Stop download.
  • Get status of file that is downloading. Let's create file name app/logic/torrent.rb, and fill it with these code below:
class Torrent
  PORT = 9091
  HOST = "http://localhost"

  class << self
    def get ids = nil, options = {}
      options[:ids] = ids if ids
      options[:fields] ||= ["id", "name", "percentDone", "downloadDir"]
      torrents = transform_response(request("torrent-get", options))
      torrents["arguments"]["torrents"]
    end

   # Format url:  
   # 1. torrent file
   # 2. magnet (torrent)
    def add url, options = {}
      options[:filename] = url
      torrent = transform_response(request("torrent-add", options))
      torrent = torrent["arguments"]
      torrent["torrent-added"] || torrent["torrent-duplicate"]
    end

    def remove ids, options = {}
      options[:ids] = ids
      options["delete-local-data"] = true unless options["delete-local-data"].present?
      transform_response(request("torrent-remove", options))
    end

    def stop ids, options = {}
      options[:ids] = ids.respond_to?(:length) ? ids : [ids]
      request("torrent-stop", options)
    end

    def start ids, options = {}
      options[:ids] = ids.respond_to?(:length) ? ids : [ids]
      request("torrent-start", options)
    end

    def request method, options
      arguments = { method: method, arguments: options }
      RestClient::Request.execute(
        url: _url,
        method: :post,
        content_type: :json,
        payload: arguments.to_json,
        headers: { "X-Transmission-Session-Id" => @session }
      )
    rescue RestClient::Conflict => ex
      save_session(ex.response)
      retry
    end

    private

    def save_session response
      @session = response.match(/<code>(.*)</code>/)[1].split(":").last.strip
    end

    def transform_response response
      attributes = ActiveSupport::JSON.decode(response)
      ActiveSupport::HashWithIndifferentAccess.new(attributes)
    end

    def _url
      "#{HOST}:#{PORT}/transmission/rpc"
    end
  end
end

Test your code

Now let's start test our code in rails console.

$ Torrent.add "magnet:?xt=urn:btih:e5ae98831675dc58c7ad12e14262f7dc043cdfe8&dn=African.Cats.2011.1080p.BluRay.H264.AAC-RARBG&tr=http%3A%2F%2Ftracker.trackerfix.com%3A80%2Fannounce&tr=udp%3A%2F%2F9.rarbg.me%3A2710&tr=udp%3A%2F%2F9.rarbg.to%3A2710"
$ Torrent.start 1

Note: you can check your torrent status without use rails application code by single console of ubuntu with code transmission-remote -l

Usage

  • Easy to use upload to cloud storage
  • Top speed that you never know, it can increase to 10MB/s
  • Don't waste your time to worry about it
  • Easy with you if you are using RSS daily download and upload for your website movies

Bad things you need to know

  • Still appends on seed or leech of torrent.
  • Run more system 's memory.
  • Need to careful with files that you download into your system because it can be virus or copy right content.
  • Need more bandwith in product.
  • Must be config only upload speed 50kb or 20kb.

Conclusion

With article is just simple rails application that connet to torrent of system but it can use for cloud torrent if you want to upload your file to other cloud storage as Google Drive, DropBox,... Code Project

0