BaseNfoParser.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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(MetadataProvider.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.AsSpan().Slice(index + srch.Length).TrimEnd('/');
  189. index = tmdbId.IndexOf('-');
  190. if (index != -1)
  191. {
  192. tmdbId = tmdbId.Slice(0, index);
  193. }
  194. if (!tmdbId.IsEmpty
  195. && !tmdbId.IsWhiteSpace()
  196. && int.TryParse(tmdbId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value))
  197. {
  198. item.SetProviderId(MetadataProvider.Tmdb, value.ToString(UsCulture));
  199. }
  200. }
  201. if (item is Series)
  202. {
  203. srch = "thetvdb.com/?tab=series&id=";
  204. index = xml.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  205. if (index != -1)
  206. {
  207. var tvdbId = xml.AsSpan().Slice(index + srch.Length).TrimEnd('/');
  208. if (!tvdbId.IsEmpty
  209. && !tvdbId.IsWhiteSpace()
  210. && int.TryParse(tvdbId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value))
  211. {
  212. item.SetProviderId(MetadataProvider.Tvdb, value.ToString(UsCulture));
  213. }
  214. }
  215. }
  216. }
  217. protected virtual void FetchDataFromXmlNode(XmlReader reader, MetadataResult<T> itemResult)
  218. {
  219. var item = itemResult.Item;
  220. switch (reader.Name)
  221. {
  222. // DateCreated
  223. case "dateadded":
  224. {
  225. var val = reader.ReadElementContentAsString();
  226. if (!string.IsNullOrWhiteSpace(val))
  227. {
  228. if (DateTime.TryParseExact(val, BaseNfoSaver.DateAddedFormat, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var added))
  229. {
  230. item.DateCreated = added.ToUniversalTime();
  231. }
  232. else if (DateTime.TryParse(val, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out added))
  233. {
  234. item.DateCreated = added.ToUniversalTime();
  235. }
  236. else
  237. {
  238. Logger.LogWarning("Invalid Added value found: " + val);
  239. }
  240. }
  241. break;
  242. }
  243. case "originaltitle":
  244. {
  245. var val = reader.ReadElementContentAsString();
  246. if (!string.IsNullOrEmpty(val))
  247. {
  248. item.OriginalTitle = val;
  249. }
  250. break;
  251. }
  252. case "title":
  253. case "localtitle":
  254. item.Name = reader.ReadElementContentAsString();
  255. break;
  256. case "criticrating":
  257. {
  258. var text = reader.ReadElementContentAsString();
  259. if (!string.IsNullOrEmpty(text))
  260. {
  261. if (float.TryParse(text, NumberStyles.Any, UsCulture, out var value))
  262. {
  263. item.CriticRating = value;
  264. }
  265. }
  266. break;
  267. }
  268. case "sorttitle":
  269. {
  270. var val = reader.ReadElementContentAsString();
  271. if (!string.IsNullOrWhiteSpace(val))
  272. {
  273. item.ForcedSortName = val;
  274. }
  275. break;
  276. }
  277. case "biography":
  278. case "plot":
  279. case "review":
  280. {
  281. var val = reader.ReadElementContentAsString();
  282. if (!string.IsNullOrWhiteSpace(val))
  283. {
  284. item.Overview = val;
  285. }
  286. break;
  287. }
  288. case "language":
  289. {
  290. var val = reader.ReadElementContentAsString();
  291. item.PreferredMetadataLanguage = val;
  292. break;
  293. }
  294. case "countrycode":
  295. {
  296. var val = reader.ReadElementContentAsString();
  297. item.PreferredMetadataCountryCode = val;
  298. break;
  299. }
  300. case "lockedfields":
  301. {
  302. var val = reader.ReadElementContentAsString();
  303. if (!string.IsNullOrWhiteSpace(val))
  304. {
  305. item.LockedFields = val.Split('|').Select(i =>
  306. {
  307. if (Enum.TryParse(i, true, out MetadataField field))
  308. {
  309. return (MetadataField?)field;
  310. }
  311. return null;
  312. }).Where(i => i.HasValue).Select(i => i.Value).ToArray();
  313. }
  314. break;
  315. }
  316. case "tagline":
  317. {
  318. var val = reader.ReadElementContentAsString();
  319. if (!string.IsNullOrWhiteSpace(val))
  320. {
  321. item.Tagline = val;
  322. }
  323. break;
  324. }
  325. case "country":
  326. {
  327. var val = reader.ReadElementContentAsString();
  328. if (!string.IsNullOrWhiteSpace(val))
  329. {
  330. item.ProductionLocations = val.Split('/')
  331. .Select(i => i.Trim())
  332. .Where(i => !string.IsNullOrWhiteSpace(i))
  333. .ToArray();
  334. }
  335. break;
  336. }
  337. case "mpaa":
  338. {
  339. var rating = reader.ReadElementContentAsString();
  340. if (!string.IsNullOrWhiteSpace(rating))
  341. {
  342. item.OfficialRating = rating;
  343. }
  344. break;
  345. }
  346. case "customrating":
  347. {
  348. var val = reader.ReadElementContentAsString();
  349. if (!string.IsNullOrWhiteSpace(val))
  350. {
  351. item.CustomRating = val;
  352. }
  353. break;
  354. }
  355. case "runtime":
  356. {
  357. var text = reader.ReadElementContentAsString();
  358. if (!string.IsNullOrWhiteSpace(text))
  359. {
  360. if (int.TryParse(text.Split(' ')[0], NumberStyles.Integer, UsCulture, out var runtime))
  361. {
  362. item.RunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
  363. }
  364. }
  365. break;
  366. }
  367. case "aspectratio":
  368. {
  369. var val = reader.ReadElementContentAsString();
  370. if (!string.IsNullOrWhiteSpace(val)
  371. && item is IHasAspectRatio hasAspectRatio)
  372. {
  373. hasAspectRatio.AspectRatio = val;
  374. }
  375. break;
  376. }
  377. case "lockdata":
  378. {
  379. var val = reader.ReadElementContentAsString();
  380. if (!string.IsNullOrWhiteSpace(val))
  381. {
  382. item.IsLocked = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  383. }
  384. break;
  385. }
  386. case "studio":
  387. {
  388. var val = reader.ReadElementContentAsString();
  389. if (!string.IsNullOrWhiteSpace(val))
  390. {
  391. item.AddStudio(val);
  392. }
  393. break;
  394. }
  395. case "director":
  396. {
  397. var val = reader.ReadElementContentAsString();
  398. foreach (var p in SplitNames(val).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Director }))
  399. {
  400. if (string.IsNullOrWhiteSpace(p.Name))
  401. {
  402. continue;
  403. }
  404. itemResult.AddPerson(p);
  405. }
  406. break;
  407. }
  408. case "credits":
  409. {
  410. var val = reader.ReadElementContentAsString();
  411. if (!string.IsNullOrWhiteSpace(val))
  412. {
  413. var parts = val.Split('/').Select(i => i.Trim())
  414. .Where(i => !string.IsNullOrEmpty(i));
  415. foreach (var p in parts.Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Writer }))
  416. {
  417. if (string.IsNullOrWhiteSpace(p.Name))
  418. {
  419. continue;
  420. }
  421. itemResult.AddPerson(p);
  422. }
  423. }
  424. break;
  425. }
  426. case "writer":
  427. {
  428. var val = reader.ReadElementContentAsString();
  429. foreach (var p in SplitNames(val).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Writer }))
  430. {
  431. if (string.IsNullOrWhiteSpace(p.Name))
  432. {
  433. continue;
  434. }
  435. itemResult.AddPerson(p);
  436. }
  437. break;
  438. }
  439. case "actor":
  440. {
  441. if (!reader.IsEmptyElement)
  442. {
  443. using (var subtree = reader.ReadSubtree())
  444. {
  445. var person = GetPersonFromXmlNode(subtree);
  446. if (!string.IsNullOrWhiteSpace(person.Name))
  447. {
  448. itemResult.AddPerson(person);
  449. }
  450. }
  451. }
  452. else
  453. {
  454. reader.Read();
  455. }
  456. break;
  457. }
  458. case "trailer":
  459. {
  460. var val = reader.ReadElementContentAsString();
  461. if (!string.IsNullOrWhiteSpace(val))
  462. {
  463. val = val.Replace("plugin://plugin.video.youtube/?action=play_video&videoid=", BaseNfoSaver.YouTubeWatchUrl, StringComparison.OrdinalIgnoreCase);
  464. item.AddTrailerUrl(val);
  465. }
  466. break;
  467. }
  468. case "displayorder":
  469. {
  470. var val = reader.ReadElementContentAsString();
  471. var hasDisplayOrder = item as IHasDisplayOrder;
  472. if (hasDisplayOrder != null)
  473. {
  474. if (!string.IsNullOrWhiteSpace(val))
  475. {
  476. hasDisplayOrder.DisplayOrder = val;
  477. }
  478. }
  479. break;
  480. }
  481. case "year":
  482. {
  483. var val = reader.ReadElementContentAsString();
  484. if (!string.IsNullOrWhiteSpace(val))
  485. {
  486. if (int.TryParse(val, out var productionYear) && productionYear > 1850)
  487. {
  488. item.ProductionYear = productionYear;
  489. }
  490. }
  491. break;
  492. }
  493. case "rating":
  494. {
  495. var rating = reader.ReadElementContentAsString();
  496. if (!string.IsNullOrWhiteSpace(rating))
  497. {
  498. // All external meta is saving this as '.' for decimal I believe...but just to be sure
  499. if (float.TryParse(rating.Replace(',', '.'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var val))
  500. {
  501. item.CommunityRating = val;
  502. }
  503. }
  504. break;
  505. }
  506. case "aired":
  507. case "formed":
  508. case "premiered":
  509. case "releasedate":
  510. {
  511. var formatString = _config.GetNfoConfiguration().ReleaseDateFormat;
  512. var val = reader.ReadElementContentAsString();
  513. if (!string.IsNullOrWhiteSpace(val))
  514. {
  515. if (DateTime.TryParseExact(val, formatString, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var date) && date.Year > 1850)
  516. {
  517. item.PremiereDate = date.ToUniversalTime();
  518. item.ProductionYear = date.Year;
  519. }
  520. }
  521. break;
  522. }
  523. case "enddate":
  524. {
  525. var formatString = _config.GetNfoConfiguration().ReleaseDateFormat;
  526. var val = reader.ReadElementContentAsString();
  527. if (!string.IsNullOrWhiteSpace(val))
  528. {
  529. if (DateTime.TryParseExact(val, formatString, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var date) && date.Year > 1850)
  530. {
  531. item.EndDate = date.ToUniversalTime();
  532. }
  533. }
  534. break;
  535. }
  536. case "genre":
  537. {
  538. var val = reader.ReadElementContentAsString();
  539. if (!string.IsNullOrWhiteSpace(val))
  540. {
  541. var parts = val.Split('/')
  542. .Select(i => i.Trim())
  543. .Where(i => !string.IsNullOrWhiteSpace(i));
  544. foreach (var p in parts)
  545. {
  546. item.AddGenre(p);
  547. }
  548. }
  549. break;
  550. }
  551. case "style":
  552. case "tag":
  553. {
  554. var val = reader.ReadElementContentAsString();
  555. if (!string.IsNullOrWhiteSpace(val))
  556. {
  557. item.AddTag(val);
  558. }
  559. break;
  560. }
  561. case "fileinfo":
  562. {
  563. if (!reader.IsEmptyElement)
  564. {
  565. using (var subtree = reader.ReadSubtree())
  566. {
  567. FetchFromFileInfoNode(subtree, item);
  568. }
  569. }
  570. else
  571. {
  572. reader.Read();
  573. }
  574. break;
  575. }
  576. default:
  577. string readerName = reader.Name;
  578. if (_validProviderIds.TryGetValue(readerName, out string providerIdValue))
  579. {
  580. var id = reader.ReadElementContentAsString();
  581. if (!string.IsNullOrWhiteSpace(id))
  582. {
  583. item.SetProviderId(providerIdValue, id);
  584. }
  585. }
  586. else
  587. {
  588. reader.Skip();
  589. }
  590. break;
  591. }
  592. }
  593. private void FetchFromFileInfoNode(XmlReader reader, T item)
  594. {
  595. reader.MoveToContent();
  596. reader.Read();
  597. // Loop through each element
  598. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  599. {
  600. if (reader.NodeType == XmlNodeType.Element)
  601. {
  602. switch (reader.Name)
  603. {
  604. case "streamdetails":
  605. {
  606. if (reader.IsEmptyElement)
  607. {
  608. reader.Read();
  609. continue;
  610. }
  611. using (var subtree = reader.ReadSubtree())
  612. {
  613. FetchFromStreamDetailsNode(subtree, item);
  614. }
  615. break;
  616. }
  617. default:
  618. reader.Skip();
  619. break;
  620. }
  621. }
  622. else
  623. {
  624. reader.Read();
  625. }
  626. }
  627. }
  628. private void FetchFromStreamDetailsNode(XmlReader reader, T item)
  629. {
  630. reader.MoveToContent();
  631. reader.Read();
  632. // Loop through each element
  633. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  634. {
  635. if (reader.NodeType == XmlNodeType.Element)
  636. {
  637. switch (reader.Name)
  638. {
  639. case "video":
  640. {
  641. if (reader.IsEmptyElement)
  642. {
  643. reader.Read();
  644. continue;
  645. }
  646. using (var subtree = reader.ReadSubtree())
  647. {
  648. FetchFromVideoNode(subtree, item);
  649. }
  650. break;
  651. }
  652. default:
  653. reader.Skip();
  654. break;
  655. }
  656. }
  657. else
  658. {
  659. reader.Read();
  660. }
  661. }
  662. }
  663. private void FetchFromVideoNode(XmlReader reader, T item)
  664. {
  665. reader.MoveToContent();
  666. reader.Read();
  667. // Loop through each element
  668. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  669. {
  670. if (reader.NodeType == XmlNodeType.Element)
  671. {
  672. switch (reader.Name)
  673. {
  674. case "format3d":
  675. {
  676. var val = reader.ReadElementContentAsString();
  677. var video = item as Video;
  678. if (video != null)
  679. {
  680. if (string.Equals("HSBS", val, StringComparison.OrdinalIgnoreCase))
  681. {
  682. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  683. }
  684. else if (string.Equals("HTAB", val, StringComparison.OrdinalIgnoreCase))
  685. {
  686. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  687. }
  688. else if (string.Equals("FTAB", val, StringComparison.OrdinalIgnoreCase))
  689. {
  690. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  691. }
  692. else if (string.Equals("FSBS", val, StringComparison.OrdinalIgnoreCase))
  693. {
  694. video.Video3DFormat = Video3DFormat.FullSideBySide;
  695. }
  696. else if (string.Equals("MVC", val, StringComparison.OrdinalIgnoreCase))
  697. {
  698. video.Video3DFormat = Video3DFormat.MVC;
  699. }
  700. }
  701. break;
  702. }
  703. default:
  704. reader.Skip();
  705. break;
  706. }
  707. }
  708. else
  709. {
  710. reader.Read();
  711. }
  712. }
  713. }
  714. /// <summary>
  715. /// Gets the persons from XML node.
  716. /// </summary>
  717. /// <param name="reader">The reader.</param>
  718. /// <returns>IEnumerable{PersonInfo}.</returns>
  719. private PersonInfo GetPersonFromXmlNode(XmlReader reader)
  720. {
  721. var name = string.Empty;
  722. var type = PersonType.Actor; // If type is not specified assume actor
  723. var role = string.Empty;
  724. int? sortOrder = null;
  725. reader.MoveToContent();
  726. reader.Read();
  727. // Loop through each element
  728. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  729. {
  730. if (reader.NodeType == XmlNodeType.Element)
  731. {
  732. switch (reader.Name)
  733. {
  734. case "name":
  735. name = reader.ReadElementContentAsString() ?? string.Empty;
  736. break;
  737. case "role":
  738. {
  739. var val = reader.ReadElementContentAsString();
  740. if (!string.IsNullOrWhiteSpace(val))
  741. {
  742. role = val;
  743. }
  744. break;
  745. }
  746. case "sortorder":
  747. {
  748. var val = reader.ReadElementContentAsString();
  749. if (!string.IsNullOrWhiteSpace(val))
  750. {
  751. if (int.TryParse(val, NumberStyles.Integer, UsCulture, out var intVal))
  752. {
  753. sortOrder = intVal;
  754. }
  755. }
  756. break;
  757. }
  758. default:
  759. reader.Skip();
  760. break;
  761. }
  762. }
  763. else
  764. {
  765. reader.Read();
  766. }
  767. }
  768. return new PersonInfo
  769. {
  770. Name = name.Trim(),
  771. Role = role,
  772. Type = type,
  773. SortOrder = sortOrder
  774. };
  775. }
  776. internal XmlReaderSettings GetXmlReaderSettings()
  777. => new XmlReaderSettings()
  778. {
  779. ValidationType = ValidationType.None,
  780. CheckCharacters = false,
  781. IgnoreProcessingInstructions = true,
  782. IgnoreComments = true
  783. };
  784. /// <summary>
  785. /// Used to split names of comma or pipe delimeted genres and people.
  786. /// </summary>
  787. /// <param name="value">The value.</param>
  788. /// <returns>IEnumerable{System.String}.</returns>
  789. private IEnumerable<string> SplitNames(string value)
  790. {
  791. value = value ?? string.Empty;
  792. // Only split by comma if there is no pipe in the string
  793. // We have to be careful to not split names like Matthew, Jr.
  794. var separator = value.IndexOf('|', StringComparison.Ordinal) == -1 && value.IndexOf(';', StringComparison.Ordinal) == -1
  795. ? new[] { ',' }
  796. : new[] { '|', ';' };
  797. value = value.Trim().Trim(separator);
  798. return string.IsNullOrWhiteSpace(value) ? Array.Empty<string>() : value.Split(separator, StringSplitOptions.RemoveEmptyEntries);
  799. }
  800. }
  801. }