| 1 |
/* |
| 2 |
* Copyright (C) 2005-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "VideoTagLoaderNFO.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "NfoFile.h" |
| 13 |
#include "URL.h" |
| 14 |
#include "filesystem/Directory.h" |
| 15 |
#include "filesystem/StackDirectory.h" |
| 16 |
#include "utils/FileUtils.h" |
| 17 |
#include "utils/StringUtils.h" |
| 18 |
#include "utils/URIUtils.h" |
| 19 |
#include "utils/log.h" |
| 20 |
#include "video/VideoInfoTag.h" |
| 21 |
|
| 22 |
#include <utility> |
| 23 |
|
| 24 |
using namespace XFILE; |
| 25 |
|
| 26 |
CVideoTagLoaderNFO::CVideoTagLoaderNFO(const CFileItem& item, |
| 27 |
ADDON::ScraperPtr info, |
| 28 |
bool lookInFolder) |
| 29 |
: IVideoInfoTagLoader(item, std::move(info), lookInFolder) |
| 30 |
{ |
| 31 |
if (m_info && m_info->Content() == CONTENT_TVSHOWS && m_item.m_bIsFolder) |
| 32 |
m_path = URIUtils::AddFileToFolder(m_item.GetPath(), "tvshow.nfo"); |
| 33 |
else |
| 34 |
m_path = FindNFO(m_item, lookInFolder); |
| 35 |
} |
| 36 |
|
| 37 |
bool CVideoTagLoaderNFO::HasInfo() const |
| 38 |
{ |
| 39 |
return !m_path.empty() && CFileUtils::Exists(m_path); |
| 40 |
} |
| 41 |
|
| 42 |
CInfoScanner::INFO_TYPE CVideoTagLoaderNFO::Load(CVideoInfoTag& tag, |
| 43 |
bool prioritise, |
| 44 |
std::vector<EmbeddedArt>*) |
| 45 |
{ |
| 46 |
CNfoFile nfoReader; |
| 47 |
CInfoScanner::INFO_TYPE result = CInfoScanner::NO_NFO; |
| 48 |
if (m_info && m_info->Content() == CONTENT_TVSHOWS && !m_item.m_bIsFolder) |
| 49 |
result = nfoReader.Create(m_path, m_info, m_item.GetVideoInfoTag()->m_iEpisode); |
| 50 |
else if (m_info) |
| 51 |
result = nfoReader.Create(m_path, m_info); |
| 52 |
|
| 53 |
if (result == CInfoScanner::FULL_NFO || result == CInfoScanner::COMBINED_NFO) |
| 54 |
nfoReader.GetDetails(tag, nullptr, prioritise); |
| 55 |
|
| 56 |
if (result == CInfoScanner::URL_NFO || result == CInfoScanner::COMBINED_NFO) |
| 57 |
{ |
| 58 |
m_url = nfoReader.ScraperUrl(); |
| 59 |
m_info = nfoReader.GetScraperInfo(); |
| 60 |
} |
| 61 |
|
| 62 |
std::string type; |
| 63 |
switch(result) |
| 64 |
{ |
| 65 |
case CInfoScanner::COMBINED_NFO: |
| 66 |
type = "mixed"; |
| 67 |
break; |
| 68 |
case CInfoScanner::FULL_NFO: |
| 69 |
type = "full"; |
| 70 |
break; |
| 71 |
case CInfoScanner::URL_NFO: |
| 72 |
type = "URL"; |
| 73 |
break; |
| 74 |
case CInfoScanner::NO_NFO: |
| 75 |
type = ""; |
| 76 |
break; |
| 77 |
case CInfoScanner::OVERRIDE_NFO: |
| 78 |
type = "override"; |
| 79 |
break; |
| 80 |
default: |
| 81 |
type = "malformed"; |
| 82 |
} |
| 83 |
if (result != CInfoScanner::NO_NFO) |
| 84 |
CLog::Log(LOGDEBUG, "VideoInfoScanner: Found matching {} NFO file: {}", type, |
| 85 |
CURL::GetRedacted(m_path)); |
| 86 |
else |
| 87 |
CLog::Log(LOGDEBUG, "VideoInfoScanner: No NFO file found. Using title search for '{}'", |
| 88 |
CURL::GetRedacted(m_item.GetPath())); |
| 89 |
|
| 90 |
return result; |
| 91 |
} |
| 92 |
|
| 93 |
std::string CVideoTagLoaderNFO::FindNFO(const CFileItem& item, |
| 94 |
bool movieFolder) const |
| 95 |
{ |
| 96 |
std::string nfoFile; |
| 97 |
// Find a matching .nfo file |
| 98 |
if (!item.m_bIsFolder) |
| 99 |
{ |
| 100 |
if (URIUtils::IsInRAR(item.GetPath())) // we have a rarred item - we want to check outside the rars |
| 101 |
{ |
| 102 |
CFileItem item2(item); |
| 103 |
CURL url(item.GetPath()); |
| 104 |
std::string strPath = URIUtils::GetDirectory(url.GetHostName()); |
| 105 |
item2.SetPath(URIUtils::AddFileToFolder(strPath, |
| 106 |
URIUtils::GetFileName(item.GetPath()))); |
| 107 |
return FindNFO(item2, movieFolder); |
| 108 |
} |
| 109 |
|
| 110 |
// grab the folder path |
| 111 |
std::string strPath = URIUtils::GetDirectory(item.GetPath()); |
| 112 |
|
| 113 |
if (movieFolder && !item.IsStack()) |
| 114 |
{ // looking up by folder name - movie.nfo takes priority - but not for stacked items (handled below) |
| 115 |
nfoFile = URIUtils::AddFileToFolder(strPath, "movie.nfo"); |
| 116 |
if (CFileUtils::Exists(nfoFile)) |
| 117 |
return nfoFile; |
| 118 |
} |
| 119 |
|
| 120 |
// try looking for .nfo file for a stacked item |
| 121 |
if (item.IsStack()) |
| 122 |
{ |
| 123 |
// first try .nfo file matching first file in stack |
| 124 |
CStackDirectory dir; |
| 125 |
std::string firstFile = dir.GetFirstStackedFile(item.GetPath()); |
| 126 |
CFileItem item2; |
| 127 |
item2.SetPath(firstFile); |
| 128 |
nfoFile = FindNFO(item2, movieFolder); |
| 129 |
// else try .nfo file matching stacked title |
| 130 |
if (nfoFile.empty()) |
| 131 |
{ |
| 132 |
std::string stackedTitlePath = dir.GetStackedTitlePath(item.GetPath()); |
| 133 |
item2.SetPath(stackedTitlePath); |
| 134 |
nfoFile = FindNFO(item2, movieFolder); |
| 135 |
} |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
// already an .nfo file? |
| 140 |
if (URIUtils::HasExtension(item.GetPath(), ".nfo")) |
| 141 |
nfoFile = item.GetPath(); |
| 142 |
// no, create .nfo file |
| 143 |
else |
| 144 |
nfoFile = URIUtils::ReplaceExtension(item.GetPath(), ".nfo"); |
| 145 |
} |
| 146 |
|
| 147 |
// test file existence |
| 148 |
if (!nfoFile.empty() && !CFileUtils::Exists(nfoFile)) |
| 149 |
nfoFile.clear(); |
| 150 |
|
| 151 |
if (nfoFile.empty()) // final attempt - strip off any cd1 folders |
| 152 |
{ |
| 153 |
URIUtils::RemoveSlashAtEnd(strPath); // need no slash for the check that follows |
| 154 |
CFileItem item2; |
| 155 |
if (StringUtils::EndsWithNoCase(strPath, "cd1")) |
| 156 |
{ |
| 157 |
strPath.erase(strPath.size() - 3); |
| 158 |
item2.SetPath(URIUtils::AddFileToFolder(strPath, URIUtils::GetFileName(item.GetPath()))); |
| 159 |
return FindNFO(item2, movieFolder); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if (nfoFile.empty() && item.IsOpticalMediaFile()) |
| 164 |
{ |
| 165 |
CFileItem parentDirectory(item.GetLocalMetadataPath(), true); |
| 166 |
nfoFile = FindNFO(parentDirectory, true); |
| 167 |
} |
| 168 |
} |
| 169 |
// folders (or stacked dvds) can take any nfo file if there's a unique one |
| 170 |
if (item.m_bIsFolder || item.IsOpticalMediaFile() || (movieFolder && nfoFile.empty())) |
| 171 |
{ |
| 172 |
// see if there is a unique nfo file in this folder, and if so, use that |
| 173 |
CFileItemList items; |
| 174 |
CDirectory dir; |
| 175 |
std::string strPath; |
| 176 |
if (item.m_bIsFolder) |
| 177 |
strPath = item.GetPath(); |
| 178 |
else |
| 179 |
strPath = URIUtils::GetDirectory(item.GetPath()); |
| 180 |
|
| 181 |
if (dir.GetDirectory(strPath, items, ".nfo", DIR_FLAG_DEFAULTS) && items.Size()) |
| 182 |
{ |
| 183 |
int numNFO = -1; |
| 184 |
for (int i = 0; i < items.Size(); i++) |
| 185 |
{ |
| 186 |
if (items[i]->IsNFO()) |
| 187 |
{ |
| 188 |
if (numNFO == -1) |
| 189 |
numNFO = i; |
| 190 |
else |
| 191 |
{ |
| 192 |
numNFO = -1; |
| 193 |
break; |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
if (numNFO > -1) |
| 198 |
return items[numNFO]->GetPath(); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return nfoFile; |
| 203 |
} |
| 204 |
|