BaseNfoParser.cs 36 KB

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