-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprojectdownloader.cpp
More file actions
224 lines (169 loc) · 6.9 KB
/
projectdownloader.cpp
File metadata and controls
224 lines (169 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// SPDX-License-Identifier: Apache-2.0
#include <iostream>
#include <thread>
#include <nlohmann/json.hpp>
#include <cpr/cpr.h>
#include "projectdownloader.h"
#include "downloaderfactory.h"
#include "idownloader.h"
using namespace libscratchcpp;
static const std::string PROJECT_META_PREFIX = "https://api.scratch.mit.edu/projects/";
static const std::string PROJECT_JSON_PREFIX = "https://projects.scratch.mit.edu/";
static const std::string ASSET_PREFIX = "https://assets.scratch.mit.edu/internalapi/asset/";
static const std::string ASSET_SUFFIX = "/get";
#define CHECK_CANCEL() \
if (m_cancel) { \
m_downloadedAssetCount = 0; \
std::cout << "Download aborted!" << std::endl; \
return false; \
}
ProjectDownloader::ProjectDownloader(IDownloaderFactory *downloaderFactory) :
m_downloaderFactory(downloaderFactory)
{
if (!m_downloaderFactory)
m_downloaderFactory = DownloaderFactory::instance().get();
m_tokenDownloader = m_downloaderFactory->create();
m_jsonDownloader = m_downloaderFactory->create();
}
bool ProjectDownloader::downloadJson(const std::string &projectId)
{
m_cancel = false;
// Get project token
std::cout << "Fetching project info of " << projectId << std::endl;
bool ret = m_tokenDownloader->download(PROJECT_META_PREFIX + projectId);
if (!ret) {
std::cerr << "Could not fetch project info of " << projectId << std::endl;
return false;
}
CHECK_CANCEL();
nlohmann::json json;
try {
json = nlohmann::json::parse(m_tokenDownloader->text());
} catch (std::exception &e) {
std::cerr << "Could not parse project info of " << projectId << std::endl;
std::cerr << e.what() << std::endl;
return false;
}
std::string token;
try {
token = json["project_token"];
} catch (std::exception &e) {
std::cerr << "Could not read project token of " << projectId << std::endl;
std::cerr << e.what() << std::endl;
return false;
}
// Download project JSON
std::cout << "Downloading project JSON of " << projectId << std::endl;
ret = m_jsonDownloader->download(PROJECT_JSON_PREFIX + projectId + "?token=" + token);
if (!ret) {
std::cerr << "Failed to download project JSON of " << projectId << std::endl;
return false;
}
CHECK_CANCEL();
return true;
}
bool ProjectDownloader::downloadAssets(const std::vector<std::string> &assetIds)
{
m_cancel = false;
auto count = assetIds.size();
// unsigned int threadCount = std::thread::hardware_concurrency();
unsigned int threadCount = 20;
m_assets.clear();
m_assets.reserve(count);
m_downloadedAssetCount = 0;
for (unsigned int i = 0; i < count; i++)
m_assets.push_back(std::string());
std::cout << "Downloading " << count << " asset(s)";
if (threadCount > 1)
std::cout << " using " << threadCount << " threads";
std::cout << std::endl;
// Create downloaders
std::vector<std::shared_ptr<IDownloader>> downloaders;
for (unsigned int i = 0; i < threadCount; i++)
downloaders.push_back(m_downloaderFactory->create());
// Download assets
auto f = [this, count](std::shared_ptr<IDownloader> downloader, int index, const std::string &id) {
if (m_cancel)
return;
bool ret = downloader->download(ASSET_PREFIX + id + ASSET_SUFFIX);
if (!ret) {
std::cerr << "Failed to download asset: " << id << std::endl;
m_cancel = true;
return;
}
m_assetsMutex.lock();
m_assets[index] = downloader->text();
m_downloadedAssetCount++;
m_downloadProgressChanged(m_downloadedAssetCount, count);
m_assetsMutex.unlock();
};
std::unordered_map<int, std::pair<std::thread, std::shared_ptr<IDownloader>>> threads;
bool done = false;
unsigned int lastCount = 0, i = 0;
while (true) {
int addCount = threadCount - threads.size();
for (int j = 0; j < addCount; j++) {
if (i >= count) {
done = true;
break;
}
std::shared_ptr<IDownloader> freeDownloader = nullptr;
for (auto downloader : downloaders) {
auto it = std::find_if(threads.begin(), threads.end(), [&downloader](std::pair<const int, std::pair<std::thread, std::shared_ptr<IDownloader>>> &pair) {
return pair.second.second == downloader;
});
if (it == threads.cend()) {
freeDownloader = downloader;
break;
}
}
assert(freeDownloader);
threads[i] = { std::thread(f, freeDownloader, i, assetIds[i]), freeDownloader };
i++;
}
std::this_thread::sleep_for(std::chrono::milliseconds(25));
m_assetsMutex.lock();
if (m_downloadedAssetCount != lastCount && !m_cancel) {
std::cout << "Downloaded assets: " << m_downloadedAssetCount << " of " << count << std::endl;
lastCount = m_downloadedAssetCount;
}
std::vector<int> toRemove;
for (auto &[index, info] : threads) {
if (!m_assets[index].empty())
toRemove.push_back(index);
}
m_assetsMutex.unlock();
for (int index : toRemove) {
threads[index].first.join();
threads.erase(index);
}
if (done || m_cancel) {
for (auto &[index, info] : threads)
info.first.join();
break;
}
}
CHECK_CANCEL();
return true;
}
void ProjectDownloader::cancel()
{
m_cancel = true;
m_downloadedAssetCount = 0;
}
sigslot::signal<unsigned int, unsigned int> &ProjectDownloader::downloadProgressChanged()
{
return m_downloadProgressChanged;
}
const std::string &ProjectDownloader::json() const
{
return m_jsonDownloader->text();
}
const std::vector<std::string> &ProjectDownloader::assets() const
{
return m_assets;
}
unsigned int ProjectDownloader::downloadedAssetCount() const
{
return m_downloadedAssetCount;
}