Skip to content

Commit 58f9ffa

Browse files
committed
Json modify implementation : 1.1.1
1 parent fe3ba2b commit 58f9ffa

File tree

6 files changed

+155
-64
lines changed

6 files changed

+155
-64
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Project exclude paths
2-
/cmake-build-debug/
2+
/cmake-build-debug/
3+
/videos/

.idea/.name

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.25)
2-
project(YConverter_)
2+
project(YConverter)
33

44
set(CMAKE_CXX_STANDARD 23)
55
set(CMAKE_EXE_LINKER_FLAGS "-static")

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM ubuntu:latest
2+
LABEL authors="matisse"
3+
4+
ENTRYPOINT ["top", "-b"]

json.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,36 @@ std::ifstream file("info.json");
102102
}
103103
}
104104

105-
void modify() {
105+
void modify(const std::string& path, const std::string& destination, const std::string& verbose) {
106+
std::string file = "info.json";
107+
108+
std::ifstream jsonFile(file);
109+
110+
if (!jsonFile.is_open()) {
111+
std::cerr << "The file can't be open." << std::endl;
112+
return;
113+
}
114+
115+
std::string jsonContent((std::istreambuf_iterator<char>(jsonFile)), std::istreambuf_iterator<char>());
116+
117+
jsonFile.close();
106118

119+
nlohmann::json jsonObj = nlohmann::json::parse(jsonContent);
120+
121+
jsonObj["destination"] = destination;
122+
jsonObj["path"] = path;
123+
jsonObj["verbose"] = verbose;
124+
125+
std::ofstream jsonFile2(file);
126+
127+
if (!jsonFile2.is_open()) {
128+
std::cerr << "The file can't be open." << std::endl;
129+
return;
130+
}
131+
132+
jsonFile2 << jsonObj.dump(4);
133+
134+
jsonFile2.close();
135+
136+
std::cout << "Json file has been writed." << std::endl;
107137
}

main.cpp

Lines changed: 116 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#include <string>
33
#include <exception>
44
#include <Windows.h>
5-
#include "json.cpp"
65
#include <conio.h>
6+
#include "json.cpp"
77

88
std::string GetCurrentDirectory()
99
{
@@ -16,8 +16,27 @@ std::string GetCurrentDirectory()
1616

1717

1818
int main() {
19+
std::string choice;
1920
std::string url;
20-
bool logs = false;
21+
22+
/*
23+
* Json file content
24+
*/
25+
std::string destination;
26+
std::string path;
27+
std::string verboseString;
28+
bool verbose;
29+
30+
std::cout << " __ __ ____ __ \n"
31+
"/\\ \\ /\\ \\/\\ _`\\ /\\ \\__ \n"
32+
"\\ `\\`\\\\/'/\\ \\ \\/\\_\\ ___ ___ __ __ __ _ __ \\ \\ ,_\\ __ _ __ \n"
33+
" `\\ `\\ /' \\ \\ \\/_/_ / __`\\ /' _ `\\ /\\ \\/\\ \\ /'__`\\ /\\`'__\\ \\ \\ \\/ /'__`\\ /\\`'__\\\n"
34+
" `\\ \\ \\ \\ \\ \\L\\ \\/\\ \\L\\ \\/\\ \\/\\ \\ \\ \\ \\_/ |/\\ __/ \\ \\ \\/ \\ \\ \\_ /\\ __/ \\ \\ \\/ \n"
35+
" \\ \\_\\ \\ \\____/\\ \\____/\\ \\_\\ \\_\\ \\ \\___/ \\ \\____\\ \\ \\_\\ \\ \\__\\\\ \\____\\ \\ \\_\\ \n"
36+
" \\/_/ \\/___/ \\/___/ \\/_/\\/_/ \\/__/ \\/____/ \\/_/ \\/__/ \\/____/ \\/_/ \n"
37+
" \n"
38+
" \n"
39+
"" << std::endl;
2140

2241
if (!initiated()) {
2342
createJson();
@@ -31,65 +50,102 @@ int main() {
3150
initJson();
3251
}
3352

34-
std::cout << "Please enter youtube URL : ";
35-
std::getline(std::cin, url);
36-
37-
if (url.find(url) == std::string::npos) {
38-
std::cout << "Invalid URL" << std::endl;
39-
return 0;
40-
} else {
41-
std::cout << "Valid URL" << std::endl;
42-
}
43-
44-
std::ifstream jsonFile(file);
45-
46-
if (!jsonFile.is_open()) {
47-
std::cerr << "The file can't be open." << std::endl;
48-
return 0;
49-
}
50-
51-
std::string jsonContent((std::istreambuf_iterator<char>(jsonFile)), std::istreambuf_iterator<char>());
52-
53-
jsonFile.close();
54-
55-
nlohmann::json jsonObj = nlohmann::json::parse(jsonContent);
56-
57-
std::string destination = jsonObj["destination"];
58-
59-
60-
if (destination.find(destination) == std::string::npos) {
61-
std::cout << "Invalid file destination" << std::endl;
62-
return 0;
63-
} else {
64-
std::cout << "Valid file destination" << std::endl;
65-
}
66-
67-
std::string verboseString = jsonObj["verbose"];
68-
std::string path = jsonObj["path"];
69-
bool verbose;
70-
71-
if (verboseString == "true") {
72-
verbose = true;
73-
} else {
74-
verbose = false;
75-
}
76-
77-
// Download the video
78-
try {
79-
if (verbose) {
80-
std::cout << "Downloading..." << std::endl;
81-
std::string command = path + "/yt-dlp_min.exe --verbose --ignore-errors -o \"" + destination + "/%(title)s.%(ext)s\" " + url;
82-
system(command.c_str());
53+
while (true) {
54+
std::cout << "[1] Download a video" << std::endl;
55+
std::cout << "[2] Modify json information" << std::endl;
56+
std::cout << "[3] Exit" << std::endl;
57+
std::cout << "Choice : ";
58+
std::getline(std::cin, choice);
59+
60+
if (choice == "1") {
61+
std::cout << "Enter the URL of the video : ";
62+
std::getline(std::cin, url);
63+
64+
if (url.find(url) == std::string::npos) {
65+
std::cout << "Invalid URL" << std::endl;
66+
return 0;
67+
} else {
68+
std::cout << "Valid URL" << std::endl;
69+
70+
std::ifstream jsonFile(file);
71+
72+
if (!jsonFile.is_open()) {
73+
std::cerr << "The file can't be open." << std::endl;
74+
return 0;
75+
}
76+
77+
std::string jsonContent((std::istreambuf_iterator<char>(jsonFile)), std::istreambuf_iterator<char>());
78+
79+
jsonFile.close();
80+
81+
nlohmann::json jsonObj = nlohmann::json::parse(jsonContent);
82+
83+
destination = jsonObj["destination"];
84+
85+
86+
if (destination.find(destination) == std::string::npos) {
87+
std::cout << "Invalid file destination" << std::endl;
88+
std::cout << "Please modify the json file" << std::endl;
89+
std::cout << "Press any key to continue..." << std::endl;
90+
getch();
91+
} else {
92+
std::cout << "Valid file destination" << std::endl;
93+
}
94+
95+
verboseString = jsonObj["verbose"];
96+
path = jsonObj["path"];
97+
98+
if (verboseString == "true") {
99+
verbose = true;
100+
} else {
101+
verbose = false;
102+
}
103+
104+
// Download the video
105+
try {
106+
std::string command = path + "/yt-dlp_min.exe --ignore-errors -o \"" + destination + "/%(title)s.%(ext)s\" " + url;
107+
108+
if (verbose) {
109+
std::cout << "Downloading..." << std::endl;
110+
command = command + " --verbose";
111+
system(command.c_str());
112+
} else {
113+
std::cout << "Downloading..." << std::endl;
114+
system(command.c_str());
115+
}
116+
} catch (std::exception& e) {
117+
std::cout << "Error : " << e.what() << std::endl;
118+
std::cout << "Press any key to continue..." << std::endl;
119+
getch();
120+
}
121+
122+
std::cout << "Download completed in the folder " + destination << std::endl;
123+
std::cout << "Press any key to continue..." << std::endl;
124+
getch();
125+
126+
}
127+
128+
} else if (choice == "2") {
129+
std::cout << "Change the directoy wehre the videos are downloaded : ";
130+
std::getline(std::cin, destination);
131+
132+
std::cout << "Change the path of YConverter : ";
133+
std::getline(std::cin, path);
134+
135+
std::cout << "Do you want to see the download progression ? (y/n) : ";
136+
std::getline(std::cin, verboseString);
137+
138+
if (verboseString == "y") {
139+
verboseString = "true";
140+
} else {
141+
verboseString = "false";
142+
}
143+
144+
modify(path, destination, verboseString);
145+
} else if (choice == "3") {
146+
return 0;
83147
} else {
84-
std::cout << "Downloading..." << std::endl;
85-
std::string command = path + "/yt-dlp_min.exe --ignore-errors -o \"" + destination + "/%(title)s.%(ext)s\" " + url;
86-
system(command.c_str());
148+
std::cout << "Invalid choice" << std::endl;
87149
}
88-
} catch (std::exception& e) {
89-
std::cout << "Error : " << e.what() << std::endl;
90150
}
91-
92-
std::cout << "Download completed" << std::endl;
93-
94-
return 0;
95151
}

0 commit comments

Comments
 (0)