Exemple #1
0
/**
 * Returns the properties for a torrent in JSON format.
 *
 * The return value is a JSON-formatted dictionary.
 * The dictionary keys are:
 *   - "save_path": Torrent save path
 *   - "creation_date": Torrent creation date
 *   - "piece_size": Torrent piece size
 *   - "comment": Torrent comment
 *   - "total_wasted": Total data wasted for torrent
 *   - "total_uploaded": Total data uploaded for torrent
 *   - "total_uploaded_session": Total data uploaded this session
 *   - "total_downloaded": Total data uploaded for torrent
 *   - "total_downloaded_session": Total data downloaded this session
 *   - "up_limit": Torrent upload limit
 *   - "dl_limit": Torrent download limit
 *   - "time_elapsed": Torrent elapsed time
 *   - "seeding_time": Torrent elapsed time while complete
 *   - "nb_connections": Torrent connection count
 *   - "nb_connections_limit": Torrent connection count limit
 *   - "share_ratio": Torrent share ratio
 */
QByteArray btjson::getPropertiesForTorrent(const QString& hash)
{
    CACHED_VARIABLE_FOR_HASH(QVariantMap, data, CACHE_DURATION_MS, hash);
    try {
        QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);

        libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters);
        if (!status.has_metadata)
            return QByteArray();

        // Save path
        QString save_path = fsutils::toNativePath(TorrentPersistentData::instance()->getSavePath(hash));
        if (save_path.isEmpty())
            save_path = fsutils::toNativePath(h.save_path());
        data[KEY_PROP_SAVE_PATH] = save_path;
        data[KEY_PROP_CREATION_DATE] = h.creation_date_unix();
        data[KEY_PROP_PIECE_SIZE] = static_cast<qlonglong>(h.piece_length());
        data[KEY_PROP_COMMENT] = h.comment();
        data[KEY_PROP_WASTED] = static_cast<qlonglong>(status.total_failed_bytes + status.total_redundant_bytes);
        data[KEY_PROP_UPLOADED] = static_cast<qlonglong>(status.all_time_upload);
        data[KEY_PROP_UPLOADED_SESSION] = static_cast<qlonglong>(status.total_payload_upload);
        data[KEY_PROP_DOWNLOADED] = static_cast<qlonglong>(status.all_time_download);
        data[KEY_PROP_DOWNLOADED_SESSION] = static_cast<qlonglong>(status.total_payload_download);
        data[KEY_PROP_UP_LIMIT] = h.upload_limit() <= 0 ? -1 : h.upload_limit();
        data[KEY_PROP_DL_LIMIT] = h.download_limit() <= 0 ? -1 : h.download_limit();
        data[KEY_PROP_TIME_ELAPSED] = status.active_time;
        data[KEY_PROP_SEEDING_TIME] = status.seeding_time;
        data[KEY_PROP_CONNECT_COUNT] = status.num_connections;
        data[KEY_PROP_CONNECT_COUNT_LIMIT] = status.connections_limit;
        const qreal ratio = QBtSession::instance()->getRealRatio(status);
        data[KEY_PROP_RATIO] = ratio > QBtSession::MAX_RATIO ? -1 : ratio;
    }
    catch (const std::exception& e) {
        qWarning() << Q_FUNC_INFO << "Invalid torrent: " << misc::toQStringU(e.what());
        return QByteArray();
    }

    return json::toJson(data);
}
Exemple #2
0
/**
 * Returns the properties for a torrent in JSON format.
 *
 * The return value is a JSON-formatted dictionary.
 * The dictionary keys are:
 *   - "save_path": Torrent save path
 *   - "creation_date": Torrent creation date
 *   - "piece_size": Torrent piece size
 *   - "comment": Torrent comment
 *   - "total_wasted": Total data wasted for torrent
 *   - "total_uploaded": Total data uploaded for torrent
 *   - "total_downloaded": Total data uploaded for torrent
 *   - "up_limit": Torrent upload limit
 *   - "dl_limit": Torrent download limit
 *   - "time_elapsed": Torrent elapsed time
 *   - "nb_connections": Torrent connection count
 *   - "share_ratio": Torrent share ratio
 */
