-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
268 lines (209 loc) · 8.57 KB
/
main.cpp
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
#include <iostream>
#include <filesystem>
#include "Downloader.h"
#include "UserProgressRecorder.h"
const std::string API_ENDPOINT = "https://api.mangadex.org";
const std::string TEMP_DIR = "temp";
const std::string OUTPUT_DIR = "manga";
const std::string PROGRESS_FILE = "progress.csv";
bool imagesToPDF(const std::string &input_dir, const std::string &output_dir, const std::string &output_name) {
// just call img2pdf on the input directory. EZPZ
std::filesystem::create_directories(output_dir);
std::cout << output_dir << std::endl;
std::cout << output_name << std::endl;
std::cout << "Creating PDF..." << std::endl;
std::string command = "img2pdf --rotation=ifvalid '" + input_dir + "'/* -o '" + output_dir + "/" + output_name +
".pdf' 2>/dev/null";
// ".pdf'";
// Some files have invalid rotation data. There is no way for me to fix this, so I have to suppress the error.
// std::cout << command << std::endl;
try {
std::system(command.c_str());
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
return false;
}
return true;
}
std::vector<std::string> splitString(const std::string &str) {
std::vector<std::string> words;
std::istringstream iss(str);
std::string word;
while (iss >> word) {
words.push_back(word);
}
return words;
}
void welcomeUser() {
std::cout << "Welcome to MangaMania!" << std::endl;
std::cout << "This app lets you download and read your favorite manga, and keep track of your progress."
<< std::endl;
std::cout << "Enjoy your reading!" << std::endl;
}
Manga getManga(Downloader &downloader) {
// Ask the user to enter the name of the manga they want to download.
std::string mangaName;
std::cout << "Enter the name of the manga you want to search for: ";
std::cin.ignore(); // Clear input buffer
std::getline(std::cin, mangaName);
// Replace spaces with plus signs.
std::replace(mangaName.begin(), mangaName.end(), ' ', '+');
std::vector<std::string> includeTagsVector;
std::vector<std::string> excludeTagsVector;
std::string searchByTag;
std::cout << "Do you want to search by tag? (y/n): ";
std::cin >> searchByTag;
// If they do, take input for include tags and exclude tags.
if (searchByTag == "y") {
std::string includeTags;
std::string excludeTags;
std::cout << "Enter the tags you want to include (separated by spaces): " << std::endl;
std::cin.ignore();
std::getline(std::cin, includeTags);
std::cout << "Enter the tags you want to exclude (separated by spaces): " << std::endl;
std::getline(std::cin, excludeTags);
// Make every tag lowercase.
std::transform(includeTags.begin(), includeTags.end(), includeTags.begin(), ::tolower);
std::transform(excludeTags.begin(), excludeTags.end(), excludeTags.begin(), ::tolower);
includeTagsVector = splitString(includeTags);
excludeTagsVector = splitString(excludeTags);
// Get the manga from the API.
}
std::vector<Manga> manga_list = downloader.searchManga(mangaName, includeTagsVector, excludeTagsVector);
// Early return if the manga list is empty.
if (manga_list.empty()) {
std::cout << "No manga found." << std::endl;
return {};
}
// Define colour escape sequences.
const std::string GREEN = "\033[32m";
const std::string RESET = "\033[0m";
for (int i = 0; i < manga_list.size(); i++) {
std::cout << GREEN << i + 1 << ". " << manga_list[i].getTitle() << RESET << GREEN << std::endl;
std::cout << RESET << "" << manga_list[i].getDescription() << std::endl;
}
// Ask the user to select a manga.
int choice;
std::cout << "\033[1;34mEnter the number of the manga: \033[0m";
std::cin >> choice;
return manga_list[choice - 1];
}
Manga::Chapter getChapter(Downloader &downloader, Manga &manga) {
// Search for the chapter
std::vector<Manga::Chapter> chapter_list = downloader.searchChapter(manga.getUuid());
// Early return if the chapter list is empty.
if (chapter_list.empty()) {
std::cout << "No chapters found. This manga will not be downloaded or saved." << std::endl;
return {};
}
for (int i = 0; i < chapter_list.size(); i++) {
std::cout << i + 1 << ". " << chapter_list[i].title << std::endl;
}
// Ask the user to select a chapter.
int choice;
std::cout << "\033[1;34mEnter the number of the chapter you want to select: \033[0m"; //blue
std::cin >> choice;
return chapter_list[choice - 1];
}
void downloadManga(Downloader &downloader, UserProgressRecorder &progressRecorder) {
Manga manga_choice = getManga(downloader);
// Check if the manga_choice is empty
if (manga_choice.getUuid().empty()) {
return;
}
Manga::Chapter chapter_choice = getChapter(downloader, manga_choice);
std::cout << "Manga: " << manga_choice.getTitle() << std::endl;
std::cout << "Chapter: " << chapter_choice.title << std::endl;
// Download the images.
std::cout << "Downloading images..." << std::endl;
if (!downloader.downloadChapter(chapter_choice, TEMP_DIR)) {
return;
}
// Convert the images to a PDF.
std::cout << "Converting images to PDF..." << std::endl;
std::string saveDirectory;
if (manga_choice.getTitle().empty()) {
saveDirectory = OUTPUT_DIR + "/" + manga_choice.getUuid();
} else {
saveDirectory = OUTPUT_DIR + "/" + manga_choice.getTitle();
}
imagesToPDF(TEMP_DIR, saveDirectory, chapter_choice.title);
// Ask the user if they want to add this to their progress.
std::string addToProgress;
std::cout << "Do you want to add this to your progress? (y/n): ";
std::cin >> addToProgress;
if (addToProgress == "y") {
// Add the chapter to the user's progress.
progressRecorder.addProgress(manga_choice, chapter_choice);
}
}
void manageMangaProgress(Downloader &downloader, UserProgressRecorder &progressRecorder) {
std::cout << "Manage manga progress" << std::endl;
std::cout << "1. View progress" << std::endl;
std::cout << "2. Add progress" << std::endl;
std::cout << "3. Remove progress" << std::endl;
std::cout << "4. Exit" << std::endl;
int choice;
std::cin >> choice;
if (choice == 1) {
// View progress
progressRecorder.viewAllProgress();
}
if (choice == 2) {
// Add progress
Manga manga_choice = getManga(downloader);
// Check if the manga_choice is empty
if (manga_choice.getUuid().empty()) {
return;
}
Manga::Chapter chapter_choice = getChapter(downloader, manga_choice);
if (chapter_choice.uuid.empty()) {
return;
}
progressRecorder.addProgress(manga_choice, chapter_choice);
}
if (choice == 3) {
std::cout << "Enter the UUID of the manga you want to remove progress for: ";
std::string mangaUuid;
std::cin >> mangaUuid;
progressRecorder.removeProgress(mangaUuid);
}
if (choice == 4) {
return;
}
}
int main() {
std::filesystem::remove_all(TEMP_DIR);
Downloader downloader(API_ENDPOINT);
UserProgressRecorder progressRecorder(PROGRESS_FILE);
welcomeUser();
int choice;
do {
std::cout << "Please choose an option:" << std::endl;
std::cout << "1. Download manga" << std::endl;
std::cout << "2. Manage library" << std::endl;
std::cout << "3. Exit" << std::endl;
std::cin >> choice;
if (std::cin.fail()) {
std::cout << "Invalid input. Please try again." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} else if (choice < 1 || choice > 3) {
std::cout << "Invalid input. Please try again." << std::endl;
} else {
switch (choice) {
case 1:
downloadManga(downloader, progressRecorder);
break;
case 2:
manageMangaProgress(downloader, progressRecorder);
break;
case 3:
std::cout << "Exiting..." << std::endl;
break;
}
}
} while (choice != 3);
std::filesystem::remove_all(TEMP_DIR); // clean up
return 0;
}