BaseNfoParser.cs 36 KB

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