Skip to content

Commit fe3ba2b

Browse files
committed
1.1.0 release, change libs and json method
1 parent 21f12e3 commit fe3ba2b

File tree

5 files changed

+24922
-18
lines changed

5 files changed

+24922
-18
lines changed

info.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"path": "",
3+
"destination": "",
4+
"verbose": false
5+
}

json.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// Created by matis on 01/07/2023.
3+
//
4+
5+
#include <string>
6+
#include <fstream>
7+
#include <filesystem>
8+
#include <iostream>
9+
#include "nlohmann/json.hpp"
10+
11+
void createJson() {
12+
std::ofstream json("info.json");
13+
}
14+
15+
bool initiated() {
16+
std::string file = "info.json";
17+
18+
if (std::filesystem::exists(file)) {
19+
return true;
20+
} else {
21+
return false;
22+
}
23+
}
24+
25+
bool fileContainsCharacter(const std::string& fileName) {
26+
std::ifstream file(fileName);
27+
28+
// Vérifier si le file est ouvert avec succès
29+
if (!file.is_open()) {
30+
std::cerr << "Impossible d'ouvrir le file." << std::endl;
31+
return false;
32+
}
33+
34+
char caractere;
35+
while (file.get(caractere)) {
36+
// Vérifier si le caractère est un caractère imprimable
37+
if (isprint(caractere)) {
38+
file.close();
39+
return true;
40+
}
41+
}
42+
43+
file.close();
44+
return false;
45+
}
46+
47+
void initJson() {
48+
std::string destination;
49+
std::string path;
50+
std::string verbose;
51+
52+
std::string file = "info.json";
53+
54+
std::cout << "Enter destination path for your videos : ";
55+
std::getline(std::cin, destination);
56+
57+
std::cout << "Enter path of YConverter (The folder which contains YConverter.exe) : ";
58+
std::getline(std::cin, path);
59+
60+
std::cout << "Do you want to see the logs ? (y/n) : ";
61+
std::getline(std::cin, verbose);
62+
63+
if (verbose == "y") {
64+
verbose = "true";
65+
} else if (verbose == "n") {
66+
verbose = "false";
67+
} else {
68+
std::cout << "Invalid answer" << std::endl;
69+
return;
70+
}
71+
72+
nlohmann::json jsonObject {
73+
{"destination", destination},
74+
{"path", path},
75+
{"verbose", verbose}
76+
};
77+
78+
std::ofstream jsonFile(file);
79+
80+
if (!jsonFile.is_open()) {
81+
std::cerr << "Impossible d'ouvrir le file." << std::endl;
82+
return;
83+
}
84+
85+
jsonFile << jsonObject.dump(4);
86+
87+
jsonFile.close();
88+
89+
std::cout << "Json file has been writed." << std::endl;
90+
}
91+
92+
bool jsonIsNull() {
93+
std::ifstream file("info.json");
94+
nlohmann::json jsonObject;
95+
96+
file >> jsonObject;
97+
98+
if (jsonObject["destination"].is_null() || jsonObject["path"].is_null() || jsonObject["verbose"].is_null()) {
99+
return true;
100+
} else {
101+
return false;
102+
}
103+
}
104+
105+
void modify() {
106+
107+
}

main.cpp

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

68
std::string GetCurrentDirectory()
79
{
@@ -12,56 +14,75 @@ std::string GetCurrentDirectory()
1214
return std::string(buffer).substr(0, pos);
1315
}
1416

17+
1518
int 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

Comments
 (0)