Skip to content

Commit

Permalink
Auto Install yt-dlp, Corrected README Typos
Browse files Browse the repository at this point in the history
  • Loading branch information
SenpaiSimon committed May 31, 2024
1 parent fc06f68 commit d4906ae
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Please install the following packages before using this script:
## Config-File

Upon running the script for the first time you will be prompted with the message that a `config.txt`-File was created.
Please add your paths and your api-key to the `config.json`-File.

### Config-File Example

Expand All @@ -55,7 +54,7 @@ Please add your paths and your api-key to the `config.json`-File.

# Usage

Just use `plex-yt-dlp -h` or `plex-yt-dl --help` for the help screen.
Just use `./plex-yt-dlp -h` or `./plex-yt-dlp --help` for the help screen.

This will print something like:

Expand Down Expand Up @@ -95,22 +94,22 @@ This will print something like:

```bash
# Download a video/playlist
ytdl.sh -u ulrHere -t video
./plex-yt-dlp -u urlHere -t video

# Download a single track or playlist
ytdl.sh -u urlHere -t music
./plex-yt-dlp -u urlHere -t music

# Download a rss feed
ytdl.sh -u urlHere -t rss
./plex-yt-dlp -u urlHere -t rss

# Download a playlist starting from index 10
ytdl.sh -u ulrHere -t video -io 10
./plex-yt-dlp -u urlHere -t video -io 10

# Download a music playlist starting from index 10
ytdl.sh -u ulrHere -t music -io 10
./plex-yt-dlp -u urlHere -t music -io 10

# Download a rss feed starting from index 10
ytdl.sh -u ulrHere -t rss -io 10
./plex-yt-dlp -u urlHere -t rss -io 10
```


Expand Down
3 changes: 2 additions & 1 deletion include/misc/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class tools {

public:
static bool fileExists(string filePath);
static void installYtdlp();
static void downloadFile(string fileName, string url);
static string getRequest(string req);
static string escapePath(const string& path);
// prints a line
static void printLine();
// execute command and get the output
Expand Down
65 changes: 64 additions & 1 deletion src/misc/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ void tools::printLine() {
cout << "==========================================================================================================" << endl;
}


bool tools::fileExists(string filePath) {
return fs::exists(filePath);
}


string escapePath(const string& path) {
#ifdef _WIN32
const char escapeChar = '^';
Expand All @@ -27,6 +29,7 @@ string escapePath(const string& path) {
return escapedPath;
}


string tools::executeCommand(const string& command) {
FILE *fp = popen(command.c_str(), "r");
if(fp == NULL) {
Expand All @@ -44,6 +47,7 @@ string tools::executeCommand(const string& command) {
return result;
}


tools::binaryStatus_t tools::checkBinaryStatus(string name) {
string commandRes = "";

Expand Down Expand Up @@ -78,6 +82,37 @@ tools::binaryStatus_t tools::checkBinaryStatus(string name) {
}
}


void tools::downloadFile(string fileName, string url) {
std::ofstream outFile;
outFile.open(fileName);

// Create a curl_ios object to handle the stream
curl_ios<std::ostream> writer(outFile);
// Pass it to the easy constructor and watch the content returned in that file!
curl_easy easy(writer);

// Add some option to the easy handle
easy.add<CURLOPT_URL>(url.c_str());
easy.add<CURLOPT_FOLLOWLOCATION>(1L);
easy.add<CURLOPT_SSL_OPTIONS>(CURLSSLOPT_NATIVE_CA); // todo linux maybe nicht?

try {
// Execute the request
easy.perform();

} catch (curl_easy_exception &error) {
// If you want to print the last error.
std::cerr<<error.what()<<std::endl;

// If you want to print the entire error stack you can do
error.print_traceback();
}

outFile.close();
}


string tools::getRequest(string req) {
// Create a stringstream object
ostringstream str;
Expand All @@ -103,6 +138,21 @@ string tools::getRequest(string req) {
return str.str();
}


void tools::installYtdlp() {
cout << "==\t " << colors::magenta("-> Downloading yt-dlp") << endl;
fs::create_directory("./temp");
downloadFile("./temp/yt-dlp", "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp");
executeCommand("chmod +x ./temp/yt-dlp");

cout << "==\t " << colors::magenta("-> Moving yt-dlp to /usr/bin/ -- please enter password") << endl;
executeCommand("sudo mv ./temp/yt-dlp /usr/bin/");
fs::remove_all("./temp");

cout << "==\t " << colors::green("-> Done") << endl;
}


void tools::checkRequirements() {
tools::printLine();
tools::binaryStatus lastStatus;
Expand All @@ -119,7 +169,12 @@ void tools::checkRequirements() {
missing = true;
if(lastStatus == MISSING) {
cout << colors::boldRed(" - missing!") << endl;
cout << "==\t " << colors::red("-> Install from here: ") << "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp" << endl;
#ifndef _WIN32
installYtdlp();
#else
cout << "==\t " << colors::red("-> Download from here: ") << "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp" << endl;
cout << "==\t " << colors::red(" then add the binary to your PATH") << endl;
#endif
} else if(lastStatus == FOUND) {
cout << colors::boldRed(" - found, but not executable!") << endl;
cout << "==\t " << colors::red("-> Make it executable: ") << "chmod +x /path/to/your/bin/yt-dlp" << endl;
Expand Down Expand Up @@ -163,6 +218,11 @@ void tools::checkRequirements() {
if(lastStatus != EXECUTABLE) {
missing = true;
cout << colors::boldRed(" - missing!") << endl;
#ifdef _WIN32
cout << "==\t " << colors::red("-> Install with command: ") << "winget install -e --id cURL.cURL" << endl;
#else
cout << "==\t " << colors::red("-> Install with command: ") << "sudo apt install curl" << endl;
#endif
} else {
cout << colors::boldGreen(" - found!") << endl;
}
Expand All @@ -175,6 +235,7 @@ void tools::checkRequirements() {
}
}


void tools::printHelp() {
tools::printLine();
cout << "==" << endl;
Expand Down Expand Up @@ -207,12 +268,14 @@ void tools::printHelp() {
tools::printLine();
}


smatch tools::getRegexMatches(string str, string pattern) {
smatch matches;
regex_search(str, matches, regex(pattern));
return matches;
}


void tools::printVersion() {
cout << "Version is: " << CUR_VERSION << endl;
}

0 comments on commit d4906ae

Please sign in to comment.