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.
8 // NfoFile.cpp: implementation of the CNfoFile class.
10 //////////////////////////////////////////////////////////////////////
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"
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
;
41 if (m_type
== AddonType::SCRAPER_ALBUMS
)
44 bNfo
= GetDetails(album
);
46 else if (m_type
== AddonType::SCRAPER_ARTISTS
)
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
)
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
)
66 bNfo
= GetDetails(details
);
70 if (details
.m_iEpisode
!= episode
)
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
);
85 for (unsigned int i
=0; i
<vecScrapers
.size(); ++i
)
86 if ((res
= Scrape(vecScrapers
[i
], m_scurl
, m_doc
)) == 0 || res
== 2)
90 return CInfoScanner::ERROR_NFO
;
93 if (!m_scurl
.HasUrls())
95 if (m_doc
.find("[scrape url]") != std::string::npos
)
96 return CInfoScanner::OVERRIDE_NFO
;
98 return CInfoScanner::FULL_NFO
;
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())
116 scraper
->ClearCache();
120 url
= scraper
->NfoUrl(content
);
122 catch (const CScraperError
&sce
)
124 CVideoInfoDownloader::ShowErrorDialog(sce
);
129 return url
.HasUrls() ? 0 : 1;
132 int CNfoFile::Load(const std::string
& strFile
)
136 std::vector
<uint8_t> buf
;
137 if (file
.LoadFile(strFile
, buf
) > 0)
139 m_doc
.assign(reinterpret_cast<char*>(buf
.data()), buf
.size());
147 void CNfoFile::Close()
154 std::vector
<ScraperPtr
> CNfoFile::GetScrapers(AddonType type
, const ScraperPtr
& selectedScraper
)
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
165 vecScrapers
.push_back(selectedScraper
);
167 // Add all scrapers except selected and default
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())
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
);