BaseNfoParser.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.Extensions;
  6. using Microsoft.Extensions.Logging;
  7. using MediaBrowser.XbmcMetadata.Configuration;
  8. using MediaBrowser.XbmcMetadata.Savers;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. using System.Threading;
  17. using System.Xml;
  18. using MediaBrowser.Controller.Entities.TV;
  19. using MediaBrowser.Model.IO;
  20. using MediaBrowser.Model.Xml;
  21. namespace MediaBrowser.XbmcMetadata.Parsers
  22. {
  23. public class BaseNfoParser<T>
  24. where T : BaseItem
  25. {
  26. /// <summary>
  27. /// The logger
  28. /// </summary>
  29. protected ILogger Logger { get; private set; }
  30. protected IFileSystem FileSystem { get; private set; }
  31. protected IProviderManager ProviderManager { get; private set; }
  32. protected IXmlReaderSettingsFactory XmlReaderSettingsFactory { get; private set; }
  33. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  34. private readonly IConfigurationManager _config;
  35. private Dictionary<string, string> _validProviderIds;
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="BaseNfoParser{T}" /> class.
  38. /// </summary>
  39. public BaseNfoParser(ILogger logger, IConfigurationManager config, IProviderManager providerManager, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlReaderSettingsFactory)
  40. {
  41. Logger = logger;
  42. _config = config;
  43. ProviderManager = providerManager;
  44. FileSystem = fileSystem;
  45. XmlReaderSettingsFactory = xmlReaderSettingsFactory;
  46. }
  47. /// <summary>
  48. /// Fetches metadata for an item from one xml file
  49. /// </summary>
  50. /// <param name="item">The item.</param>
  51. /// <param name="metadataFile">The metadata file.</param>
  52. /// <param name="cancellationToken">The cancellation token.</param>
  53. /// <exception cref="System.ArgumentNullException">
  54. /// </exception>
  55. public void Fetch(MetadataResult<T> item, string metadataFile, CancellationToken cancellationToken)
  56. {
  57. if (item == null)
  58. {
  59. throw new ArgumentNullException(nameof(item));
  60. }
  61. if (string.IsNullOrEmpty(metadataFile))
  62. {
  63. throw new ArgumentException("The metadata file was empty or null.", nameof(metadataFile));
  64. }
  65. var settings = XmlReaderSettingsFactory.Create(false);
  66. settings.CheckCharacters = false;
  67. settings.IgnoreProcessingInstructions = true;
  68. settings.IgnoreComments = true;
  69. _validProviderIds = _validProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  70. var idInfos = ProviderManager.GetExternalIdInfos(item.Item);
  71. foreach (var info in idInfos)
  72. {
  73. var id = info.Key + "Id";
  74. if (!_validProviderIds.ContainsKey(id))
  75. {
  76. _validProviderIds.Add(id, info.Key);
  77. }
  78. }
  79. //Additional Mappings
  80. _validProviderIds.Add("collectionnumber", "TmdbCollection");
  81. _validProviderIds.Add("tmdbcolid", "TmdbCollection");
  82. _validProviderIds.Add("imdb_id", "Imdb");
  83. Fetch(item, metadataFile, settings, cancellationToken);
  84. }
  85. protected virtual bool SupportsUrlAfterClosingXmlTag
  86. {
  87. get { return false; }
  88. }
  89. /// <summary>
  90. /// Fetches the specified item.
  91. /// </summary>
  92. /// <param name="item">The item.</param>
  93. /// <param name="metadataFile">The metadata file.</param>
  94. /// <param name="settings">The settings.</param>
  95. /// <param name="cancellationToken">The cancellation token.</param>
  96. protected virtual void Fetch(MetadataResult<T> item, string metadataFile, XmlReaderSettings settings, CancellationToken cancellationToken)
  97. {
  98. if (!SupportsUrlAfterClosingXmlTag)
  99. {
  100. using (var fileStream = FileSystem.OpenRead(metadataFile))
  101. {
  102. using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
  103. {
  104. // Use XmlReader for best performance
  105. using (var reader = XmlReader.Create(streamReader, settings))
  106. {
  107. item.ResetPeople();
  108. reader.MoveToContent();
  109. reader.Read();
  110. // Loop through each element
  111. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  112. {
  113. cancellationToken.ThrowIfCancellationRequested();
  114. if (reader.NodeType == XmlNodeType.Element)
  115. {
  116. FetchDataFromXmlNode(reader, item);
  117. }
  118. else
  119. {
  120. reader.Read();
  121. }
  122. }
  123. }
  124. }
  125. }
  126. return;
  127. }
  128. using (var fileStream = FileSystem.OpenRead(metadataFile))
  129. {
  130. using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
  131. {
  132. item.ResetPeople();
  133. // Need to handle a url after the xml data
  134. // http://kodi.wiki/view/NFO_files/movies
  135. var xml = streamReader.ReadToEnd();
  136. // Find last closing Tag
  137. // Need to do this in two steps to account for random > characters after the closing xml
  138. var index = xml.LastIndexOf(@"</", StringComparison.Ordinal);
  139. // If closing tag exists, move to end of Tag
  140. if (index != -1)
  141. {
  142. index = xml.IndexOf('>', index);
  143. }
  144. if (index != -1)
  145. {
  146. var endingXml = xml.Substring(index);
  147. ParseProviderLinks(item.Item, endingXml);
  148. // If the file is just an imdb url, don't go any further
  149. if (index == 0)
  150. {
  151. return;
  152. }
  153. xml = xml.Substring(0, index + 1);
  154. }
  155. else
  156. {
  157. // If the file is just an Imdb url, handle that
  158. ParseProviderLinks(item.Item, xml);
  159. return;
  160. }
  161. // These are not going to be valid xml so no sense in causing the provider to fail and spamming the log with exceptions
  162. try
  163. {
  164. using (var stringReader = new StringReader(xml))
  165. {
  166. // Use XmlReader for best performance
  167. using (var reader = XmlReader.Create(stringReader, settings))
  168. {
  169. reader.MoveToContent();
  170. reader.Read();
  171. // Loop through each element
  172. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  173. {
  174. cancellationToken.ThrowIfCancellationRequested();
  175. if (reader.NodeType == XmlNodeType.Element)
  176. {
  177. FetchDataFromXmlNode(reader, item);
  178. }
  179. else
  180. {
  181. reader.Read();
  182. }
  183. }
  184. }
  185. }
  186. }
  187. catch (XmlException)
  188. {
  189. }
  190. }
  191. }
  192. }
  193. protected virtual string MovieDbParserSearchString
  194. {
  195. get { return "themoviedb.org/movie/"; }
  196. }
  197. protected void ParseProviderLinks(T item, string xml)
  198. {
  199. //Look for a match for the Regex pattern "tt" followed by 7 digits
  200. Match m = Regex.Match(xml, @"tt([0-9]{7})", RegexOptions.IgnoreCase);
  201. if (m.Success)
  202. {
  203. item.SetProviderId(MetadataProviders.Imdb, m.Value);
  204. }
  205. // Support Tmdb
  206. // https://www.themoviedb.org/movie/30287-fallo
  207. var srch = MovieDbParserSearchString;
  208. var index = xml.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  209. if (index != -1)
  210. {
  211. var tmdbId = xml.Substring(index + srch.Length).TrimEnd('/').Split('-')[0];
  212. int value;
  213. if (!string.IsNullOrWhiteSpace(tmdbId) && int.TryParse(tmdbId, NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
  214. {
  215. item.SetProviderId(MetadataProviders.Tmdb, value.ToString(_usCulture));
  216. }
  217. }
  218. if (item is Series)
  219. {
  220. srch = "thetvdb.com/?tab=series&id=";
  221. index = xml.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  222. if (index != -1)
  223. {
  224. var tvdbId = xml.Substring(index + srch.Length).TrimEnd('/');
  225. int value;
  226. if (!string.IsNullOrWhiteSpace(tvdbId) && int.TryParse(tvdbId, NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
  227. {
  228. item.SetProviderId(MetadataProviders.Tvdb, value.ToString(_usCulture));
  229. }
  230. }
  231. }
  232. }
  233. protected virtual void FetchDataFromXmlNode(XmlReader reader, MetadataResult<T> itemResult)
  234. {
  235. var item = itemResult.Item;
  236. switch (reader.Name)
  237. {
  238. // DateCreated
  239. case "dateadded":
  240. {
  241. var val = reader.ReadElementContentAsString();
  242. if (!string.IsNullOrWhiteSpace(val))
  243. {
  244. DateTime added;
  245. if (DateTime.TryParseExact(val, BaseNfoSaver.DateAddedFormat, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out added))
  246. {
  247. item.DateCreated = added.ToUniversalTime();
  248. }
  249. else if (DateTime.TryParse(val, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out added))
  250. {
  251. item.DateCreated = added.ToUniversalTime();
  252. }
  253. else
  254. {
  255. Logger.LogWarning("Invalid Added value found: " + val);
  256. }
  257. }
  258. break;
  259. }
  260. case "originaltitle":
  261. {
  262. var val = reader.ReadElementContentAsString();
  263. if (!string.IsNullOrEmpty(val))
  264. {
  265. item.OriginalTitle = val;
  266. }
  267. break;
  268. }
  269. case "title":
  270. case "localtitle":
  271. item.Name = reader.ReadElementContentAsString();
  272. break;
  273. case "criticrating":
  274. {
  275. var text = reader.ReadElementContentAsString();
  276. if (!string.IsNullOrEmpty(text))
  277. {
  278. float value;
  279. if (float.TryParse(text, NumberStyles.Any, _usCulture, out value))
  280. {
  281. item.CriticRating = value;
  282. }
  283. }
  284. break;
  285. }
  286. case "sorttitle":
  287. {
  288. var val = reader.ReadElementContentAsString();
  289. if (!string.IsNullOrWhiteSpace(val))
  290. {
  291. item.ForcedSortName = val;
  292. }
  293. break;
  294. }
  295. case "biography":
  296. case "plot":
  297. case "review":
  298. {
  299. var val = reader.ReadElementContentAsString();
  300. if (!string.IsNullOrWhiteSpace(val))
  301. {
  302. item.Overview = val;
  303. }
  304. break;
  305. }
  306. case "language":
  307. {
  308. var val = reader.ReadElementContentAsString();
  309. item.PreferredMetadataLanguage = val;
  310. break;
  311. }
  312. case "countrycode":
  313. {
  314. var val = reader.ReadElementContentAsString();
  315. item.PreferredMetadataCountryCode = val;
  316. break;
  317. }
  318. case "lockedfields":
  319. {
  320. var val = reader.ReadElementContentAsString();
  321. if (!string.IsNullOrWhiteSpace(val))
  322. {
  323. item.LockedFields = val.Split('|').Select(i =>
  324. {
  325. MetadataFields field;
  326. if (Enum.TryParse<MetadataFields>(i, true, out field))
  327. {
  328. return (MetadataFields?)field;
  329. }
  330. return null;
  331. }).Where(i => i.HasValue).Select(i => i.Value).ToArray();
  332. }
  333. break;
  334. }
  335. case "tagline":
  336. {
  337. var val = reader.ReadElementContentAsString();
  338. if (!string.IsNullOrWhiteSpace(val))
  339. {
  340. item.Tagline = val;
  341. }
  342. break;
  343. }
  344. case "country":
  345. {
  346. var val = reader.ReadElementContentAsString();
  347. if (!string.IsNullOrWhiteSpace(val))
  348. {
  349. item.ProductionLocations = val.Split('/')
  350. .Select(i => i.Trim())
  351. .Where(i => !string.IsNullOrWhiteSpace(i))
  352. .ToArray();
  353. }
  354. break;
  355. }
  356. case "mpaa":
  357. {
  358. var rating = reader.ReadElementContentAsString();
  359. if (!string.IsNullOrWhiteSpace(rating))
  360. {
  361. item.OfficialRating = rating;
  362. }
  363. break;
  364. }
  365. case "customrating":
  366. {
  367. var val = reader.ReadElementContentAsString();
  368. if (!string.IsNullOrWhiteSpace(val))
  369. {
  370. item.CustomRating = val;
  371. }
  372. break;
  373. }
  374. case "runtime":
  375. {
  376. var text = reader.ReadElementContentAsString();
  377. if (!string.IsNullOrWhiteSpace(text))
  378. {
  379. int runtime;
  380. if (int.TryParse(text.Split(' ')[0], NumberStyles.Integer, _usCulture, out runtime))
  381. {
  382. item.RunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
  383. }
  384. }
  385. break;
  386. }
  387. case "aspectratio":
  388. {
  389. var val = reader.ReadElementContentAsString();
  390. var hasAspectRatio = item as IHasAspectRatio;
  391. if (!string.IsNullOrWhiteSpace(val) && hasAspectRatio != null)
  392. {
  393. hasAspectRatio.AspectRatio = val;
  394. }
  395. break;
  396. }
  397. case "lockdata":
  398. {
  399. var val = reader.ReadElementContentAsString();
  400. if (!string.IsNullOrWhiteSpace(val))
  401. {
  402. item.IsLocked = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  403. }
  404. break;
  405. }
  406. case "studio":
  407. {
  408. var val = reader.ReadElementContentAsString();
  409. if (!string.IsNullOrWhiteSpace(val))
  410. {
  411. //var parts = val.Split('/')
  412. // .Select(i => i.Trim())
  413. // .Where(i => !string.IsNullOrWhiteSpace(i));
  414. //foreach (var p in parts)
  415. //{
  416. // item.AddStudio(p);
  417. //}
  418. item.AddStudio(val);
  419. }
  420. break;
  421. }
  422. case "director":
  423. {
  424. var val = reader.ReadElementContentAsString();
  425. foreach (var p in SplitNames(val).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Director }))
  426. {
  427. if (string.IsNullOrWhiteSpace(p.Name))
  428. {
  429. continue;
  430. }
  431. itemResult.AddPerson(p);
  432. }
  433. break;
  434. }
  435. case "credits":
  436. {
  437. var val = reader.ReadElementContentAsString();
  438. if (!string.IsNullOrWhiteSpace(val))
  439. {
  440. var parts = val.Split('/').Select(i => i.Trim())
  441. .Where(i => !string.IsNullOrEmpty(i));
  442. foreach (var p in parts.Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Writer }))
  443. {
  444. if (string.IsNullOrWhiteSpace(p.Name))
  445. {
  446. continue;
  447. }
  448. itemResult.AddPerson(p);
  449. }
  450. }
  451. break;
  452. }
  453. case "writer":
  454. {
  455. var val = reader.ReadElementContentAsString();
  456. foreach (var p in SplitNames(val).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Writer }))
  457. {
  458. if (string.IsNullOrWhiteSpace(p.Name))
  459. {
  460. continue;
  461. }
  462. itemResult.AddPerson(p);
  463. }
  464. break;
  465. }
  466. case "actor":
  467. {
  468. if (!reader.IsEmptyElement)
  469. {
  470. using (var subtree = reader.ReadSubtree())
  471. {
  472. var person = GetPersonFromXmlNode(subtree);
  473. if (!string.IsNullOrWhiteSpace(person.Name))
  474. {
  475. itemResult.AddPerson(person);
  476. }
  477. }
  478. }
  479. else
  480. {
  481. reader.Read();
  482. }
  483. break;
  484. }
  485. case "trailer":
  486. {
  487. var val = reader.ReadElementContentAsString();
  488. if (!string.IsNullOrWhiteSpace(val))
  489. {
  490. val = val.Replace("plugin://plugin.video.youtube/?action=play_video&videoid=", BaseNfoSaver.YouTubeWatchUrl, StringComparison.OrdinalIgnoreCase);
  491. item.AddTrailerUrl(val);
  492. }
  493. break;
  494. }
  495. case "displayorder":
  496. {
  497. var val = reader.ReadElementContentAsString();
  498. var hasDisplayOrder = item as IHasDisplayOrder;
  499. if (hasDisplayOrder != null)
  500. {
  501. if (!string.IsNullOrWhiteSpace(val))
  502. {
  503. hasDisplayOrder.DisplayOrder = val;
  504. }
  505. }
  506. break;
  507. }
  508. case "year":
  509. {
  510. var val = reader.ReadElementContentAsString();
  511. if (!string.IsNullOrWhiteSpace(val))
  512. {
  513. int productionYear;
  514. if (int.TryParse(val, out productionYear) && productionYear > 1850)
  515. {
  516. item.ProductionYear = productionYear;
  517. }
  518. }
  519. break;
  520. }
  521. case "rating":
  522. {
  523. var rating = reader.ReadElementContentAsString();
  524. if (!string.IsNullOrWhiteSpace(rating))
  525. {
  526. float val;
  527. // All external meta is saving this as '.' for decimal I believe...but just to be sure
  528. if (float.TryParse(rating.Replace(',', '.'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out val))
  529. {
  530. item.CommunityRating = val;
  531. }
  532. }
  533. break;
  534. }
  535. case "aired":
  536. case "formed":
  537. case "premiered":
  538. case "releasedate":
  539. {
  540. var formatString = _config.GetNfoConfiguration().ReleaseDateFormat;
  541. var val = reader.ReadElementContentAsString();
  542. if (!string.IsNullOrWhiteSpace(val))
  543. {
  544. DateTime date;
  545. if (DateTime.TryParseExact(val, formatString, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out date) && date.Year > 1850)
  546. {
  547. item.PremiereDate = date.ToUniversalTime();
  548. item.ProductionYear = date.Year;
  549. }
  550. }
  551. break;
  552. }
  553. case "enddate":
  554. {
  555. var formatString = _config.GetNfoConfiguration().ReleaseDateFormat;
  556. var val = reader.ReadElementContentAsString();
  557. if (!string.IsNullOrWhiteSpace(val))
  558. {
  559. DateTime date;
  560. if (DateTime.TryParseExact(val, formatString, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out date) && date.Year > 1850)
  561. {
  562. item.EndDate = date.ToUniversalTime();
  563. }
  564. }
  565. break;
  566. }
  567. case "genre":
  568. {
  569. var val = reader.ReadElementContentAsString();
  570. if (!string.IsNullOrWhiteSpace(val))
  571. {
  572. var parts = val.Split('/')
  573. .Select(i => i.Trim())
  574. .Where(i => !string.IsNullOrWhiteSpace(i));
  575. foreach (var p in parts)
  576. {
  577. item.AddGenre(p);
  578. }
  579. }
  580. break;
  581. }
  582. case "style":
  583. case "tag":
  584. {
  585. var val = reader.ReadElementContentAsString();
  586. if (!string.IsNullOrWhiteSpace(val))
  587. {
  588. item.AddTag(val);
  589. }
  590. break;
  591. }
  592. case "fileinfo":
  593. {
  594. if (!reader.IsEmptyElement)
  595. {
  596. using (var subtree = reader.ReadSubtree())
  597. {
  598. FetchFromFileInfoNode(subtree, item);
  599. }
  600. }
  601. else
  602. {
  603. reader.Read();
  604. }
  605. break;
  606. }
  607. default:
  608. string readerName = reader.Name;
  609. string providerIdValue;
  610. if (_validProviderIds.TryGetValue(readerName, out providerIdValue))
  611. {
  612. var id = reader.ReadElementContentAsString();
  613. if (!string.IsNullOrWhiteSpace(id))
  614. {
  615. item.SetProviderId(providerIdValue, id);
  616. }
  617. }
  618. else
  619. {
  620. reader.Skip();
  621. }
  622. break;
  623. }
  624. }
  625. private void FetchFromFileInfoNode(XmlReader reader, T item)
  626. {
  627. reader.MoveToContent();
  628. reader.Read();
  629. // Loop through each element
  630. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  631. {
  632. if (reader.NodeType == XmlNodeType.Element)
  633. {
  634. switch (reader.Name)
  635. {
  636. case "streamdetails":
  637. {
  638. if (reader.IsEmptyElement)
  639. {
  640. reader.Read();
  641. continue;
  642. }
  643. using (var subtree = reader.ReadSubtree())
  644. {
  645. FetchFromStreamDetailsNode(subtree, item);
  646. }
  647. break;
  648. }
  649. default:
  650. reader.Skip();
  651. break;
  652. }
  653. }
  654. else
  655. {
  656. reader.Read();
  657. }
  658. }
  659. }
  660. private void FetchFromStreamDetailsNode(XmlReader reader, T item)
  661. {
  662. reader.MoveToContent();
  663. reader.Read();
  664. // Loop through each element
  665. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  666. {
  667. if (reader.NodeType == XmlNodeType.Element)
  668. {
  669. switch (reader.Name)
  670. {
  671. case "video":
  672. {
  673. if (reader.IsEmptyElement)
  674. {
  675. reader.Read();
  676. continue;
  677. }
  678. using (var subtree = reader.ReadSubtree())
  679. {
  680. FetchFromVideoNode(subtree, item);
  681. }
  682. break;
  683. }
  684. default:
  685. reader.Skip();
  686. break;
  687. }
  688. }
  689. else
  690. {
  691. reader.Read();
  692. }
  693. }
  694. }
  695. private void FetchFromVideoNode(XmlReader reader, T item)
  696. {
  697. reader.MoveToContent();
  698. reader.Read();
  699. // Loop through each element
  700. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  701. {
  702. if (reader.NodeType == XmlNodeType.Element)
  703. {
  704. switch (reader.Name)
  705. {
  706. case "format3d":
  707. {
  708. var val = reader.ReadElementContentAsString();
  709. var video = item as Video;
  710. if (video != null)
  711. {
  712. if (string.Equals("HSBS", val, StringComparison.OrdinalIgnoreCase))
  713. {
  714. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  715. }
  716. else if (string.Equals("HTAB", val, StringComparison.OrdinalIgnoreCase))
  717. {
  718. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  719. }
  720. else if (string.Equals("FTAB", val, StringComparison.OrdinalIgnoreCase))
  721. {
  722. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  723. }
  724. else if (string.Equals("FSBS", val, StringComparison.OrdinalIgnoreCase))
  725. {
  726. video.Video3DFormat = Video3DFormat.FullSideBySide;
  727. }
  728. else if (string.Equals("MVC", val, StringComparison.OrdinalIgnoreCase))
  729. {
  730. video.Video3DFormat = Video3DFormat.MVC;
  731. }
  732. }
  733. break;
  734. }
  735. default:
  736. reader.Skip();
  737. break;
  738. }
  739. }
  740. else
  741. {
  742. reader.Read();
  743. }
  744. }
  745. }
  746. /// <summary>
  747. /// Gets the persons from XML node.
  748. /// </summary>
  749. /// <param name="reader">The reader.</param>
  750. /// <returns>IEnumerable{PersonInfo}.</returns>
  751. private PersonInfo GetPersonFromXmlNode(XmlReader reader)
  752. {
  753. var name = string.Empty;
  754. var type = PersonType.Actor; // If type is not specified assume actor
  755. var role = string.Empty;
  756. int? sortOrder = null;
  757. reader.MoveToContent();
  758. reader.Read();
  759. // Loop through each element
  760. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  761. {
  762. if (reader.NodeType == XmlNodeType.Element)
  763. {
  764. switch (reader.Name)
  765. {
  766. case "name":
  767. name = reader.ReadElementContentAsString() ?? string.Empty;
  768. break;
  769. case "role":
  770. {
  771. var val = reader.ReadElementContentAsString();
  772. if (!string.IsNullOrWhiteSpace(val))
  773. {
  774. role = val;
  775. }
  776. break;
  777. }
  778. case "sortorder":
  779. {
  780. var val = reader.ReadElementContentAsString();
  781. if (!string.IsNullOrWhiteSpace(val))
  782. {
  783. int intVal;
  784. if (int.TryParse(val, NumberStyles.Integer, _usCulture, out intVal))
  785. {
  786. sortOrder = intVal;
  787. }
  788. }
  789. break;
  790. }
  791. default:
  792. reader.Skip();
  793. break;
  794. }
  795. }
  796. else
  797. {
  798. reader.Read();
  799. }
  800. }
  801. return new PersonInfo
  802. {
  803. Name = name.Trim(),
  804. Role = role,
  805. Type = type,
  806. SortOrder = sortOrder
  807. };
  808. }
  809. /// <summary>
  810. /// Used to split names of comma or pipe delimeted genres and people
  811. /// </summary>
  812. /// <param name="value">The value.</param>
  813. /// <returns>IEnumerable{System.String}.</returns>
  814. private IEnumerable<string> SplitNames(string value)
  815. {
  816. value = value ?? string.Empty;
  817. // Only split by comma if there is no pipe in the string
  818. // We have to be careful to not split names like Matthew, Jr.
  819. var separator = value.IndexOf('|') == -1 && value.IndexOf(';') == -1 ? new[] { ',' } : new[] { '|', ';' };
  820. value = value.Trim().Trim(separator);
  821. return string.IsNullOrWhiteSpace(value) ? Array.Empty<string>() : Split(value, separator, StringSplitOptions.RemoveEmptyEntries);
  822. }
  823. /// <summary>
  824. /// Provides an additional overload for string.split
  825. /// </summary>
  826. /// <param name="val">The val.</param>
  827. /// <param name="separators">The separators.</param>
  828. /// <param name="options">The options.</param>
  829. /// <returns>System.String[][].</returns>
  830. private string[] Split(string val, char[] separators, StringSplitOptions options)
  831. {
  832. return val.Split(separators, options);
  833. }
  834. }
  835. }