22#include < string>
33#include < exception>
44#include < Windows.h>
5+ #include " json.cpp"
6+ #include < conio.h>
57
68std::string GetCurrentDirectory ()
79{
@@ -12,56 +14,75 @@ std::string GetCurrentDirectory()
1214 return std::string (buffer).substr (0 , pos);
1315}
1416
17+
1518int main () {
1619 std::string url;
1720 bool logs = false ;
1821
22+ if (!initiated ()) {
23+ createJson ();
24+
25+ std::cout << " Json file created" << std::endl;
26+ }
27+
28+ std::string file = " info.json" ;
29+
30+ if (!fileContainsCharacter (file) || jsonIsNull ()) {
31+ initJson ();
32+ }
33+
1934 std::cout << " Please enter youtube URL : " ;
2035 std::getline (std::cin, url);
2136
22- // Check if the URL is valid
2337 if (url.find (url) == std::string::npos) {
2438 std::cout << " Invalid URL" << std::endl;
2539 return 0 ;
2640 } else {
2741 std::cout << " Valid URL" << std::endl;
2842 }
2943
30- // File destination
31- std::string destination;
32- std::cout << " Please enter a file destination : " ;
33- std::getline (std::cin, destination);
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+
3459
35- // Check if the file destination is valid
3660 if (destination.find (destination) == std::string::npos) {
3761 std::cout << " Invalid file destination" << std::endl;
3862 return 0 ;
3963 } else {
4064 std::cout << " Valid file destination" << std::endl;
4165 }
4266
43- std::string verbose;
44- std::cout << " Do you want to see the logs ? (y/n) : " ;
45- std::getline (std::cin, verbose) ;
67+ std::string verboseString = jsonObj[ " verbose" ] ;
68+ std::string path = jsonObj[ " path " ] ;
69+ bool verbose;
4670
47- if (verbose == " y" ) {
48- logs = true ;
49- } else if (verbose == " n" ) {
50- logs = false ;
71+ if (verboseString == " true" ) {
72+ verbose = true ;
5173 } else {
52- std::cout << " Invalid answer" << std::endl;
53- return 0 ;
74+ verbose = false ;
5475 }
5576
5677 // Download the video
5778 try {
58- if (logs ) {
79+ if (verbose ) {
5980 std::cout << " Downloading..." << std::endl;
60- std::string command = GetCurrentDirectoryA () + " /yt-dlp_min.exe --verbose --ignore-errors -o \" " + destination + " /%(title)s.%(ext)s\" " + url;
81+ std::string command = path + " /yt-dlp_min.exe --verbose --ignore-errors -o \" " + destination + " /%(title)s.%(ext)s\" " + url;
6182 system (command.c_str ());
6283 } else {
6384 std::cout << " Downloading..." << std::endl;
64- std::string command = GetCurrentDirectoryA () + " /yt-dlp_min.exe --ignore-errors -o \" " + destination + " /%(title)s.%(ext)s\" " + url;
85+ std::string command = path + " /yt-dlp_min.exe --ignore-errors -o \" " + destination + " /%(title)s.%(ext)s\" " + url;
6586 system (command.c_str ());
6687 }
6788 } catch (std::exception& e) {
0 commit comments