[ExecString] combine SplitParameters with identical function of CUtil
[xbmc.git] / xbmc / NfoFile.cpp
blob2a11da2c171b9c97ebaa8f549c9a29959d64e1d1
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8 // NfoFile.cpp: implementation of the CNfoFile class.
9 //
10 //////////////////////////////////////////////////////////////////////
12 #include "NfoFile.h"
14 #include "FileItem.h"
15 #include "ServiceBroker.h"
16 #include "addons/AddonManager.h"
17 #include "addons/AddonSystemSettings.h"
18 #include "addons/addoninfo/AddonType.h"
19 #include "filesystem/File.h"
20 #include "music/Album.h"
21 #include "music/Artist.h"
22 #include "video/VideoInfoDownloader.h"
24 #include <string>
25 #include <vector>
27 using namespace XFILE;
28 using namespace ADDON;
30 CInfoScanner::INFO_TYPE CNfoFile::Create(const std::string& strPath,
31 const ScraperPtr& info, int episode)
33 m_info = info; // assume we can use these settings
34 m_type = ScraperTypeFromContent(info->Content());
35 if (Load(strPath) != 0)
36 return CInfoScanner::NO_NFO;
38 CFileItemList items;
39 bool bNfo=false;
41 if (m_type == AddonType::SCRAPER_ALBUMS)
43 CAlbum album;
44 bNfo = GetDetails(album);
46 else if (m_type == AddonType::SCRAPER_ARTISTS)
48 CArtist artist;
49 bNfo = GetDetails(artist);
51 else if (m_type == AddonType::SCRAPER_TVSHOWS || m_type == AddonType::SCRAPER_MOVIES ||
52 m_type == AddonType::SCRAPER_MUSICVIDEOS)
54 // first check if it's an XML file with the info we need
55 CVideoInfoTag details;
56 bNfo = GetDetails(details);
57 if (episode > -1 && bNfo && m_type == AddonType::SCRAPER_TVSHOWS)
59 int infos=0;
60 while (m_headPos != std::string::npos && details.m_iEpisode != episode)
62 m_headPos = m_doc.find("<episodedetails", m_headPos);
63 if (m_headPos == std::string::npos)
64 break;
66 bNfo = GetDetails(details);
67 infos++;
68 m_headPos++;
70 if (details.m_iEpisode != episode)
72 bNfo = false;
73 details.Reset();
74 m_headPos = 0;
75 if (infos == 1) // still allow differing nfo/file numbers for single ep nfo's
76 bNfo = GetDetails(details);
81 std::vector<ScraperPtr> vecScrapers = GetScrapers(m_type, m_info);
83 // search ..
84 int res = -1;
85 for (unsigned int i=0; i<vecScrapers.size(); ++i)
86 if ((res = Scrape(vecScrapers[i], m_scurl, m_doc)) == 0 || res == 2)
87 break;
89 if (res == 2)
90 return CInfoScanner::ERROR_NFO;
91 if (bNfo)
93 if (!m_scurl.HasUrls())
95 if (m_doc.find("[scrape url]") != std::string::npos)
96 return CInfoScanner::OVERRIDE_NFO;
97 else
98 return CInfoScanner::FULL_NFO;
100 else
101 return CInfoScanner::COMBINED_NFO;
103 return m_scurl.HasUrls() ? CInfoScanner::URL_NFO : CInfoScanner::NO_NFO;
106 // return value: 0 - success; 1 - no result; skip; 2 - error
107 int CNfoFile::Scrape(ScraperPtr& scraper, CScraperUrl& url,
108 const std::string& content)
110 if (scraper->IsNoop())
112 url = CScraperUrl();
113 return 0;
116 scraper->ClearCache();
120 url = scraper->NfoUrl(content);
122 catch (const CScraperError &sce)
124 CVideoInfoDownloader::ShowErrorDialog(sce);
125 if (!sce.FAborted())
126 return 2;
129 return url.HasUrls() ? 0 : 1;
132 int CNfoFile::Load(const std::string& strFile)
134 Close();
135 XFILE::CFile file;
136 std::vector<uint8_t> buf;
137 if (file.LoadFile(strFile, buf) > 0)
139 m_doc.assign(reinterpret_cast<char*>(buf.data()), buf.size());
140 m_headPos = 0;
141 return 0;
143 m_doc.clear();
144 return 1;
147 void CNfoFile::Close()
149 m_doc.clear();
150 m_headPos = 0;
151 m_scurl.Clear();
154 std::vector<ScraperPtr> CNfoFile::GetScrapers(AddonType type, const ScraperPtr& selectedScraper)
156 AddonPtr addon;
157 ScraperPtr defaultScraper;
158 if (CAddonSystemSettings::GetInstance().GetActive(type, addon))
159 defaultScraper = std::dynamic_pointer_cast<CScraper>(addon);
161 std::vector<ScraperPtr> vecScrapers;
163 // add selected scraper - first priority
164 if (selectedScraper)
165 vecScrapers.push_back(selectedScraper);
167 // Add all scrapers except selected and default
168 VECADDONS addons;
169 CServiceBroker::GetAddonMgr().GetAddons(addons, type);
171 for (auto& addon : addons)
173 ScraperPtr scraper = std::dynamic_pointer_cast<CScraper>(addon);
175 // skip if scraper requires settings and there's nothing set yet
176 if (scraper->RequiresSettings() && !scraper->HasUserSettings())
177 continue;
179 if ((!selectedScraper || selectedScraper->ID() != scraper->ID()) &&
180 (!defaultScraper || defaultScraper->ID() != scraper->ID()))
181 vecScrapers.push_back(scraper);
184 // add default scraper - not user selectable so it's last priority
185 if (defaultScraper && (!selectedScraper ||
186 selectedScraper->ID() != defaultScraper->ID()) &&
187 (!defaultScraper->RequiresSettings() || defaultScraper->HasUserSettings()))
188 vecScrapers.push_back(defaultScraper);
190 return vecScrapers;