Files
fix-subs-in-nested-folders/fix_subs.cpp
2022-11-19 02:24:32 -06:00

119 lines
4.3 KiB
C++

#include <iostream>
#include <filesystem>
#include <string>
#include "ezOptionParser.hpp"
int main(int argc, const char * argv[])
{
//std::string path = "/mnt/z/Marvel's Daredevil (2015) [tvdb-281662]";
// args parser
ez::ezOptionParser opt;
opt.overview = "Fix subs in nested folders for TV show for Plex";
opt.syntax = "fix_subs -f <FOLDER>";
opt.add (
"",
1,
0,
0,
"PATH of the folder to be fixed.",
"-f"
);
opt.parse(argc, argv);
if (opt.lastArgs.size() != 1)
{
std::cerr << "ERROR: Expected 1 argument, but got " << opt.lastArgs.size() << std::endl << std::endl;
std::string usage;
opt.getUsage(usage);
std::cout << usage;
exit(1);
}
std::string path = *opt.lastArgs[0];
// remove trailing slash if there is one
while (path.back() == '/')
{
path = path.substr(0, path.length()-1);
}
std::cout << "Path set to: " << path << std::endl << "Press ENTER to continue" << std::endl;
std::cin.ignore();
std::cout << "Beginning to fix subs folders..." << std::endl;
// iterate through the top directory
for (const auto &top_dir : std::filesystem::directory_iterator(path))
{
// if there are files in the top directory, ignore them
if (std::filesystem::is_regular_file(top_dir.path()) == true)
continue;
// only care for directories starting with 'Season'
std::string folder_name = top_dir.path().filename();
if (folder_name.starts_with("Season") == true)
{
std::string top_seasons_dir = top_dir.path();
// ensure "Subs" directory exists
std::string seasons_dir_subs = top_seasons_dir + "/Subs";
if (std::filesystem::exists(seasons_dir_subs) == true)
{
// interate through the seasons directories
for (const auto &seasons_dir : std::filesystem::directory_iterator(top_dir.path()))
{
std::string item = seasons_dir.path();
// ignore txt files
if (item.ends_with(".txt") == true || item.ends_with(".srt"))
continue;
// only care about the files
if (std::filesystem::is_regular_file(item) == true)
{
std::string full_episode_filename = seasons_dir.path().filename();
// strip extension from filename
std::string episode_filename = full_episode_filename.substr(0, full_episode_filename.find_last_of("."));
// iterate through the season's subs folder
std::string episode_sub_dir = seasons_dir_subs + "/" + episode_filename;
for (const auto &subs_dir : std::filesystem::directory_iterator(episode_sub_dir))
{
// only care about English (lol)
std::string sub_filename = subs_dir.path().filename();
if (sub_filename.ends_with("English.srt"))
{
// fix filename
std::string sub_new_filename = episode_filename + "_" + sub_filename;
// move to season directory
std::string new_sub_dir = top_seasons_dir + "/" + sub_new_filename;
try
{
std::filesystem::rename(subs_dir.path(), new_sub_dir);
}
catch (std::filesystem::filesystem_error &e)
{
std::cout << "Error: " << e.what() << std::endl;
}
}
}
}
}
//delete "Subs" directory
try
{
std::filesystem::remove_all(seasons_dir_subs);
}
catch (std::filesystem::filesystem_error &e)
{
std::cout << "Error: " << e.what() << std::endl;
}
}
}
}
return 0;
}