QString btjson::getPropertiesForTorrent(const QString& hash)
{
  CACHED_VARIABLE_FOR_HASH(JsonDict, data, CACHE_DURATION_MS, hash);
  try {
    QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);

    if (!h.has_metadata())
      return QString();

    // Save path
    QString save_path = TorrentPersistentData::getSavePath(hash);
    if (save_path.isEmpty())
      save_path = h.save_path();
    data.add(KEY_PROP_SAVE_PATH, save_path);
    data.add(KEY_PROP_CREATION_DATE, h.creation_date());
    data.add(KEY_PROP_PIECE_SIZE, misc::friendlyUnit(h.piece_length()));
    data.add(KEY_PROP_COMMENT, h.comment());
    data.add(KEY_PROP_WASTED, misc::friendlyUnit(h.total_failed_bytes() + h.total_redundant_bytes()));
    data.add(KEY_PROP_UPLOADED, QString(misc::friendlyUnit(h.all_time_upload()) + " (" + misc::friendlyUnit(h.total_payload_upload()) + " " + tr("this session") + ")"));
    data.add(KEY_PROP_DOWNLOADED, QString(misc::friendlyUnit(h.all_time_download()) + " (" + misc::friendlyUnit(h.total_payload_download()) + " " + tr("this session") + ")"));
    data.add(KEY_PROP_UP_LIMIT, h.upload_limit() <= 0 ? QString::fromUtf8("∞") : misc::friendlyUnit(h.upload_limit(), true));
    data.add(KEY_PROP_DL_LIMIT, h.download_limit() <= 0 ? QString::fromUtf8("∞") : misc::friendlyUnit(h.download_limit(), true));
    QString elapsed_txt = misc::userFriendlyDuration(h.active_time());
    if (h.is_seed())
      elapsed_txt += " ("+tr("Seeded for %1", "e.g. Seeded for 3m10s").arg(misc::userFriendlyDuration(h.seeding_time()))+")";
    data.add(KEY_PROP_TIME_ELAPSED, elapsed_txt);
    data.add(KEY_PROP_CONNECT_COUNT, QString(QString::number(h.num_connections()) + " (" + tr("%1 max", "e.g. 10 max").arg(QString::number(h.connections_limit())) + ")"));
    const qreal ratio = QBtSession::instance()->getRealRatio(h.hash());
    /* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
    ** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
    data.add(KEY_PROP_RATIO, ratio > 100. ? QString::fromUtf8("∞") : QString::number((int)(ratio*10)/10.0, 'f', 1));
  } catch(const std::exception& e) {
    qWarning() << Q_FUNC_INFO << "Invalid torrent: " << e.what();
    return QString();
  }

  return data.toString();
}
Exemple #3
0
/**
 * Returns the properties for a torrent in JSON format.
 *
 * The return value is a JSON-formatted dictionary.
 * The dictionary keys are:
 *   - "save_path": Torrent save path
 *   - "creation_date": Torrent creation date
 *   - "piece_size": Torrent piece size
 *   - "comment": Torrent comment
 *   - "total_wasted": Total data wasted for torrent
 *   - "total_uploaded": Total data uploaded for torrent
 *   - "total_downloaded": Total data uploaded for torrent
 *   - "up_limit": Torrent upload limit
 *   - "dl_limit": Torrent download limit
 *   - "time_elapsed": Torrent elapsed time
 *   - "nb_connections": Torrent connection count
 *   - "share_ratio": Torrent share ratio
 */
QByteArray btjson::getPropertiesForTorrent(const QString& hash)
{
  CACHED_VARIABLE_FOR_HASH(QVariantMap, data, CACHE_DURATION_MS, hash);
  try {
    QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);

    libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters);
    if (!status.has_metadata)
      return QByteArray();

    // Save path
    QString save_path = fsutils::toNativePath(TorrentPersistentData::getSavePath(hash));
    if (save_path.isEmpty())
      save_path = fsutils::toNativePath(h.save_path());
    data[KEY_PROP_SAVE_PATH] = save_path;
    data[KEY_PROP_CREATION_DATE] = h.creation_date();
    data[KEY_PROP_PIECE_SIZE] = misc::friendlyUnit(h.piece_length(), false, true);
    data[KEY_PROP_COMMENT] = h.comment();
    data[KEY_PROP_WASTED] = misc::friendlyUnit(status.total_failed_bytes + status.total_redundant_bytes, false, true);
    data[KEY_PROP_UPLOADED] = QString(misc::friendlyUnit(status.all_time_upload, false, true) + " (" + misc::friendlyUnit(status.total_payload_upload, false, true) + " " + tr("this session") + ")");
    data[KEY_PROP_DOWNLOADED] = QString(misc::friendlyUnit(status.all_time_download, false, true) + " (" + misc::friendlyUnit(status.total_payload_download, false, true) + " " + tr("this session") + ")");
    data[KEY_PROP_UP_LIMIT] = h.upload_limit() <= 0 ? QString::fromUtf8("∞") : misc::friendlyUnit(h.upload_limit(), true, true);
    data[KEY_PROP_DL_LIMIT] = h.download_limit() <= 0 ? QString::fromUtf8("∞") : misc::friendlyUnit(h.download_limit(), true, true);
    QString elapsed_txt = misc::userFriendlyDuration(status.active_time);
    if (h.is_seed(status))
      elapsed_txt += " ("+tr("Seeded for %1", "e.g. Seeded for 3m10s").arg(misc::userFriendlyDuration(status.seeding_time))+")";
    data[KEY_PROP_TIME_ELAPSED] = elapsed_txt;
    data[KEY_PROP_CONNECT_COUNT] = QString(QString::number(status.num_connections) + " (" + tr("%1 max", "e.g. 10 max").arg(QString::number(status.connections_limit)) + ")");
    const qreal ratio = QBtSession::instance()->getRealRatio(status);
    data[KEY_PROP_RATIO] = ratio > 100. ? QString::fromUtf8("∞") : misc::accurateDoubleToString(ratio, 1, false);
  } catch(const std::exception& e) {
    qWarning() << Q_FUNC_INFO << "Invalid torrent: " << e.what();
    return QByteArray();
  }

  return json::toJson(data);
}