-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
433 lines (363 loc) · 17.3 KB
/
Copy pathmain.cpp
File metadata and controls
433 lines (363 loc) · 17.3 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <thread>
#include <future>
#include <chrono>
#include <filesystem>
#include <regex>
#include <iomanip>
#include <sstream>
#include <curl/curl.h>
namespace fs = std::filesystem;
// ANSI Color codes for clean output
namespace Colors {
const std::string RESET = "\033[0m";
const std::string BOLD = "\033[1m";
const std::string DIM = "\033[2m";
const std::string RED = "\033[31m";
const std::string GREEN = "\033[32m";
const std::string YELLOW = "\033[33m";
const std::string BLUE = "\033[34m";
const std::string MAGENTA = "\033[35m";
const std::string CYAN = "\033[36m";
const std::string WHITE = "\033[37m";
}
// Clean design elements
namespace Design {
void printHeader() {
std::cout << Colors::CYAN << Colors::BOLD;
std::cout << "██╗███╗ ███╗ ██████╗ ██╗ ██╗███████╗██╗ ██╗\n";
std::cout << "██║████╗ ████║██╔════╝ ██║ ██║██╔════╝╚██╗██╔╝\n";
std::cout << "██║██╔████╔██║██║ ███╗██║ ██║█████╗ ╚███╔╝ \n";
std::cout << "██║██║╚██╔╝██║██║ ██║██║ ██║██╔══╝ ██╔██╗ \n";
std::cout << "██║██║ ╚═╝ ██║╚██████╔╝███████╗██║███████╗██╔╝ ██╗\n";
std::cout << "╚═╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝╚══════╝╚═╝ ╚═╝\n";
std::cout << Colors::RESET << "\n";
std::cout << Colors::YELLOW << "High-Performance Manga Image Link Extractor\n";
std::cout << Colors::DIM << "Version 1.0 - Built with C++17\n" << Colors::RESET;
std::cout << "\n";
}
void printUsage(const std::string& programName) {
std::cout << Colors::YELLOW << Colors::BOLD << "USAGE:" << Colors::RESET << "\n";
std::cout << " " << Colors::GREEN << programName << Colors::WHITE << " <filename> <start_chapter> <end_chapter>" << Colors::RESET << "\n\n";
std::cout << Colors::YELLOW << Colors::BOLD << "EXAMPLES:" << Colors::RESET << "\n";
std::cout << " " << Colors::DIM << "# Process chapters 1-100 from manga.txt" << Colors::RESET << "\n";
std::cout << " " << Colors::GREEN << programName << Colors::WHITE << " manga.txt 1 100" << Colors::RESET << "\n\n";
std::cout << " " << Colors::DIM << "# Process chapters 50-75" << Colors::RESET << "\n";
std::cout << " " << Colors::GREEN << programName << Colors::WHITE << " chapters.txt 50 75" << Colors::RESET << "\n\n";
}
void printSeparator(char symbol = '-', int length = 60) {
std::cout << Colors::CYAN;
for (int i = 0; i < length; ++i) {
std::cout << symbol;
}
std::cout << Colors::RESET << "\n";
}
void printProgress(const std::string& message, const std::string& color = Colors::BLUE) {
std::cout << color << "> " << Colors::BOLD << message << Colors::RESET << "\n";
}
void printSuccess(const std::string& message) {
std::cout << Colors::GREEN << "[SUCCESS] " << Colors::BOLD << message << Colors::RESET << "\n";
}
void printWarning(const std::string& message) {
std::cout << Colors::YELLOW << "[WARNING] " << Colors::BOLD << message << Colors::RESET << "\n";
}
void printError(const std::string& message) {
std::cout << Colors::RED << "[ERROR] " << Colors::BOLD << message << Colors::RESET << "\n";
}
void printStats(const std::string& label, const std::string& value, const std::string& unit = "") {
std::cout << Colors::CYAN << " * " << Colors::WHITE << std::left << std::setw(20) << label
<< Colors::YELLOW << Colors::BOLD << std::right << std::setw(10) << value
<< Colors::DIM << " " << unit << Colors::RESET << "\n";
}
}
struct WriteCallbackData {
std::string data;
};
// Callback function for libcurl to write response data
size_t WriteCallback(void* contents, size_t size, size_t nmemb, WriteCallbackData* userp) {
size_t totalSize = size * nmemb;
userp->data.append(static_cast<char*>(contents), totalSize);
return totalSize;
}
class ImgLiex {
private:
std::unordered_map<int, std::string> chapterLinks;
std::unordered_map<int, size_t> expectedCountsCache;
std::string folderName;
int processedCount = 0;
int skippedCount = 0;
int errorCount = 0;
std::mutex statsMutex;
public:
ImgLiex(const std::string& filename) : folderName(fs::path(filename).stem()) {
fs::create_directories(folderName);
}
bool extractChapterLinks(const std::string& inputFile) {
std::ifstream file(inputFile);
if (!file.is_open()) {
std::cerr << "Error: Cannot open file " << inputFile << std::endl;
return false;
}
std::string line;
while (std::getline(file, line)) {
if (line.find("# Chapter") == 0) {
// Extract chapter number
std::regex chapterRegex(R"(# Chapter (\d+))");
std::smatch match;
if (std::regex_search(line, match, chapterRegex)) {
int chapterNum = std::stoi(match[1].str());
// Get next line as URL
if (std::getline(file, line)) {
// Trim whitespace
line.erase(0, line.find_first_not_of(" \t\r\n"));
line.erase(line.find_last_not_of(" \t\r\n") + 1);
chapterLinks[chapterNum] = line;
}
}
}
}
Design::printProgress("Loaded chapter links", Colors::GREEN);
Design::printStats("Total chapters", std::to_string(chapterLinks.size()));
Design::printSeparator();
return true;
}
std::vector<std::string> extractImageLinks(const std::string& htmlContent) {
std::vector<std::string> imageLinks;
// Simple regex to find img tags with class="imgholder"
std::regex imgRegex(R"(<img[^>]*class=["\']imgholder["\'][^>]*src=["\']([^"\']+)["\'][^>]*>)");
std::regex imgRegex2(R"(<img[^>]*src=["\']([^"\']+)["\'][^>]*class=["\']imgholder["\'][^>]*>)");
std::sregex_iterator start(htmlContent.begin(), htmlContent.end(), imgRegex);
std::sregex_iterator end;
for (std::sregex_iterator i = start; i != end; ++i) {
std::smatch match = *i;
imageLinks.push_back(match[1].str());
}
// Try second pattern if first didn't match
if (imageLinks.empty()) {
start = std::sregex_iterator(htmlContent.begin(), htmlContent.end(), imgRegex2);
for (std::sregex_iterator i = start; i != end; ++i) {
std::smatch match = *i;
imageLinks.push_back(match[1].str());
}
}
return imageLinks;
}
std::string downloadHTML(const std::string& url) {
CURL* curl;
CURLcode res;
WriteCallbackData data;
curl = curl_easy_init();
if (!curl) {
throw std::runtime_error("Failed to initialize CURL");
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "imgliex/1.0");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
throw std::runtime_error("CURL error: " + std::string(curl_easy_strerror(res)));
}
return data.data;
}
void saveImageLinks(int chapterNum, const std::vector<std::string>& imageLinks) {
fs::path chapterFolder = fs::path(folderName) / ("chapter-" + std::to_string(chapterNum));
fs::create_directories(chapterFolder);
std::ofstream baseFile(chapterFolder / "base.txt");
if (!baseFile.is_open()) {
throw std::runtime_error("Cannot create base.txt for chapter " + std::to_string(chapterNum));
}
for (const auto& link : imageLinks) {
baseFile << link << "\n";
}
}
size_t countLinesfast(const fs::path& filePath) {
std::ifstream file(filePath, std::ios::binary);
if (!file.is_open()) return 0;
size_t count = 0;
std::string line;
while (std::getline(file, line)) {
++count;
}
return count;
}
bool chapterAlreadyProcessed(int chapterNum, size_t expectedImgCount) {
fs::path chapterFolder = fs::path(folderName) / ("chapter-" + std::to_string(chapterNum));
fs::path baseFile = chapterFolder / "base.txt";
if (!fs::exists(baseFile)) {
return false;
}
size_t lineCount = countLinesfast(baseFile);
return lineCount == expectedImgCount;
}
void processChapter(int chapterNum, const std::string& url) {
try {
size_t expectedImgCount;
// Check cache first
auto cacheIt = expectedCountsCache.find(chapterNum);
if (cacheIt != expectedCountsCache.end()) {
expectedImgCount = cacheIt->second;
} else {
std::string htmlContent = downloadHTML(url);
std::vector<std::string> imageLinks = extractImageLinks(htmlContent);
expectedImgCount = imageLinks.size();
expectedCountsCache[chapterNum] = expectedImgCount;
// Save immediately since we already have the data
if (!chapterAlreadyProcessed(chapterNum, expectedImgCount)) {
saveImageLinks(chapterNum, imageLinks);
{
std::lock_guard<std::mutex> lock(statsMutex);
processedCount++;
}
std::cout << Colors::GREEN << "[OK] " << Colors::WHITE << "Chapter " << Colors::YELLOW << std::setw(3) << chapterNum
<< Colors::WHITE << " -> " << Colors::CYAN << expectedImgCount << Colors::DIM << " images" << Colors::RESET << "\n";
return;
}
}
// Check if already processed
if (chapterAlreadyProcessed(chapterNum, expectedImgCount)) {
{
std::lock_guard<std::mutex> lock(statsMutex);
skippedCount++;
}
std::cout << Colors::YELLOW << "[SKIP] " << Colors::WHITE << "Chapter " << Colors::YELLOW << std::setw(3) << chapterNum
<< Colors::DIM << " -> already processed (" << expectedImgCount << " images)" << Colors::RESET << "\n";
return;
}
// Process if not cached and not processed
std::string htmlContent = downloadHTML(url);
std::vector<std::string> imageLinks = extractImageLinks(htmlContent);
saveImageLinks(chapterNum, imageLinks);
{
std::lock_guard<std::mutex> lock(statsMutex);
processedCount++;
}
std::cout << Colors::GREEN << "[OK] " << Colors::WHITE << "Chapter " << Colors::YELLOW << std::setw(3) << chapterNum
<< Colors::WHITE << " -> " << Colors::CYAN << imageLinks.size() << Colors::DIM << " images" << Colors::RESET << "\n";
} catch (const std::exception& e) {
{
std::lock_guard<std::mutex> lock(statsMutex);
errorCount++;
}
std::cout << Colors::RED << "[FAIL] " << Colors::WHITE << "Chapter " << Colors::YELLOW << std::setw(3) << chapterNum
<< Colors::RED << " -> " << e.what() << Colors::RESET << "\n";
}
}
void processChapters(int startChapter, int endChapter, int numThreads = 4) {
Design::printProgress("Starting processing", Colors::MAGENTA);
Design::printStats("Chapter range", std::to_string(startChapter) + " - " + std::to_string(endChapter));
Design::printStats("Threads", std::to_string(numThreads));
Design::printStats("Output folder", folderName);
Design::printSeparator();
auto startTime = std::chrono::high_resolution_clock::now();
std::vector<std::future<void>> futures;
for (int chapterNum = startChapter; chapterNum <= endChapter; ++chapterNum) {
auto it = chapterLinks.find(chapterNum);
if (it != chapterLinks.end()) {
futures.push_back(
std::async(std::launch::async, [this, chapterNum, url = it->second]() {
this->processChapter(chapterNum, url);
})
);
// Limit concurrent threads
if (futures.size() >= static_cast<size_t>(numThreads)) {
for (auto& future : futures) {
future.wait();
}
futures.clear();
}
} else {
Design::printWarning("Chapter " + std::to_string(chapterNum) + " not found in input file");
}
}
// Wait for remaining futures
for (auto& future : futures) {
future.wait();
}
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
// Print final statistics
std::cout << "\n";
Design::printSeparator('=');
Design::printSuccess("Processing Complete!");
Design::printSeparator();
Design::printStats("Processed", std::to_string(processedCount), "chapters");
Design::printStats("Skipped", std::to_string(skippedCount), "chapters");
Design::printStats("Errors", std::to_string(errorCount), "chapters");
Design::printStats("Total time", std::to_string(duration.count() / 1000.0), "seconds");
if (processedCount > 0) {
double avgTime = (duration.count() / 1000.0) / processedCount;
Design::printStats("Avg per chapter", std::to_string(avgTime), "sec/chapter");
}
Design::printSeparator();
std::cout << Colors::CYAN << "Results saved in: " << Colors::YELLOW << Colors::BOLD
<< folderName << Colors::RESET << "\n\n";
Design::printSeparator('=');
}
};
int main(int argc, char* argv[]) {
// Initialize CURL globally
curl_global_init(CURL_GLOBAL_DEFAULT);
// Print clean header
Design::printHeader();
// Check command line arguments
if (argc != 4) {
Design::printError("Invalid number of arguments!");
Design::printUsage(argv[0]);
Design::printSeparator('=');
curl_global_cleanup();
return 1;
}
try {
std::string filename = argv[1];
int startChapter = std::stoi(argv[2]);
int endChapter = std::stoi(argv[3]);
if (startChapter > endChapter) {
Design::printError("Start chapter cannot be greater than end chapter!");
curl_global_cleanup();
return 1;
}
if (startChapter < 1 || endChapter < 1) {
Design::printError("Chapter numbers must be positive!");
curl_global_cleanup();
return 1;
}
// Check if input file exists
if (!fs::exists(filename)) {
Design::printError("Input file '" + filename + "' not found!");
curl_global_cleanup();
return 1;
}
Design::printProgress("Initializing ImgLiex", Colors::BLUE);
ImgLiex extractor(filename);
Design::printProgress("Loading chapter links from " + filename, Colors::BLUE);
if (!extractor.extractChapterLinks(filename)) {
Design::printError("Failed to load chapter links!");
curl_global_cleanup();
return 1;
}
// Auto-detect optimal thread count (max 8 to be nice to servers)
int numThreads = std::min(8, static_cast<int>(std::thread::hardware_concurrency()));
if (numThreads == 0) numThreads = 4; // fallback
extractor.processChapters(startChapter, endChapter, numThreads);
} catch (const std::invalid_argument& e) {
Design::printError("Invalid chapter number format!");
Design::printUsage(argv[0]);
curl_global_cleanup();
return 1;
} catch (const std::exception& e) {
Design::printError("Unexpected error: " + std::string(e.what()));
curl_global_cleanup();
return 1;
}
// Cleanup CURL
curl_global_cleanup();
return 0;
}