BaseNfoParser.cs 36 KB

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