BaseNfoParser.cs 33 KB

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