BaseItemXmlParser.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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 "LockedFields":
  204. {
  205. var fields = new List<MetadataFields>();
  206. var val = reader.ReadElementContentAsString();
  207. if (!string.IsNullOrWhiteSpace(val))
  208. {
  209. var list = val.Split('|').Select(i =>
  210. {
  211. MetadataFields field;
  212. if (Enum.TryParse<MetadataFields>(i, true, out field))
  213. {
  214. return (MetadataFields?)field;
  215. }
  216. return null;
  217. }).Where(i => i.HasValue).Select(i => i.Value);
  218. fields.AddRange(list);
  219. }
  220. item.LockedFields = fields;
  221. break;
  222. }
  223. case "TagLines":
  224. {
  225. FetchFromTaglinesNode(reader.ReadSubtree(), item);
  226. break;
  227. }
  228. case "ContentRating":
  229. case "certification":
  230. case "MPAARating":
  231. {
  232. var rating = reader.ReadElementContentAsString();
  233. if (!string.IsNullOrWhiteSpace(rating))
  234. {
  235. item.OfficialRating = rating;
  236. }
  237. break;
  238. }
  239. case "MPAADescription":
  240. {
  241. var rating = reader.ReadElementContentAsString();
  242. if (!string.IsNullOrWhiteSpace(rating))
  243. {
  244. item.OfficialRatingDescription = rating;
  245. }
  246. break;
  247. }
  248. case "CustomRating":
  249. {
  250. var val = reader.ReadElementContentAsString();
  251. if (!string.IsNullOrWhiteSpace(val))
  252. {
  253. item.CustomRating = val;
  254. }
  255. break;
  256. }
  257. case "Runtime":
  258. case "RunningTime":
  259. {
  260. var text = reader.ReadElementContentAsString();
  261. if (!string.IsNullOrWhiteSpace(text))
  262. {
  263. int runtime;
  264. if (int.TryParse(text.Split(' ')[0], NumberStyles.Integer, _usCulture, out runtime))
  265. {
  266. // For audio and video don't replace ffmpeg data
  267. if (item is Video || item is Audio)
  268. {
  269. item.OriginalRunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
  270. }
  271. else
  272. {
  273. item.RunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
  274. }
  275. }
  276. }
  277. break;
  278. }
  279. case "Genre":
  280. {
  281. foreach (var name in SplitNames(reader.ReadElementContentAsString()))
  282. {
  283. if (string.IsNullOrWhiteSpace(name))
  284. {
  285. continue;
  286. }
  287. item.AddGenre(name);
  288. }
  289. break;
  290. }
  291. case "AspectRatio":
  292. {
  293. var val = reader.ReadElementContentAsString();
  294. if (!string.IsNullOrWhiteSpace(val))
  295. {
  296. item.AspectRatio = val;
  297. }
  298. break;
  299. }
  300. case "LockData":
  301. {
  302. var val = reader.ReadElementContentAsString();
  303. if (!string.IsNullOrWhiteSpace(val))
  304. {
  305. item.DontFetchMeta = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  306. }
  307. break;
  308. }
  309. case "Network":
  310. {
  311. foreach (var name in SplitNames(reader.ReadElementContentAsString()))
  312. {
  313. if (string.IsNullOrWhiteSpace(name))
  314. {
  315. continue;
  316. }
  317. item.AddStudio(name);
  318. }
  319. break;
  320. }
  321. case "Director":
  322. {
  323. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Director }))
  324. {
  325. if (string.IsNullOrWhiteSpace(p.Name))
  326. {
  327. continue;
  328. }
  329. item.AddPerson(p);
  330. }
  331. break;
  332. }
  333. case "Writer":
  334. {
  335. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Writer }))
  336. {
  337. if (string.IsNullOrWhiteSpace(p.Name))
  338. {
  339. continue;
  340. }
  341. item.AddPerson(p);
  342. }
  343. break;
  344. }
  345. case "Actors":
  346. {
  347. var actors = reader.ReadInnerXml();
  348. if (actors.Contains("<"))
  349. {
  350. // This is one of the mis-named "Actors" full nodes created by MB2
  351. // Create a reader and pass it to the persons node processor
  352. FetchDataFromPersonsNode(new XmlTextReader(new StringReader("<Persons>" + actors + "</Persons>")), item);
  353. }
  354. else
  355. {
  356. // Old-style piped string
  357. foreach (var p in SplitNames(actors).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Actor }))
  358. {
  359. if (string.IsNullOrWhiteSpace(p.Name))
  360. {
  361. continue;
  362. }
  363. item.AddPerson(p);
  364. }
  365. }
  366. break;
  367. }
  368. case "GuestStars":
  369. {
  370. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.GuestStar }))
  371. {
  372. if (string.IsNullOrWhiteSpace(p.Name))
  373. {
  374. continue;
  375. }
  376. item.AddPerson(p);
  377. }
  378. break;
  379. }
  380. case "Trailer":
  381. {
  382. var val = reader.ReadElementContentAsString();
  383. if (!string.IsNullOrWhiteSpace(val))
  384. {
  385. item.AddTrailerUrl(val, false);
  386. }
  387. break;
  388. }
  389. case "ProductionYear":
  390. {
  391. var val = reader.ReadElementContentAsString();
  392. if (!string.IsNullOrWhiteSpace(val))
  393. {
  394. int productionYear;
  395. if (int.TryParse(val, out productionYear) && productionYear > 1850)
  396. {
  397. item.ProductionYear = productionYear;
  398. }
  399. }
  400. break;
  401. }
  402. case "Rating":
  403. case "IMDBrating":
  404. {
  405. var rating = reader.ReadElementContentAsString();
  406. if (!string.IsNullOrWhiteSpace(rating))
  407. {
  408. float val;
  409. // All external meta is saving this as '.' for decimal I believe...but just to be sure
  410. if (float.TryParse(rating.Replace(',', '.'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out val))
  411. {
  412. item.CommunityRating = val;
  413. }
  414. }
  415. break;
  416. }
  417. case "BirthDate":
  418. case "PremiereDate":
  419. case "FirstAired":
  420. {
  421. var firstAired = reader.ReadElementContentAsString();
  422. if (!string.IsNullOrWhiteSpace(firstAired))
  423. {
  424. DateTime airDate;
  425. if (DateTime.TryParse(firstAired, out airDate) && airDate.Year > 1850)
  426. {
  427. item.PremiereDate = airDate.ToUniversalTime();
  428. item.ProductionYear = airDate.Year;
  429. }
  430. }
  431. break;
  432. }
  433. case "DeathDate":
  434. case "EndDate":
  435. {
  436. var firstAired = reader.ReadElementContentAsString();
  437. if (!string.IsNullOrWhiteSpace(firstAired))
  438. {
  439. DateTime airDate;
  440. if (DateTime.TryParse(firstAired, out airDate) && airDate.Year > 1850)
  441. {
  442. item.EndDate = airDate.ToUniversalTime();
  443. }
  444. }
  445. break;
  446. }
  447. case "TvDbId":
  448. var tvdbId = reader.ReadElementContentAsString();
  449. if (!string.IsNullOrWhiteSpace(tvdbId))
  450. {
  451. item.SetProviderId(MetadataProviders.Tvdb, tvdbId);
  452. }
  453. break;
  454. case "GamesDbId":
  455. var gamesdbId = reader.ReadElementContentAsString();
  456. if (!string.IsNullOrWhiteSpace(gamesdbId))
  457. {
  458. item.SetProviderId(MetadataProviders.Gamesdb, gamesdbId);
  459. }
  460. break;
  461. case "MusicbrainzId":
  462. var mbz = reader.ReadElementContentAsString();
  463. if (!string.IsNullOrWhiteSpace(mbz))
  464. {
  465. item.SetProviderId(MetadataProviders.Musicbrainz, mbz);
  466. }
  467. break;
  468. case "RottenTomatoesId":
  469. var rtId = reader.ReadElementContentAsString();
  470. if (!string.IsNullOrWhiteSpace(rtId))
  471. {
  472. item.SetProviderId(MetadataProviders.RottenTomatoes, rtId);
  473. }
  474. break;
  475. case "TMDbId":
  476. var tmdb = reader.ReadElementContentAsString();
  477. if (!string.IsNullOrWhiteSpace(tmdb))
  478. {
  479. item.SetProviderId(MetadataProviders.Tmdb, tmdb);
  480. }
  481. break;
  482. case "CollectionNumber":
  483. var tmdbCollection = reader.ReadElementContentAsString();
  484. if (!string.IsNullOrWhiteSpace(tmdbCollection))
  485. {
  486. item.SetProviderId(MetadataProviders.TmdbCollection, tmdbCollection);
  487. }
  488. break;
  489. case "TVcomId":
  490. var TVcomId = reader.ReadElementContentAsString();
  491. if (!string.IsNullOrWhiteSpace(TVcomId))
  492. {
  493. item.SetProviderId(MetadataProviders.Tvcom, TVcomId);
  494. }
  495. break;
  496. case "IMDB_ID":
  497. case "IMDB":
  498. case "IMDbId":
  499. var imDbId = reader.ReadElementContentAsString();
  500. if (!string.IsNullOrWhiteSpace(imDbId))
  501. {
  502. item.SetProviderId(MetadataProviders.Imdb, imDbId);
  503. }
  504. break;
  505. case "Genres":
  506. FetchFromGenresNode(reader.ReadSubtree(), item);
  507. break;
  508. case "Tags":
  509. FetchFromTagsNode(reader.ReadSubtree(), item);
  510. break;
  511. case "Persons":
  512. FetchDataFromPersonsNode(reader.ReadSubtree(), item);
  513. break;
  514. case "ParentalRating":
  515. FetchFromParentalRatingNode(reader.ReadSubtree(), item);
  516. break;
  517. case "Studios":
  518. FetchFromStudiosNode(reader.ReadSubtree(), item);
  519. break;
  520. case "MediaInfo":
  521. FetchFromMediaInfoNode(reader.ReadSubtree(), item);
  522. break;
  523. default:
  524. reader.Skip();
  525. break;
  526. }
  527. }
  528. /// <summary>
  529. /// Fetches from media info node.
  530. /// </summary>
  531. /// <param name="reader">The reader.</param>
  532. /// <param name="item">The item.</param>
  533. private void FetchFromMediaInfoNode(XmlReader reader, T item)
  534. {
  535. reader.MoveToContent();
  536. while (reader.Read())
  537. {
  538. if (reader.NodeType == XmlNodeType.Element)
  539. {
  540. switch (reader.Name)
  541. {
  542. case "Video":
  543. FetchFromMediaInfoVideoNode(reader.ReadSubtree(), item);
  544. break;
  545. default:
  546. reader.Skip();
  547. break;
  548. }
  549. }
  550. }
  551. }
  552. /// <summary>
  553. /// Fetches from media info video node.
  554. /// </summary>
  555. /// <param name="reader">The reader.</param>
  556. /// <param name="item">The item.</param>
  557. private void FetchFromMediaInfoVideoNode(XmlReader reader, T item)
  558. {
  559. reader.MoveToContent();
  560. while (reader.Read())
  561. {
  562. if (reader.NodeType == XmlNodeType.Element)
  563. {
  564. switch (reader.Name)
  565. {
  566. case "Format3D":
  567. {
  568. var video = item as Video;
  569. if (video != null)
  570. {
  571. var val = reader.ReadElementContentAsString();
  572. if (string.Equals("HSBS", val))
  573. {
  574. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  575. }
  576. else if (string.Equals("HTAB", val))
  577. {
  578. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  579. }
  580. else if (string.Equals("FTAB", val))
  581. {
  582. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  583. }
  584. else if (string.Equals("FSBS", val))
  585. {
  586. video.Video3DFormat = Video3DFormat.FullSideBySide;
  587. }
  588. }
  589. break;
  590. }
  591. default:
  592. reader.Skip();
  593. break;
  594. }
  595. }
  596. }
  597. }
  598. /// <summary>
  599. /// Fetches from taglines node.
  600. /// </summary>
  601. /// <param name="reader">The reader.</param>
  602. /// <param name="item">The item.</param>
  603. private void FetchFromTaglinesNode(XmlReader reader, T item)
  604. {
  605. reader.MoveToContent();
  606. while (reader.Read())
  607. {
  608. if (reader.NodeType == XmlNodeType.Element)
  609. {
  610. switch (reader.Name)
  611. {
  612. case "Tagline":
  613. {
  614. var val = reader.ReadElementContentAsString();
  615. if (!string.IsNullOrWhiteSpace(val))
  616. {
  617. item.AddTagline(val);
  618. }
  619. break;
  620. }
  621. default:
  622. reader.Skip();
  623. break;
  624. }
  625. }
  626. }
  627. }
  628. /// <summary>
  629. /// Fetches from genres node.
  630. /// </summary>
  631. /// <param name="reader">The reader.</param>
  632. /// <param name="item">The item.</param>
  633. private void FetchFromGenresNode(XmlReader reader, T item)
  634. {
  635. reader.MoveToContent();
  636. while (reader.Read())
  637. {
  638. if (reader.NodeType == XmlNodeType.Element)
  639. {
  640. switch (reader.Name)
  641. {
  642. case "Genre":
  643. {
  644. var genre = reader.ReadElementContentAsString();
  645. if (!string.IsNullOrWhiteSpace(genre))
  646. {
  647. item.AddGenre(genre);
  648. }
  649. break;
  650. }
  651. default:
  652. reader.Skip();
  653. break;
  654. }
  655. }
  656. }
  657. }
  658. private void FetchFromTagsNode(XmlReader reader, T item)
  659. {
  660. reader.MoveToContent();
  661. while (reader.Read())
  662. {
  663. if (reader.NodeType == XmlNodeType.Element)
  664. {
  665. switch (reader.Name)
  666. {
  667. case "Tag":
  668. {
  669. var tag = reader.ReadElementContentAsString();
  670. if (!string.IsNullOrWhiteSpace(tag))
  671. {
  672. item.AddTag(tag);
  673. }
  674. break;
  675. }
  676. default:
  677. reader.Skip();
  678. break;
  679. }
  680. }
  681. }
  682. }
  683. /// <summary>
  684. /// Fetches the data from persons node.
  685. /// </summary>
  686. /// <param name="reader">The reader.</param>
  687. /// <param name="item">The item.</param>
  688. private void FetchDataFromPersonsNode(XmlReader reader, T item)
  689. {
  690. reader.MoveToContent();
  691. while (reader.Read())
  692. {
  693. if (reader.NodeType == XmlNodeType.Element)
  694. {
  695. switch (reader.Name)
  696. {
  697. case "Person":
  698. case "Actor":
  699. {
  700. foreach (var person in GetPersonsFromXmlNode(reader.ReadSubtree()))
  701. {
  702. item.AddPerson(person);
  703. }
  704. break;
  705. }
  706. default:
  707. reader.Skip();
  708. break;
  709. }
  710. }
  711. }
  712. }
  713. /// <summary>
  714. /// Fetches from studios node.
  715. /// </summary>
  716. /// <param name="reader">The reader.</param>
  717. /// <param name="item">The item.</param>
  718. private void FetchFromStudiosNode(XmlReader reader, T item)
  719. {
  720. reader.MoveToContent();
  721. while (reader.Read())
  722. {
  723. if (reader.NodeType == XmlNodeType.Element)
  724. {
  725. switch (reader.Name)
  726. {
  727. case "Studio":
  728. {
  729. var studio = reader.ReadElementContentAsString();
  730. if (!string.IsNullOrWhiteSpace(studio))
  731. {
  732. item.AddStudio(studio);
  733. }
  734. break;
  735. }
  736. default:
  737. reader.Skip();
  738. break;
  739. }
  740. }
  741. }
  742. }
  743. /// <summary>
  744. /// Fetches from parental rating node.
  745. /// </summary>
  746. /// <param name="reader">The reader.</param>
  747. /// <param name="item">The item.</param>
  748. private void FetchFromParentalRatingNode(XmlReader reader, T item)
  749. {
  750. reader.MoveToContent();
  751. while (reader.Read())
  752. {
  753. if (reader.NodeType == XmlNodeType.Element)
  754. {
  755. switch (reader.Name)
  756. {
  757. // Removed support for "Value" tag as it conflicted with MPAA rating but leaving this function for possible
  758. // future support of "Description" -ebr
  759. default:
  760. reader.Skip();
  761. break;
  762. }
  763. }
  764. }
  765. }
  766. /// <summary>
  767. /// Gets the persons from XML node.
  768. /// </summary>
  769. /// <param name="reader">The reader.</param>
  770. /// <returns>IEnumerable{PersonInfo}.</returns>
  771. private IEnumerable<PersonInfo> GetPersonsFromXmlNode(XmlReader reader)
  772. {
  773. var names = new List<string>();
  774. var type = "Actor"; // If type is not specified assume actor
  775. var role = string.Empty;
  776. reader.MoveToContent();
  777. while (reader.Read())
  778. {
  779. if (reader.NodeType == XmlNodeType.Element)
  780. {
  781. switch (reader.Name)
  782. {
  783. case "Name":
  784. names.AddRange(SplitNames(reader.ReadElementContentAsString()));
  785. break;
  786. case "Type":
  787. {
  788. var val = reader.ReadElementContentAsString();
  789. if (!string.IsNullOrWhiteSpace(val))
  790. {
  791. type = val;
  792. }
  793. break;
  794. }
  795. case "Role":
  796. {
  797. var val = reader.ReadElementContentAsString();
  798. if (!string.IsNullOrWhiteSpace(val))
  799. {
  800. role = val;
  801. }
  802. break;
  803. }
  804. default:
  805. reader.Skip();
  806. break;
  807. }
  808. }
  809. }
  810. return names.Select(n => new PersonInfo { Name = n.Trim(), Role = role, Type = type });
  811. }
  812. /// <summary>
  813. /// Used to split names of comma or pipe delimeted genres and people
  814. /// </summary>
  815. /// <param name="value">The value.</param>
  816. /// <returns>IEnumerable{System.String}.</returns>
  817. private IEnumerable<string> SplitNames(string value)
  818. {
  819. value = value ?? string.Empty;
  820. // Only split by comma if there is no pipe in the string
  821. // We have to be careful to not split names like Matthew, Jr.
  822. var separator = value.IndexOf('|') == -1 && value.IndexOf(';') == -1 ? new[] { ',' } : new[] { '|', ';' };
  823. value = value.Trim().Trim(separator);
  824. return string.IsNullOrWhiteSpace(value) ? new string[] { } : Split(value, separator, StringSplitOptions.RemoveEmptyEntries);
  825. }
  826. /// <summary>
  827. /// Provides an additional overload for string.split
  828. /// </summary>
  829. /// <param name="val">The val.</param>
  830. /// <param name="separators">The separators.</param>
  831. /// <param name="options">The options.</param>
  832. /// <returns>System.String[][].</returns>
  833. private static string[] Split(string val, char[] separators, StringSplitOptions options)
  834. {
  835. return val.Split(separators, options);
  836. }
  837. }
  838. }