Skip to content

gcarreno/laz-qBitTorrent-WebUI

Repository files navigation

laz-qBitTorrent-WebUI

masterBuild Status devBuild Status Supported FreePascal version: FPC-2.6.2 FPC-3.x Supported Lazarus version: Laz-0.9.x Laz-1.x GitHub Release GitHub Downloads

A Lazarus component to access the qBitTorrent Web UI

Implements API version 2.6.0

Documentation on v2.6.0: https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)

Dependencies

Ararat Synapse

This component depends on the Ararat Synapse Network Lib.

You can get it from one of these options:

I'm using the Online Package Manager and I'm testing this with version 40.1 of the Synapse Network lib.

The package itself has a dependency on laz_synapse.

Since I'm building and testing this with GitHub Actions I'm usin the setup-lazarus action to setup the environment with all the necessary packages.

Usage

GET Methods

GetTorrents

Retrieves the torrent list.

uses
  qBitTorrentWebUI;

type
  TForm1 = class(TForm)
    {...}
    Fqb: TqBitTorrentWebUI;
    {...}
  public
    {...}
    procedure GetTorrentsExecute(Sender: TObject);
    {...}
  end;

var
  Form1: TForm1;

implementation

prodecure TForm1.GetTorrentsExecute(Sender: TObject);
begin
  try
    Fqb.Active := True;
    if Fqb.GetTorrents then
    begin
      // Do something after a successfull torrents retrival
    end;
  except
    on E:Exception do
    begin
      // If torrents retrieval fails it will raise an exception
    end;
    Fqb.Active := False;
  end;
end;

GetTorrentProperties(Hash)

Retrieves the properties for a torrent

uses
  qBitTorrentWebUI;

type
  TForm1 = class(TForm)
    {...}
    Fqb: TqBitTorrentWebUI;
    {...}
  public
    {...}
    procedure GetTorrentPropertiesExecute(Sender: TObject);
    {...}
  end;

var
  Form1: TForm1;

implementation

prodecure TForm1.GetTorrentPropertiesExecute(Sender: TObject);
var
  oFilter: TqBTorrentsFilter;
begin
  try
    try
      Fqb.Active := True;
      oFilter := TqBTorrentsFilter.Create;
      try
        oFilter
          .withFilter('all')
          .withSort('priority')
          .withLimit(10);
        if Fqb.GetTorrentsFiltered(oFilter) then
        begin
          if Fqb.Items.Count > 0 then
          begin
            if Fqb.GetTorrentProperties(Fqb[0].Hash) then
            begin
              // Do something after a successfull torrent's properties retrieval
            end;
          end;
        end;
      finally
        oFilter.Free;
      end;
    except
      on E:Exception do
      begin
        // If torrents retrieval fails it will raise an exception
      end;
    end;
  finally
    Fqb.Active := False;
  end;
end;

GetTorrentTrackers(Hash)

Retrieves the trackers for a torrent

uses
  qBitTorrentWebUI;

type
  TForm1 = class(TForm)
    {...}
    Fqb: TqBitTorrentWebUI;
    {...}
  public
    {...}
    procedure GetTorrentTrackersExecute(Sender: TObject);
    {...}
  end;

var
  Form1: TForm1;

implementation

prodecure TForm1.GetTorrentTrackersExecute(Sender: TObject);
var
  oFilter: TqBTorrentsFilter;
begin
  try
    try
      Fqb.Active := True;
      oFilter := TqBTorrentsFilter.Create;
      try
        oFilter
          .withFilter('all')
          .withSort('priority')
          .withLimit(10);
        if Fqb.GetTorrentsFiltered(oFilter) then
        begin
          if Fqb.Items.Count > 0 then
          begin
            if Fqb.GetTorrentTrackers(Fqb[0].Hash) then
            begin
              // Do something after a successfull torrent's trackers retrieval
            end;
          end;
        end;
      finally
        oFilter.Free;
      end;
    except
      on E:Exception do
      begin
        // If torrents retrieval fails it will raise an exception
      end;
    end;
  finally
    Fqb.Active := False;
  end;
end;

GetTorrentWebSeeds(Hash)

Retrieves the web seeds for a torrent

uses
  qBitTorrentWebUI;

type
  TForm1 = class(TForm)
    {...}
    Fqb: TqBitTorrentWebUI;
    {...}
  public
    {...}
    procedure GetTorrentWebSeedsExecute(Sender: TObject);
    {...}
  end;

var
  Form1: TForm1;

implementation

prodecure TForm1.GetTorrentWebSeedsExecute(Sender: TObject);
var
  oFilter: TqBTorrentsFilter;
begin
  try
    try
      Fqb.Active := True;
      oFilter := TqBTorrentsFilter.Create;
      try
        oFilter
          .withFilter('all')
          .withSort('priority')
          .withLimit(10);
        if Fqb.GetTorrentsFiltered(oFilter) then
        begin
          if Fqb.Items.Count > 0 then
          begin
            if Fqb.GetTorrentWebSeeds(Fqb[0].Hash) then
            begin
              // Do something after a successfull torrent's trackers retrieval
            end;
          end;
        end;
      finally
        oFilter.Free;
      end;
    except
      on E:Exception do
      begin
        // If torrents retrieval fails it will raise an exception
      end;
    end;
  finally
    Fqb.Active := False;
  end;
end;

GetTorrentFiles(Hash)

Retrieves the files for a torrent

uses
  qBitTorrentWebUI;

type
  TForm1 = class(TForm)
    {...}
    Fqb: TqBitTorrentWebUI;
    {...}
  public
    {...}
    procedure GetTorrentFilesExecute(Sender: TObject);
    {...}
  end;

var
  Form1: TForm1;

implementation

prodecure TForm1.GetTorrentFilesExecute(Sender: TObject);
var
  oFilter: TqBTorrentsFilter;
begin
  try
    try
      Fqb.Active := True;
      oFilter := TqBTorrentsFilter.Create;
      try
        oFilter
          .withFilter('all')
          .withSort('priority')
          .withLimit(10);
        if Fqb.GetTorrentsFiltered(oFilter) then
        begin
          if Fqb.Items.Count > 0 then
          begin
            if Fqb.GetTorrentFiles(Fqb[0].Hash) then
            begin
              // Do something after a successfull torrent's files retrieval
            end;
          end;
        end;
      finally
        oFilter.Free;
      end;
    except
      on E:Exception do
      begin
        // If torrents retrieval fails it will raise an exception
      end;
    end;
  finally
    Fqb.Active := False;
  end;
end;

Commands

Shutdown

This send a command to shutdown(close) the qBitTorrent client.

It also sets Active to False.

uses
  qBitTorrentWebUI;

type
  TForm1 = class(TForm)
    {...}
    Fqb: TqBitTorrentWebUI;
    {...}
  public
    {...}
    procedure ExecShutdownExecute(Sender: TObject);
    {...}
  end;

var
  Form1: TForm1;

implementation

prodecure TForm1.ExecShutdownExecute(Sender: TObject);
begin
  try
    Fqb.Active := True;
    if Fqb.ExecShutdown then
    begin
      // Do something after a successfull shutdown
    end;
  except
    on E:Exception do
    begin
      // If shutdown fails it will raise an exception
    end;
    Fqb.Active := False;
  end;
end;