|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <thread> |
| 5 | +#include <nlohmann/json.hpp> |
| 6 | + |
| 7 | +#include "projectdownloader.h" |
| 8 | +#include "downloaderfactory.h" |
| 9 | +#include "idownloader.h" |
| 10 | + |
| 11 | +using namespace libscratchcpp; |
| 12 | + |
| 13 | +static const std::string PROJECT_META_PREFIX = "https://api.scratch.mit.edu/projects/"; |
| 14 | +static const std::string PROJECT_JSON_PREFIX = "https://projects.scratch.mit.edu/"; |
| 15 | +static const std::string ASSET_PREFIX = "https://assets.scratch.mit.edu/internalapi/asset/"; |
| 16 | +static const std::string ASSET_SUFFIX = "/get"; |
| 17 | + |
| 18 | +#define CHECK_CANCEL() \ |
| 19 | + m_cancelMutex.lock(); \ |
| 20 | + if (m_cancel) { \ |
| 21 | + m_downloadedAssetCount = 0; \ |
| 22 | + return false; \ |
| 23 | + } \ |
| 24 | + m_cancelMutex.unlock() |
| 25 | + |
| 26 | +ProjectDownloader::ProjectDownloader(IDownloaderFactory *downloaderFactory) : |
| 27 | + m_downloaderFactory(downloaderFactory) |
| 28 | +{ |
| 29 | + if (!m_downloaderFactory) |
| 30 | + m_downloaderFactory = DownloaderFactory::instance().get(); |
| 31 | + |
| 32 | + m_tokenDownloader = m_downloaderFactory->create(); |
| 33 | + m_jsonDownloader = m_downloaderFactory->create(); |
| 34 | +} |
| 35 | + |
| 36 | +bool ProjectDownloader::downloadJson(const std::string &projectId) |
| 37 | +{ |
| 38 | + m_cancelMutex.lock(); |
| 39 | + m_cancel = false; |
| 40 | + m_cancelMutex.unlock(); |
| 41 | + |
| 42 | + // Get project token |
| 43 | + std::cout << "Fetching project info of " << projectId << std::endl; |
| 44 | + m_tokenDownloader->startDownload(PROJECT_META_PREFIX + projectId); |
| 45 | + m_tokenDownloader->wait(); |
| 46 | + |
| 47 | + if (m_tokenDownloader->text().empty()) { |
| 48 | + std::cerr << "Could not fetch project info of " << projectId << std::endl; |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + if (m_tokenDownloader->isCancelled()) |
| 53 | + return false; |
| 54 | + |
| 55 | + CHECK_CANCEL(); |
| 56 | + |
| 57 | + nlohmann::json json; |
| 58 | + |
| 59 | + try { |
| 60 | + json = nlohmann::json::parse(m_tokenDownloader->text()); |
| 61 | + } catch (std::exception &e) { |
| 62 | + std::cerr << "Could not parse project info of " << projectId << std::endl; |
| 63 | + std::cerr << e.what() << std::endl; |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + std::string token; |
| 68 | + |
| 69 | + try { |
| 70 | + token = json["project_token"]; |
| 71 | + } catch (std::exception &e) { |
| 72 | + std::cerr << "Could not read project token of " << projectId << std::endl; |
| 73 | + std::cerr << e.what() << std::endl; |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + // Download project JSON |
| 78 | + std::cout << "Downloading project JSON of " << projectId << std::endl; |
| 79 | + m_jsonDownloader->startDownload(PROJECT_JSON_PREFIX + projectId + "?token=" + token); |
| 80 | + m_jsonDownloader->wait(); |
| 81 | + |
| 82 | + if (m_jsonDownloader->text().empty()) { |
| 83 | + std::cerr << "Failed to download project JSON of " << projectId << std::endl; |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + if (m_jsonDownloader->isCancelled()) |
| 88 | + return false; |
| 89 | + |
| 90 | + CHECK_CANCEL(); |
| 91 | + |
| 92 | + return true; |
| 93 | +} |
| 94 | + |
| 95 | +bool ProjectDownloader::downloadAssets(const std::vector<std::string> &assetIds) |
| 96 | +{ |
| 97 | + m_cancelMutex.lock(); |
| 98 | + m_cancel = false; |
| 99 | + m_cancelMutex.unlock(); |
| 100 | + |
| 101 | + auto count = assetIds.size(); |
| 102 | + unsigned int threadCount = std::thread::hardware_concurrency(); |
| 103 | + unsigned int times; // how many times we should download assets simultaneously (in "groups") |
| 104 | + m_assets.clear(); |
| 105 | + m_downloadedAssetCount = 0; |
| 106 | + |
| 107 | + // Calculate number of "groups" |
| 108 | + if (threadCount == 0) { |
| 109 | + times = count; |
| 110 | + threadCount = 1; |
| 111 | + } else |
| 112 | + times = std::ceil(count / static_cast<double>(threadCount)); |
| 113 | + |
| 114 | + std::cout << "Downloading " << count << " asset(s)"; |
| 115 | + |
| 116 | + if (threadCount > 1) |
| 117 | + std::cout << " using " << threadCount << " threads"; |
| 118 | + |
| 119 | + std::cout << std::endl; |
| 120 | + |
| 121 | + // Create downloaders |
| 122 | + std::vector<std::shared_ptr<IDownloader>> downloaders; |
| 123 | + |
| 124 | + for (unsigned int i = 0; i < threadCount; i++) |
| 125 | + downloaders.push_back(m_downloaderFactory->create()); |
| 126 | + |
| 127 | + // Download assets |
| 128 | + for (unsigned int i = 0; i < times; i++) { |
| 129 | + unsigned int currentCount = std::min(threadCount, static_cast<unsigned int>(count - i * threadCount)); |
| 130 | + |
| 131 | + for (unsigned int j = 0; j < currentCount; j++) |
| 132 | + downloaders[j]->startDownload(ASSET_PREFIX + assetIds[i * threadCount + j] + ASSET_SUFFIX); |
| 133 | + |
| 134 | + for (unsigned int j = 0; j < currentCount; j++) { |
| 135 | + downloaders[j]->wait(); |
| 136 | + assert(m_assets.size() == i * threadCount + j); |
| 137 | + |
| 138 | + if (downloaders[j]->isCancelled()) |
| 139 | + return false; |
| 140 | + |
| 141 | + CHECK_CANCEL(); |
| 142 | + |
| 143 | + m_assets.push_back(downloaders[j]->text()); |
| 144 | + m_downloadedAssetCount++; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return true; |
| 149 | +} |
| 150 | + |
| 151 | +void ProjectDownloader::cancel() |
| 152 | +{ |
| 153 | + m_tokenDownloader->cancel(); |
| 154 | + m_jsonDownloader->cancel(); |
| 155 | + m_cancelMutex.lock(); |
| 156 | + m_cancel = true; |
| 157 | + m_cancelMutex.unlock(); |
| 158 | + m_downloadedAssetCount = 0; |
| 159 | +} |
| 160 | + |
| 161 | +const std::string &ProjectDownloader::json() const |
| 162 | +{ |
| 163 | + return m_jsonDownloader->text(); |
| 164 | +} |
| 165 | + |
| 166 | +const std::vector<std::string> &ProjectDownloader::assets() const |
| 167 | +{ |
| 168 | + return m_assets; |
| 169 | +} |
| 170 | + |
| 171 | +unsigned int ProjectDownloader::downloadedAssetCount() const |
| 172 | +{ |
| 173 | + return m_downloadedAssetCount; |
| 174 | +} |
0 commit comments