view src/track/media.cc @ 117:2c1b6782e1d0

pages/torrents: work around conversion error on Linux
author Paper <mrpapersonic@gmail.com>
date Tue, 07 Nov 2023 16:06:17 -0500
parents ab191e28e69d
children 39521c47c7a3
line wrap: on
line source

#include "track/media.h"
#include "track/constants.h"
#include "animia.h"
#include "anitomy/anitomy.h"
#include "core/filesystem.h"
#include "core/strings.h"
#include <string>
#include <unordered_map>
#include <vector>

namespace Track {
namespace Media {

Filesystem::Path GetCurrentPlaying() {
	/* getting all open files */
	std::vector<int> pids = Animia::get_all_pids();
	for (int i : pids) {
		for (const std::string& player : media_players) {
			if (Animia::get_process_name(i) != player)
				continue;
			for (const std::string& f : Animia::filter_system_files(Animia::get_open_files(i))) {
				Filesystem::Path p(f);
				for (const std::string& ext : media_extensions) {
					if (p.Extension() == ext)
						return p;
				}
			}
		}
	}
	return Filesystem::Path();
}

std::unordered_map<std::string, std::string> GetMapFromElements(const anitomy::Elements& elements) {
	/* there are way more than this in anitomy, but we only need basic information
	   I also just prefer using maps than using the ".get()" stuff which is why I'm doing this */
	std::unordered_map<std::string, std::string> ret;

	ret["title"] = Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle));
	ret["filename"] = Strings::ToUtf8String(elements.get(anitomy::kElementFileName));
	ret["language"] = Strings::ToUtf8String(elements.get(anitomy::kElementLanguage));
	ret["group"] = Strings::ToUtf8String(elements.get(anitomy::kElementReleaseGroup));
	ret["episode"] = Strings::ToUtf8String(elements.get(anitomy::kElementEpisodeNumber));
	ret["resolution"] = Strings::ToUtf8String(elements.get(anitomy::kElementVideoResolution));

	return ret;
}

std::unordered_map<std::string, std::string> GetFileElements(std::string basename) {
	anitomy::Anitomy anitomy;
	anitomy.Parse(Strings::ToWstring(basename));

	return GetMapFromElements(anitomy.elements());
}

std::unordered_map<std::string, std::string> GetFileElements(Filesystem::Path path) {
	anitomy::Anitomy anitomy;
	anitomy.Parse(Strings::ToWstring(path.Basename()));

	return GetMapFromElements(anitomy.elements());
}

} // namespace Media
} // namespace Track