<commit_before>#include <Hadouken/Http/JsonRpc/SessionGetTorrentsMethod.hpp>

#include <Hadouken/BitTorrent/Session.hpp>
#include <Hadouken/BitTorrent/TorrentInfo.hpp>
#include <Hadouken/BitTorrent/TorrentHandle.hpp>
#include <Hadouken/BitTorrent/TorrentStatus.hpp>
#include <Hadouken/BitTorrent/TorrentSubsystem.hpp>
#include <Poco/Util/Application.h>

using namespace Hadouken::BitTorrent;
using namespace Hadouken::Http::JsonRpc;
using namespace Poco::JSON;
using namespace Poco::Util;

Poco::Dynamic::Var::Ptr SessionGetTorrentsMethod::execute(const Array::Ptr& params)
{
    /*
    Return torrents in this session as a dictionary where the torrents info
    hash is key.

    {
      "<hash>": {
        "name": <string>,
        ...
      },

      ...
    }

    The input to this method is either an empty array [] which means that all
    torrents should be returned. If the array is non-empty, the elements of
    that array are hex-encoded info hashes telling which torrents to return.
    */
    
    Application& app = Application::instance();
    Session& sess = app.getSubsystem<TorrentSubsystem>().getSession();

    std::vector<TorrentHandle> handles;

    if (params->size() > 0)
    {
        for (Poco::Dynamic::Var item : *params)
        {
            std::string hash = item.extract<std::string>();
            TorrentHandle foundHandle = sess.findTorrent(hash);

            if (foundHandle.isValid())
            {
                handles.push_back(foundHandle);
            }
        }
    }
    else
    {
        handles = sess.getTorrents();
    }

    Poco::DynamicStruct result;

    for (TorrentHandle handle : handles)
    {
        TorrentInfo info = handle.getTorrentFile();
        TorrentStatus status = handle.getStatus();

        Poco::DynamicStruct data;
        data["name"] = status.getName();
        data["infoHash"] = status.getInfoHash();
        data["progress"] = status.getProgress();
        data["savePath"] = status.getSavePath();
        data["downloadRate"] = status.getDownloadRate();
        data["uploadRate"] = status.getUploadRate();
        data["downloadedBytes"] = status.getTotalDownload();
        data["downloadedBytesTotal"] = status.getAllTimeDownload();
        data["uploadedBytes"] = status.getTotalUpload();
        data["uploadedBytesTotal"] = status.getAllTimeUpload();
        data["numPeers"] = status.getNumPeers();
        data["numSeeds"] = status.getNumSeeds();
        data["totalSize"] = info.getTotalSize();
        data["state"] = (int)status.getState();
        data["isFinished"] = status.isFinished();
        data["isMovingStorage"] = status.isMovingStorage();
        data["isPaused"] = status.isPaused();
        data["isSeeding"] = status.isSeeding();
        data["isSequentialDownload"] = status.isSequentialDownload();

        Poco::Dynamic::Array tags;
        std::vector<std::string> t;
        handle.getTags(t);

        for (std::string tag : t)
        {
            tags.push_back(tag);
        }

        data["tags"] = tags;

        result[handle.getInfoHash()] = data;
    }

    return new Poco::Dynamic::Var(result);
}
<commit_msg>Added queue position to 'session.getTorrents'.<commit_after>#include <Hadouken/Http/JsonRpc/SessionGetTorrentsMethod.hpp>

#include <Hadouken/BitTorrent/Session.hpp>
#include <Hadouken/BitTorrent/TorrentInfo.hpp>
#include <Hadouken/BitTorrent/TorrentHandle.hpp>
#include <Hadouken/BitTorrent/TorrentStatus.hpp>
#include <Hadouken/BitTorrent/TorrentSubsystem.hpp>
#include <Poco/Util/Application.h>

using namespace Hadouken::BitTorrent;
using namespace Hadouken::Http::JsonRpc;
using namespace Poco::JSON;
using namespace Poco::Util;

Poco::Dynamic::Var::Ptr SessionGetTorrentsMethod::execute(const Array::Ptr& params)
{
    /*
    Return torrents in this session as a dictionary where the torrents info
    hash is key.

    {
      "<hash>": {
        "name": <string>,
        ...
      },

      ...
    }

    The input to this method is either an empty array [] which means that all
    torrents should be returned. If the array is non-empty, the elements of
    that array are hex-encoded info hashes telling which torrents to return.
    */
    
    Application& app = Application::instance();
    Session& sess = app.getSubsystem<TorrentSubsystem>().getSession();

    std::vector<TorrentHandle> handles;

    if (params->size() > 0)
    {
        for (Poco::Dynamic::Var item : *params)
        {
            std::string hash = item.extract<std::string>();
            TorrentHandle foundHandle = sess.findTorrent(hash);

            if (foundHandle.isValid())
            {
                handles.push_back(foundHandle);
            }
        }
    }
    else
    {
        handles = sess.getTorrents();
    }

    Poco::DynamicStruct result;

    for (TorrentHandle handle : handles)
    {
        TorrentInfo info = handle.getTorrentFile();
        TorrentStatus status = handle.getStatus();

        Poco::DynamicStruct data;
        data["name"] = status.getName();
        data["infoHash"] = status.getInfoHash();
        data["progress"] = status.getProgress();
        data["savePath"] = status.getSavePath();
        data["downloadRate"] = status.getDownloadRate();
        data["uploadRate"] = status.getUploadRate();
        data["downloadedBytes"] = status.getTotalDownload();
        data["downloadedBytesTotal"] = status.getAllTimeDownload();
        data["uploadedBytes"] = status.getTotalUpload();
        data["uploadedBytesTotal"] = status.getAllTimeUpload();
        data["numPeers"] = status.getNumPeers();
        data["numSeeds"] = status.getNumSeeds();
        data["totalSize"] = info.getTotalSize();
        data["state"] = (int)status.getState();
        data["isFinished"] = status.isFinished();
        data["isMovingStorage"] = status.isMovingStorage();
        data["isPaused"] = status.isPaused();
        data["isSeeding"] = status.isSeeding();
        data["isSequentialDownload"] = status.isSequentialDownload();
        data["queuePosition"] = status.getQueuePosition();

        Poco::Dynamic::Array tags;
        std::vector<std::string> t;
        handle.getTags(t);

        for (std::string tag : t)
        {
            tags.push_back(tag);
        }

        data["tags"] = tags;

        result[handle.getInfoHash()] = data;
    }

    return new Poco::Dynamic::Var(result);
}

{'fraction_non_alphanumeric': 0.13575015693659762, 'fraction_numerical': 0.00031387319522912746, 'mean_word_length': 2.883607556368068, 'pattern_counts': {'":': 4, '<': 29, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4635448', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1861, 'n_words': 368}