BaseNfoParser.cs 34 KB

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