libaria2ex.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. //
  36. // Compile and link like this:
  37. // $ g++ -Wall -O2 -g -std=c++11 -o libaria2ex libaria2ex.cc -laria2
  38. #include <iostream>
  39. #include <chrono>
  40. #include <aria2/aria2.h>
  41. int downloadEventCallback(aria2::Session* session, aria2::DownloadEvent event,
  42. aria2::A2Gid gid, void* userData)
  43. {
  44. switch (event) {
  45. case aria2::EVENT_ON_DOWNLOAD_COMPLETE:
  46. std::cerr << "COMPLETE";
  47. break;
  48. case aria2::EVENT_ON_DOWNLOAD_ERROR:
  49. std::cerr << "ERROR";
  50. break;
  51. default:
  52. return 0;
  53. }
  54. std::cerr << " [" << aria2::gidToHex(gid) << "] ";
  55. aria2::DownloadHandle* dh = aria2::getDownloadHandle(session, gid);
  56. if (!dh)
  57. return 0;
  58. if (dh->getNumFiles() > 0) {
  59. aria2::FileData f = dh->getFile(1);
  60. // Path may be empty if the file name has not been determined yet.
  61. if (f.path.empty()) {
  62. if (!f.uris.empty()) {
  63. std::cerr << f.uris[0].uri;
  64. }
  65. }
  66. else {
  67. std::cerr << f.path;
  68. }
  69. }
  70. aria2::deleteDownloadHandle(dh);
  71. std::cerr << std::endl;
  72. return 0;
  73. }
  74. int main(int argc, char** argv)
  75. {
  76. int rv;
  77. if (argc < 2) {
  78. std::cerr << "Usage: libaria2ex URI [URI...]\n"
  79. << "\n"
  80. << " Download given URIs in parallel in the current directory."
  81. << std::endl;
  82. exit(EXIT_SUCCESS);
  83. }
  84. aria2::libraryInit();
  85. // session is actually singleton: 1 session per process
  86. aria2::Session* session;
  87. // Create default configuration. The libaria2 takes care of signal
  88. // handling.
  89. aria2::SessionConfig config;
  90. // Add event callback
  91. config.downloadEventCallback = downloadEventCallback;
  92. session = aria2::sessionNew(aria2::KeyVals(), config);
  93. // Add download item to session
  94. for (int i = 1; i < argc; ++i) {
  95. std::vector<std::string> uris = {argv[i]};
  96. aria2::KeyVals options;
  97. rv = aria2::addUri(session, nullptr, uris, options);
  98. if (rv < 0) {
  99. std::cerr << "Failed to add download " << uris[0] << std::endl;
  100. }
  101. }
  102. auto start = std::chrono::steady_clock::now();
  103. for (;;) {
  104. rv = aria2::run(session, aria2::RUN_ONCE);
  105. if (rv != 1) {
  106. break;
  107. }
  108. // the application can call aria2 API to add URI or query progress
  109. // here
  110. auto now = std::chrono::steady_clock::now();
  111. auto count =
  112. std::chrono::duration_cast<std::chrono::milliseconds>(now - start)
  113. .count();
  114. // Print progress information once per 500ms
  115. if (count >= 500) {
  116. start = now;
  117. aria2::GlobalStat gstat = aria2::getGlobalStat(session);
  118. std::cerr << "Overall #Active:" << gstat.numActive
  119. << " #waiting:" << gstat.numWaiting
  120. << " D:" << gstat.downloadSpeed / 1024 << "KiB/s"
  121. << " U:" << gstat.uploadSpeed / 1024 << "KiB/s " << std::endl;
  122. std::vector<aria2::A2Gid> gids = aria2::getActiveDownload(session);
  123. for (const auto& gid : gids) {
  124. aria2::DownloadHandle* dh = aria2::getDownloadHandle(session, gid);
  125. if (dh) {
  126. std::cerr << " [" << aria2::gidToHex(gid) << "] "
  127. << dh->getCompletedLength() << "/" << dh->getTotalLength()
  128. << "("
  129. << (dh->getTotalLength() > 0
  130. ? (100 * dh->getCompletedLength() /
  131. dh->getTotalLength())
  132. : 0)
  133. << "%)"
  134. << " D:" << dh->getDownloadSpeed() / 1024
  135. << "KiB/s, U:" << dh->getUploadSpeed() / 1024 << "KiB/s"
  136. << std::endl;
  137. aria2::deleteDownloadHandle(dh);
  138. }
  139. }
  140. }
  141. }
  142. rv = aria2::sessionFinal(session);
  143. aria2::libraryDeinit();
  144. return rv;
  145. }