BaseNfoParser.cs 38 KB

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