BaseItemXmlParser.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.Audio;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Logging;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Xml;
  13. namespace MediaBrowser.Controller.Providers
  14. {
  15. /// <summary>
  16. /// Provides a base class for parsing metadata xml
  17. /// </summary>
  18. /// <typeparam name="T"></typeparam>
  19. public class BaseItemXmlParser<T>
  20. where T : BaseItem, new()
  21. {
  22. /// <summary>
  23. /// The logger
  24. /// </summary>
  25. protected ILogger Logger { get; private set; }
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="BaseItemXmlParser{T}" /> class.
  28. /// </summary>
  29. /// <param name="logger">The logger.</param>
  30. public BaseItemXmlParser(ILogger logger)
  31. {
  32. Logger = logger;
  33. }
  34. /// <summary>
  35. /// Fetches metadata for an item from one xml file
  36. /// </summary>
  37. /// <param name="item">The item.</param>
  38. /// <param name="metadataFile">The metadata file.</param>
  39. /// <param name="cancellationToken">The cancellation token.</param>
  40. /// <exception cref="System.ArgumentNullException"></exception>
  41. public void Fetch(T item, string metadataFile, CancellationToken cancellationToken)
  42. {
  43. if (item == null)
  44. {
  45. throw new ArgumentNullException();
  46. }
  47. if (string.IsNullOrEmpty(metadataFile))
  48. {
  49. throw new ArgumentNullException();
  50. }
  51. var settings = new XmlReaderSettings
  52. {
  53. CheckCharacters = false,
  54. IgnoreProcessingInstructions = true,
  55. IgnoreComments = true,
  56. ValidationType = ValidationType.None
  57. };
  58. item.Taglines.Clear();
  59. item.Studios.Clear();
  60. item.Genres.Clear();
  61. item.People.Clear();
  62. item.Tags.Clear();
  63. //Fetch(item, metadataFile, settings, Encoding.GetEncoding("ISO-8859-1"), cancellationToken);
  64. Fetch(item, metadataFile, settings, Encoding.UTF8, cancellationToken);
  65. }
  66. /// <summary>
  67. /// Fetches the specified item.
  68. /// </summary>
  69. /// <param name="item">The item.</param>
  70. /// <param name="metadataFile">The metadata file.</param>
  71. /// <param name="settings">The settings.</param>
  72. /// <param name="encoding">The encoding.</param>
  73. /// <param name="cancellationToken">The cancellation token.</param>
  74. private void Fetch(T item, string metadataFile, XmlReaderSettings settings, Encoding encoding, CancellationToken cancellationToken)
  75. {
  76. using (var streamReader = new StreamReader(metadataFile, encoding))
  77. {
  78. // Use XmlReader for best performance
  79. using (var reader = XmlReader.Create(streamReader, settings))
  80. {
  81. reader.MoveToContent();
  82. // Loop through each element
  83. while (reader.Read())
  84. {
  85. cancellationToken.ThrowIfCancellationRequested();
  86. if (reader.NodeType == XmlNodeType.Element)
  87. {
  88. FetchDataFromXmlNode(reader, item);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  95. /// <summary>
  96. /// Fetches metadata from one Xml Element
  97. /// </summary>
  98. /// <param name="reader">The reader.</param>
  99. /// <param name="item">The item.</param>
  100. protected virtual void FetchDataFromXmlNode(XmlReader reader, T item)
  101. {
  102. switch (reader.Name)
  103. {
  104. // DateCreated
  105. case "Added":
  106. DateTime added;
  107. if (DateTime.TryParse(reader.ReadElementContentAsString() ?? string.Empty, out added))
  108. {
  109. item.DateCreated = added.ToUniversalTime();
  110. }
  111. break;
  112. case "LocalTitle":
  113. item.Name = reader.ReadElementContentAsString();
  114. break;
  115. case "Type":
  116. {
  117. var type = reader.ReadElementContentAsString();
  118. if (!string.IsNullOrWhiteSpace(type) && !type.Equals("none", StringComparison.OrdinalIgnoreCase))
  119. {
  120. item.DisplayMediaType = type;
  121. }
  122. break;
  123. }
  124. case "CriticRating":
  125. {
  126. var text = reader.ReadElementContentAsString();
  127. float value;
  128. if (float.TryParse(text, NumberStyles.Any, _usCulture, out value))
  129. {
  130. item.CriticRating = value;
  131. }
  132. break;
  133. }
  134. case "Budget":
  135. {
  136. var text = reader.ReadElementContentAsString();
  137. double value;
  138. if (double.TryParse(text, NumberStyles.Any, _usCulture, out value))
  139. {
  140. item.Budget = value;
  141. }
  142. break;
  143. }
  144. case "Revenue":
  145. {
  146. var text = reader.ReadElementContentAsString();
  147. double value;
  148. if (double.TryParse(text, NumberStyles.Any, _usCulture, out value))
  149. {
  150. item.Revenue = value;
  151. }
  152. break;
  153. }
  154. case "SortTitle":
  155. item.ForcedSortName = reader.ReadElementContentAsString();
  156. break;
  157. case "Overview":
  158. case "Description":
  159. {
  160. var val = reader.ReadElementContentAsString();
  161. if (!string.IsNullOrWhiteSpace(val))
  162. {
  163. item.Overview = val;
  164. }
  165. break;
  166. }
  167. case "CriticRatingSummary":
  168. {
  169. var val = reader.ReadElementContentAsString();
  170. if (!string.IsNullOrWhiteSpace(val))
  171. {
  172. item.CriticRatingSummary = val;
  173. }
  174. break;
  175. }
  176. case "TagLine":
  177. {
  178. var tagline = reader.ReadElementContentAsString();
  179. if (!string.IsNullOrWhiteSpace(tagline))
  180. {
  181. item.AddTagline(tagline);
  182. }
  183. break;
  184. }
  185. case "PlaceOfBirth":
  186. {
  187. var val = reader.ReadElementContentAsString();
  188. if (!string.IsNullOrWhiteSpace(val))
  189. {
  190. item.ProductionLocations = new List<string> { val };
  191. }
  192. break;
  193. }
  194. case "Website":
  195. {
  196. var val = reader.ReadElementContentAsString();
  197. if (!string.IsNullOrWhiteSpace(val))
  198. {
  199. item.HomePageUrl = val;
  200. }
  201. break;
  202. }
  203. case "TagLines":
  204. {
  205. FetchFromTaglinesNode(reader.ReadSubtree(), item);
  206. break;
  207. }
  208. case "ContentRating":
  209. case "certification":
  210. case "MPAARating":
  211. {
  212. var rating = reader.ReadElementContentAsString();
  213. if (!string.IsNullOrWhiteSpace(rating))
  214. {
  215. item.OfficialRating = rating;
  216. }
  217. break;
  218. }
  219. case "MPAADescription":
  220. {
  221. var rating = reader.ReadElementContentAsString();
  222. if (!string.IsNullOrWhiteSpace(rating))
  223. {
  224. item.OfficialRatingDescription = rating;
  225. }
  226. break;
  227. }
  228. case "CustomRating":
  229. {
  230. var val = reader.ReadElementContentAsString();
  231. if (!string.IsNullOrWhiteSpace(val))
  232. {
  233. item.CustomRating = val;
  234. }
  235. break;
  236. }
  237. case "Runtime":
  238. case "RunningTime":
  239. {
  240. var text = reader.ReadElementContentAsString();
  241. if (!string.IsNullOrWhiteSpace(text))
  242. {
  243. int runtime;
  244. if (int.TryParse(text.Split(' ')[0], NumberStyles.Integer, _usCulture, out runtime))
  245. {
  246. // For audio and video don't replace ffmpeg data
  247. if (item is Video || item is Audio)
  248. {
  249. item.OriginalRunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
  250. }
  251. else
  252. {
  253. item.RunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
  254. }
  255. }
  256. }
  257. break;
  258. }
  259. case "Genre":
  260. {
  261. foreach (var name in SplitNames(reader.ReadElementContentAsString()))
  262. {
  263. if (string.IsNullOrWhiteSpace(name))
  264. {
  265. continue;
  266. }
  267. item.AddGenre(name);
  268. }
  269. break;
  270. }
  271. case "AspectRatio":
  272. {
  273. var val = reader.ReadElementContentAsString();
  274. if (!string.IsNullOrWhiteSpace(val))
  275. {
  276. item.AspectRatio = val;
  277. }
  278. break;
  279. }
  280. case "LockData":
  281. {
  282. var val = reader.ReadElementContentAsString();
  283. if (!string.IsNullOrWhiteSpace(val))
  284. {
  285. item.DontFetchMeta = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  286. }
  287. break;
  288. }
  289. case "Network":
  290. {
  291. foreach (var name in SplitNames(reader.ReadElementContentAsString()))
  292. {
  293. if (string.IsNullOrWhiteSpace(name))
  294. {
  295. continue;
  296. }
  297. item.AddStudio(name);
  298. }
  299. break;
  300. }
  301. case "Director":
  302. {
  303. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Director }))
  304. {
  305. if (string.IsNullOrWhiteSpace(p.Name))
  306. {
  307. continue;
  308. }
  309. item.AddPerson(p);
  310. }
  311. break;
  312. }
  313. case "Writer":
  314. {
  315. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Writer }))
  316. {
  317. if (string.IsNullOrWhiteSpace(p.Name))
  318. {
  319. continue;
  320. }
  321. item.AddPerson(p);
  322. }
  323. break;
  324. }
  325. case "Actors":
  326. {
  327. var actors = reader.ReadInnerXml();
  328. if (actors.Contains("<"))
  329. {
  330. // This is one of the mis-named "Actors" full nodes created by MB2
  331. // Create a reader and pass it to the persons node processor
  332. FetchDataFromPersonsNode(new XmlTextReader(new StringReader("<Persons>" + actors + "</Persons>")), item);
  333. }
  334. else
  335. {
  336. // Old-style piped string
  337. foreach (var p in SplitNames(actors).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Actor }))
  338. {
  339. if (string.IsNullOrWhiteSpace(p.Name))
  340. {
  341. continue;
  342. }
  343. item.AddPerson(p);
  344. }
  345. }
  346. break;
  347. }
  348. case "GuestStars":
  349. {
  350. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.GuestStar }))
  351. {
  352. if (string.IsNullOrWhiteSpace(p.Name))
  353. {
  354. continue;
  355. }
  356. item.AddPerson(p);
  357. }
  358. break;
  359. }
  360. case "Trailer":
  361. {
  362. var val = reader.ReadElementContentAsString();
  363. if (!string.IsNullOrWhiteSpace(val))
  364. {
  365. item.AddTrailerUrl(val, false);
  366. }
  367. break;
  368. }
  369. case "ProductionYear":
  370. {
  371. var val = reader.ReadElementContentAsString();
  372. if (!string.IsNullOrWhiteSpace(val))
  373. {
  374. int productionYear;
  375. if (int.TryParse(val, out productionYear) && productionYear > 1850)
  376. {
  377. item.ProductionYear = productionYear;
  378. }
  379. }
  380. break;
  381. }
  382. case "Rating":
  383. case "IMDBrating":
  384. {
  385. var rating = reader.ReadElementContentAsString();
  386. if (!string.IsNullOrWhiteSpace(rating))
  387. {
  388. float val;
  389. // All external meta is saving this as '.' for decimal I believe...but just to be sure
  390. if (float.TryParse(rating.Replace(',', '.'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out val))
  391. {
  392. item.CommunityRating = val;
  393. }
  394. }
  395. break;
  396. }
  397. case "BirthDate":
  398. case "PremiereDate":
  399. case "FirstAired":
  400. {
  401. var firstAired = reader.ReadElementContentAsString();
  402. if (!string.IsNullOrWhiteSpace(firstAired))
  403. {
  404. DateTime airDate;
  405. if (DateTime.TryParse(firstAired, out airDate) && airDate.Year > 1850)
  406. {
  407. item.PremiereDate = airDate.ToUniversalTime();
  408. item.ProductionYear = airDate.Year;
  409. }
  410. }
  411. break;
  412. }
  413. case "DeathDate":
  414. case "EndDate":
  415. {
  416. var firstAired = reader.ReadElementContentAsString();
  417. if (!string.IsNullOrWhiteSpace(firstAired))
  418. {
  419. DateTime airDate;
  420. if (DateTime.TryParse(firstAired, out airDate) && airDate.Year > 1850)
  421. {
  422. item.EndDate = airDate.ToUniversalTime();
  423. }
  424. }
  425. break;
  426. }
  427. case "TvDbId":
  428. var tvdbId = reader.ReadElementContentAsString();
  429. if (!string.IsNullOrWhiteSpace(tvdbId))
  430. {
  431. item.SetProviderId(MetadataProviders.Tvdb, tvdbId);
  432. }
  433. break;
  434. case "GamesDbId":
  435. var gamesdbId = reader.ReadElementContentAsString();
  436. if (!string.IsNullOrWhiteSpace(gamesdbId))
  437. {
  438. item.SetProviderId(MetadataProviders.Gamesdb, gamesdbId);
  439. }
  440. break;
  441. case "MusicbrainzId":
  442. var mbz = reader.ReadElementContentAsString();
  443. if (!string.IsNullOrWhiteSpace(mbz))
  444. {
  445. item.SetProviderId(MetadataProviders.Musicbrainz, mbz);
  446. }
  447. break;
  448. case "RottenTomatoesId":
  449. var rtId = reader.ReadElementContentAsString();
  450. if (!string.IsNullOrWhiteSpace(rtId))
  451. {
  452. item.SetProviderId(MetadataProviders.RottenTomatoes, rtId);
  453. }
  454. break;
  455. case "TMDbId":
  456. var tmdb = reader.ReadElementContentAsString();
  457. if (!string.IsNullOrWhiteSpace(tmdb))
  458. {
  459. item.SetProviderId(MetadataProviders.Tmdb, tmdb);
  460. }
  461. break;
  462. case "CollectionNumber":
  463. var tmdbCollection = reader.ReadElementContentAsString();
  464. if (!string.IsNullOrWhiteSpace(tmdbCollection))
  465. {
  466. item.SetProviderId(MetadataProviders.TmdbCollection, tmdbCollection);
  467. }
  468. break;
  469. case "TVcomId":
  470. var TVcomId = reader.ReadElementContentAsString();
  471. if (!string.IsNullOrWhiteSpace(TVcomId))
  472. {
  473. item.SetProviderId(MetadataProviders.Tvcom, TVcomId);
  474. }
  475. break;
  476. case "IMDB_ID":
  477. case "IMDB":
  478. case "IMDbId":
  479. var imDbId = reader.ReadElementContentAsString();
  480. if (!string.IsNullOrWhiteSpace(imDbId))
  481. {
  482. item.SetProviderId(MetadataProviders.Imdb, imDbId);
  483. }
  484. break;
  485. case "Genres":
  486. FetchFromGenresNode(reader.ReadSubtree(), item);
  487. break;
  488. case "Tags":
  489. FetchFromTagsNode(reader.ReadSubtree(), item);
  490. break;
  491. case "Persons":
  492. FetchDataFromPersonsNode(reader.ReadSubtree(), item);
  493. break;
  494. case "ParentalRating":
  495. FetchFromParentalRatingNode(reader.ReadSubtree(), item);
  496. break;
  497. case "Studios":
  498. FetchFromStudiosNode(reader.ReadSubtree(), item);
  499. break;
  500. case "MediaInfo":
  501. FetchFromMediaInfoNode(reader.ReadSubtree(), item);
  502. break;
  503. default:
  504. reader.Skip();
  505. break;
  506. }
  507. }
  508. /// <summary>
  509. /// Fetches from media info node.
  510. /// </summary>
  511. /// <param name="reader">The reader.</param>
  512. /// <param name="item">The item.</param>
  513. private void FetchFromMediaInfoNode(XmlReader reader, T item)
  514. {
  515. reader.MoveToContent();
  516. while (reader.Read())
  517. {
  518. if (reader.NodeType == XmlNodeType.Element)
  519. {
  520. switch (reader.Name)
  521. {
  522. case "Video":
  523. FetchFromMediaInfoVideoNode(reader.ReadSubtree(), item);
  524. break;
  525. default:
  526. reader.Skip();
  527. break;
  528. }
  529. }
  530. }
  531. }
  532. /// <summary>
  533. /// Fetches from media info video node.
  534. /// </summary>
  535. /// <param name="reader">The reader.</param>
  536. /// <param name="item">The item.</param>
  537. private void FetchFromMediaInfoVideoNode(XmlReader reader, T item)
  538. {
  539. reader.MoveToContent();
  540. while (reader.Read())
  541. {
  542. if (reader.NodeType == XmlNodeType.Element)
  543. {
  544. switch (reader.Name)
  545. {
  546. case "Format3D":
  547. {
  548. var video = item as Video;
  549. if (video != null)
  550. {
  551. var val = reader.ReadElementContentAsString();
  552. if (string.Equals("HSBS", val))
  553. {
  554. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  555. }
  556. else if (string.Equals("HTAB", val))
  557. {
  558. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  559. }
  560. else if (string.Equals("FTAB", val))
  561. {
  562. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  563. }
  564. else if (string.Equals("FSBS", val))
  565. {
  566. video.Video3DFormat = Video3DFormat.FullSideBySide;
  567. }
  568. }
  569. break;
  570. }
  571. default:
  572. reader.Skip();
  573. break;
  574. }
  575. }
  576. }
  577. }
  578. /// <summary>
  579. /// Fetches from taglines node.
  580. /// </summary>
  581. /// <param name="reader">The reader.</param>
  582. /// <param name="item">The item.</param>
  583. private void FetchFromTaglinesNode(XmlReader reader, T item)
  584. {
  585. reader.MoveToContent();
  586. while (reader.Read())
  587. {
  588. if (reader.NodeType == XmlNodeType.Element)
  589. {
  590. switch (reader.Name)
  591. {
  592. case "Tagline":
  593. {
  594. var val = reader.ReadElementContentAsString();
  595. if (!string.IsNullOrWhiteSpace(val))
  596. {
  597. item.AddTagline(val);
  598. }
  599. break;
  600. }
  601. default:
  602. reader.Skip();
  603. break;
  604. }
  605. }
  606. }
  607. }
  608. /// <summary>
  609. /// Fetches from genres node.
  610. /// </summary>
  611. /// <param name="reader">The reader.</param>
  612. /// <param name="item">The item.</param>
  613. private void FetchFromGenresNode(XmlReader reader, T item)
  614. {
  615. reader.MoveToContent();
  616. while (reader.Read())
  617. {
  618. if (reader.NodeType == XmlNodeType.Element)
  619. {
  620. switch (reader.Name)
  621. {
  622. case "Genre":
  623. {
  624. var genre = reader.ReadElementContentAsString();
  625. if (!string.IsNullOrWhiteSpace(genre))
  626. {
  627. item.AddGenre(genre);
  628. }
  629. break;
  630. }
  631. default:
  632. reader.Skip();
  633. break;
  634. }
  635. }
  636. }
  637. }
  638. private void FetchFromTagsNode(XmlReader reader, T item)
  639. {
  640. reader.MoveToContent();
  641. while (reader.Read())
  642. {
  643. if (reader.NodeType == XmlNodeType.Element)
  644. {
  645. switch (reader.Name)
  646. {
  647. case "Tag":
  648. {
  649. var tag = reader.ReadElementContentAsString();
  650. if (!string.IsNullOrWhiteSpace(tag))
  651. {
  652. item.AddTag(tag);
  653. }
  654. break;
  655. }
  656. default:
  657. reader.Skip();
  658. break;
  659. }
  660. }
  661. }
  662. }
  663. /// <summary>
  664. /// Fetches the data from persons node.
  665. /// </summary>
  666. /// <param name="reader">The reader.</param>
  667. /// <param name="item">The item.</param>
  668. private void FetchDataFromPersonsNode(XmlReader reader, T item)
  669. {
  670. reader.MoveToContent();
  671. while (reader.Read())
  672. {
  673. if (reader.NodeType == XmlNodeType.Element)
  674. {
  675. switch (reader.Name)
  676. {
  677. case "Person":
  678. case "Actor":
  679. {
  680. foreach (var person in GetPersonsFromXmlNode(reader.ReadSubtree()))
  681. {
  682. item.AddPerson(person);
  683. }
  684. break;
  685. }
  686. default:
  687. reader.Skip();
  688. break;
  689. }
  690. }
  691. }
  692. }
  693. /// <summary>
  694. /// Fetches from studios node.
  695. /// </summary>
  696. /// <param name="reader">The reader.</param>
  697. /// <param name="item">The item.</param>
  698. private void FetchFromStudiosNode(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 "Studio":
  708. {
  709. var studio = reader.ReadElementContentAsString();
  710. if (!string.IsNullOrWhiteSpace(studio))
  711. {
  712. item.AddStudio(studio);
  713. }
  714. break;
  715. }
  716. default:
  717. reader.Skip();
  718. break;
  719. }
  720. }
  721. }
  722. }
  723. /// <summary>
  724. /// Fetches from parental rating node.
  725. /// </summary>
  726. /// <param name="reader">The reader.</param>
  727. /// <param name="item">The item.</param>
  728. private void FetchFromParentalRatingNode(XmlReader reader, T item)
  729. {
  730. reader.MoveToContent();
  731. while (reader.Read())
  732. {
  733. if (reader.NodeType == XmlNodeType.Element)
  734. {
  735. switch (reader.Name)
  736. {
  737. // Removed support for "Value" tag as it conflicted with MPAA rating but leaving this function for possible
  738. // future support of "Description" -ebr
  739. default:
  740. reader.Skip();
  741. break;
  742. }
  743. }
  744. }
  745. }
  746. /// <summary>
  747. /// Gets the persons from XML node.
  748. /// </summary>
  749. /// <param name="reader">The reader.</param>
  750. /// <returns>IEnumerable{PersonInfo}.</returns>
  751. private IEnumerable<PersonInfo> GetPersonsFromXmlNode(XmlReader reader)
  752. {
  753. var names = new List<string>();
  754. var type = "Actor"; // If type is not specified assume actor
  755. var role = string.Empty;
  756. reader.MoveToContent();
  757. while (reader.Read())
  758. {
  759. if (reader.NodeType == XmlNodeType.Element)
  760. {
  761. switch (reader.Name)
  762. {
  763. case "Name":
  764. names.AddRange(SplitNames(reader.ReadElementContentAsString()));
  765. break;
  766. case "Type":
  767. {
  768. var val = reader.ReadElementContentAsString();
  769. if (!string.IsNullOrWhiteSpace(val))
  770. {
  771. type = val;
  772. }
  773. break;
  774. }
  775. case "Role":
  776. {
  777. var val = reader.ReadElementContentAsString();
  778. if (!string.IsNullOrWhiteSpace(val))
  779. {
  780. role = val;
  781. }
  782. break;
  783. }
  784. default:
  785. reader.Skip();
  786. break;
  787. }
  788. }
  789. }
  790. return names.Select(n => new PersonInfo { Name = n.Trim(), Role = role, Type = type });
  791. }
  792. /// <summary>
  793. /// Used to split names of comma or pipe delimeted genres and people
  794. /// </summary>
  795. /// <param name="value">The value.</param>
  796. /// <returns>IEnumerable{System.String}.</returns>
  797. private IEnumerable<string> SplitNames(string value)
  798. {
  799. value = value ?? string.Empty;
  800. // Only split by comma if there is no pipe in the string
  801. // We have to be careful to not split names like Matthew, Jr.
  802. var separator = value.IndexOf('|') == -1 && value.IndexOf(';') == -1 ? new[] { ',' } : new[] { '|', ';' };
  803. value = value.Trim().Trim(separator);
  804. return string.IsNullOrWhiteSpace(value) ? new string[] { } : Split(value, separator, StringSplitOptions.RemoveEmptyEntries);
  805. }
  806. /// <summary>
  807. /// Provides an additional overload for string.split
  808. /// </summary>
  809. /// <param name="val">The val.</param>
  810. /// <param name="separators">The separators.</param>
  811. /// <param name="options">The options.</param>
  812. /// <returns>System.String[][].</returns>
  813. private static string[] Split(string val, char[] separators, StringSplitOptions options)
  814. {
  815. return val.Split(separators, options);
  816. }
  817. }
  818. }