BaseNfoParser.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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, tmdbId);
  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, tvdbId);
  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 fields = new List<MetadataFields>();
  333. var val = reader.ReadElementContentAsString();
  334. if (!string.IsNullOrWhiteSpace(val))
  335. {
  336. var list = val.Split('|').Select(i =>
  337. {
  338. MetadataFields field;
  339. if (Enum.TryParse<MetadataFields>(i, true, out field))
  340. {
  341. return (MetadataFields?)field;
  342. }
  343. return null;
  344. }).Where(i => i.HasValue).Select(i => i.Value);
  345. fields.AddRange(list);
  346. }
  347. item.LockedFields = fields;
  348. break;
  349. }
  350. case "tagline":
  351. {
  352. var val = reader.ReadElementContentAsString();
  353. if (!string.IsNullOrWhiteSpace(val))
  354. {
  355. item.Tagline = val;
  356. }
  357. break;
  358. }
  359. case "country":
  360. {
  361. var val = reader.ReadElementContentAsString();
  362. if (!string.IsNullOrWhiteSpace(val))
  363. {
  364. item.ProductionLocations.AddRange(val.Split('/')
  365. .Select(i => i.Trim())
  366. .Where(i => !string.IsNullOrWhiteSpace(i)));
  367. }
  368. break;
  369. }
  370. case "mpaa":
  371. {
  372. var rating = reader.ReadElementContentAsString();
  373. if (!string.IsNullOrWhiteSpace(rating))
  374. {
  375. item.OfficialRating = rating;
  376. }
  377. break;
  378. }
  379. case "customrating":
  380. {
  381. var val = reader.ReadElementContentAsString();
  382. if (!string.IsNullOrWhiteSpace(val))
  383. {
  384. item.CustomRating = val;
  385. }
  386. break;
  387. }
  388. case "runtime":
  389. {
  390. var text = reader.ReadElementContentAsString();
  391. if (!string.IsNullOrWhiteSpace(text))
  392. {
  393. int runtime;
  394. if (int.TryParse(text.Split(' ')[0], NumberStyles.Integer, _usCulture, out runtime))
  395. {
  396. item.RunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
  397. }
  398. }
  399. break;
  400. }
  401. case "aspectratio":
  402. {
  403. var val = reader.ReadElementContentAsString();
  404. var hasAspectRatio = item as IHasAspectRatio;
  405. if (!string.IsNullOrWhiteSpace(val) && hasAspectRatio != null)
  406. {
  407. hasAspectRatio.AspectRatio = val;
  408. }
  409. break;
  410. }
  411. case "lockdata":
  412. {
  413. var val = reader.ReadElementContentAsString();
  414. if (!string.IsNullOrWhiteSpace(val))
  415. {
  416. item.IsLocked = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  417. }
  418. break;
  419. }
  420. case "studio":
  421. {
  422. var val = reader.ReadElementContentAsString();
  423. if (!string.IsNullOrWhiteSpace(val))
  424. {
  425. //var parts = val.Split('/')
  426. // .Select(i => i.Trim())
  427. // .Where(i => !string.IsNullOrWhiteSpace(i));
  428. //foreach (var p in parts)
  429. //{
  430. // item.AddStudio(p);
  431. //}
  432. item.AddStudio(val);
  433. }
  434. break;
  435. }
  436. case "director":
  437. {
  438. var val = reader.ReadElementContentAsString();
  439. foreach (var p in SplitNames(val).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Director }))
  440. {
  441. if (string.IsNullOrWhiteSpace(p.Name))
  442. {
  443. continue;
  444. }
  445. itemResult.AddPerson(p);
  446. }
  447. break;
  448. }
  449. case "credits":
  450. {
  451. var val = reader.ReadElementContentAsString();
  452. if (!string.IsNullOrWhiteSpace(val))
  453. {
  454. var parts = val.Split('/').Select(i => i.Trim())
  455. .Where(i => !string.IsNullOrEmpty(i));
  456. foreach (var p in parts.Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Writer }))
  457. {
  458. if (string.IsNullOrWhiteSpace(p.Name))
  459. {
  460. continue;
  461. }
  462. itemResult.AddPerson(p);
  463. }
  464. }
  465. break;
  466. }
  467. case "writer":
  468. {
  469. var val = reader.ReadElementContentAsString();
  470. foreach (var p in SplitNames(val).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Writer }))
  471. {
  472. if (string.IsNullOrWhiteSpace(p.Name))
  473. {
  474. continue;
  475. }
  476. itemResult.AddPerson(p);
  477. }
  478. break;
  479. }
  480. case "actor":
  481. {
  482. if (!reader.IsEmptyElement)
  483. {
  484. using (var subtree = reader.ReadSubtree())
  485. {
  486. var person = GetPersonFromXmlNode(subtree);
  487. if (!string.IsNullOrWhiteSpace(person.Name))
  488. {
  489. itemResult.AddPerson(person);
  490. }
  491. }
  492. }
  493. else
  494. {
  495. reader.Read();
  496. }
  497. break;
  498. }
  499. case "trailer":
  500. {
  501. var val = reader.ReadElementContentAsString();
  502. var hasTrailer = item as IHasTrailers;
  503. if (hasTrailer != null)
  504. {
  505. if (!string.IsNullOrWhiteSpace(val))
  506. {
  507. val = val.Replace("plugin://plugin.video.youtube/?action=play_video&videoid=", "https://www.youtube.com/watch?v=", StringComparison.OrdinalIgnoreCase);
  508. hasTrailer.AddTrailerUrl(val, false);
  509. }
  510. }
  511. break;
  512. }
  513. case "displayorder":
  514. {
  515. var val = reader.ReadElementContentAsString();
  516. var hasDisplayOrder = item as IHasDisplayOrder;
  517. if (hasDisplayOrder != null)
  518. {
  519. if (!string.IsNullOrWhiteSpace(val))
  520. {
  521. hasDisplayOrder.DisplayOrder = val;
  522. }
  523. }
  524. break;
  525. }
  526. case "year":
  527. {
  528. var val = reader.ReadElementContentAsString();
  529. if (!string.IsNullOrWhiteSpace(val))
  530. {
  531. int productionYear;
  532. if (int.TryParse(val, out productionYear) && productionYear > 1850)
  533. {
  534. item.ProductionYear = productionYear;
  535. }
  536. }
  537. break;
  538. }
  539. case "rating":
  540. {
  541. var rating = reader.ReadElementContentAsString();
  542. if (!string.IsNullOrWhiteSpace(rating))
  543. {
  544. float val;
  545. // All external meta is saving this as '.' for decimal I believe...but just to be sure
  546. if (float.TryParse(rating.Replace(',', '.'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out val))
  547. {
  548. item.CommunityRating = val;
  549. }
  550. }
  551. break;
  552. }
  553. case "aired":
  554. case "formed":
  555. case "premiered":
  556. case "releasedate":
  557. {
  558. var formatString = _config.GetNfoConfiguration().ReleaseDateFormat;
  559. var val = reader.ReadElementContentAsString();
  560. if (!string.IsNullOrWhiteSpace(val))
  561. {
  562. DateTime date;
  563. if (DateTime.TryParseExact(val, formatString, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out date) && date.Year > 1850)
  564. {
  565. item.PremiereDate = date.ToUniversalTime();
  566. item.ProductionYear = date.Year;
  567. }
  568. }
  569. break;
  570. }
  571. case "enddate":
  572. {
  573. var formatString = _config.GetNfoConfiguration().ReleaseDateFormat;
  574. var val = reader.ReadElementContentAsString();
  575. if (!string.IsNullOrWhiteSpace(val))
  576. {
  577. DateTime date;
  578. if (DateTime.TryParseExact(val, formatString, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out date) && date.Year > 1850)
  579. {
  580. item.EndDate = date.ToUniversalTime();
  581. }
  582. }
  583. break;
  584. }
  585. case "genre":
  586. {
  587. var val = reader.ReadElementContentAsString();
  588. if (!string.IsNullOrWhiteSpace(val))
  589. {
  590. var parts = val.Split('/')
  591. .Select(i => i.Trim())
  592. .Where(i => !string.IsNullOrWhiteSpace(i));
  593. foreach (var p in parts)
  594. {
  595. item.AddGenre(p);
  596. }
  597. }
  598. break;
  599. }
  600. case "style":
  601. case "tag":
  602. {
  603. var val = reader.ReadElementContentAsString();
  604. if (!string.IsNullOrWhiteSpace(val))
  605. {
  606. item.AddTag(val);
  607. }
  608. break;
  609. }
  610. case "fileinfo":
  611. {
  612. if (!reader.IsEmptyElement)
  613. {
  614. using (var subtree = reader.ReadSubtree())
  615. {
  616. FetchFromFileInfoNode(subtree, item);
  617. }
  618. }
  619. else
  620. {
  621. reader.Read();
  622. }
  623. break;
  624. }
  625. default:
  626. string readerName = reader.Name;
  627. string providerIdValue;
  628. if (_validProviderIds.TryGetValue(readerName, out providerIdValue))
  629. {
  630. var id = reader.ReadElementContentAsString();
  631. if (!string.IsNullOrWhiteSpace(id))
  632. {
  633. item.SetProviderId(providerIdValue, id);
  634. }
  635. }
  636. else
  637. {
  638. reader.Skip();
  639. }
  640. break;
  641. }
  642. }
  643. private void FetchFromFileInfoNode(XmlReader reader, T item)
  644. {
  645. reader.MoveToContent();
  646. reader.Read();
  647. // Loop through each element
  648. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  649. {
  650. if (reader.NodeType == XmlNodeType.Element)
  651. {
  652. switch (reader.Name)
  653. {
  654. case "streamdetails":
  655. {
  656. if (reader.IsEmptyElement)
  657. {
  658. reader.Read();
  659. continue;
  660. }
  661. using (var subtree = reader.ReadSubtree())
  662. {
  663. FetchFromStreamDetailsNode(subtree, item);
  664. }
  665. break;
  666. }
  667. default:
  668. reader.Skip();
  669. break;
  670. }
  671. }
  672. else
  673. {
  674. reader.Read();
  675. }
  676. }
  677. }
  678. private void FetchFromStreamDetailsNode(XmlReader reader, T item)
  679. {
  680. reader.MoveToContent();
  681. reader.Read();
  682. // Loop through each element
  683. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  684. {
  685. if (reader.NodeType == XmlNodeType.Element)
  686. {
  687. switch (reader.Name)
  688. {
  689. case "video":
  690. {
  691. if (reader.IsEmptyElement)
  692. {
  693. reader.Read();
  694. continue;
  695. }
  696. using (var subtree = reader.ReadSubtree())
  697. {
  698. FetchFromVideoNode(subtree, item);
  699. }
  700. break;
  701. }
  702. default:
  703. reader.Skip();
  704. break;
  705. }
  706. }
  707. else
  708. {
  709. reader.Read();
  710. }
  711. }
  712. }
  713. private void FetchFromVideoNode(XmlReader reader, T item)
  714. {
  715. reader.MoveToContent();
  716. reader.Read();
  717. // Loop through each element
  718. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  719. {
  720. if (reader.NodeType == XmlNodeType.Element)
  721. {
  722. switch (reader.Name)
  723. {
  724. case "format3d":
  725. {
  726. var val = reader.ReadElementContentAsString();
  727. var video = item as Video;
  728. if (video != null)
  729. {
  730. if (string.Equals("HSBS", val, StringComparison.OrdinalIgnoreCase))
  731. {
  732. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  733. }
  734. else if (string.Equals("HTAB", val, StringComparison.OrdinalIgnoreCase))
  735. {
  736. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  737. }
  738. else if (string.Equals("FTAB", val, StringComparison.OrdinalIgnoreCase))
  739. {
  740. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  741. }
  742. else if (string.Equals("FSBS", val, StringComparison.OrdinalIgnoreCase))
  743. {
  744. video.Video3DFormat = Video3DFormat.FullSideBySide;
  745. }
  746. else if (string.Equals("MVC", val, StringComparison.OrdinalIgnoreCase))
  747. {
  748. video.Video3DFormat = Video3DFormat.MVC;
  749. }
  750. }
  751. break;
  752. }
  753. default:
  754. reader.Skip();
  755. break;
  756. }
  757. }
  758. else
  759. {
  760. reader.Read();
  761. }
  762. }
  763. }
  764. /// <summary>
  765. /// Gets the persons from XML node.
  766. /// </summary>
  767. /// <param name="reader">The reader.</param>
  768. /// <returns>IEnumerable{PersonInfo}.</returns>
  769. private PersonInfo GetPersonFromXmlNode(XmlReader reader)
  770. {
  771. var name = string.Empty;
  772. var type = PersonType.Actor; // If type is not specified assume actor
  773. var role = string.Empty;
  774. int? sortOrder = null;
  775. reader.MoveToContent();
  776. reader.Read();
  777. // Loop through each element
  778. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  779. {
  780. if (reader.NodeType == XmlNodeType.Element)
  781. {
  782. switch (reader.Name)
  783. {
  784. case "name":
  785. name = reader.ReadElementContentAsString() ?? string.Empty;
  786. break;
  787. case "role":
  788. {
  789. var val = reader.ReadElementContentAsString();
  790. if (!string.IsNullOrWhiteSpace(val))
  791. {
  792. role = val;
  793. }
  794. break;
  795. }
  796. case "sortorder":
  797. {
  798. var val = reader.ReadElementContentAsString();
  799. if (!string.IsNullOrWhiteSpace(val))
  800. {
  801. int intVal;
  802. if (int.TryParse(val, NumberStyles.Integer, _usCulture, out intVal))
  803. {
  804. sortOrder = intVal;
  805. }
  806. }
  807. break;
  808. }
  809. default:
  810. reader.Skip();
  811. break;
  812. }
  813. }
  814. else
  815. {
  816. reader.Read();
  817. }
  818. }
  819. return new PersonInfo
  820. {
  821. Name = name.Trim(),
  822. Role = role,
  823. Type = type,
  824. SortOrder = sortOrder
  825. };
  826. }
  827. /// <summary>
  828. /// Used to split names of comma or pipe delimeted genres and people
  829. /// </summary>
  830. /// <param name="value">The value.</param>
  831. /// <returns>IEnumerable{System.String}.</returns>
  832. private IEnumerable<string> SplitNames(string value)
  833. {
  834. value = value ?? string.Empty;
  835. // Only split by comma if there is no pipe in the string
  836. // We have to be careful to not split names like Matthew, Jr.
  837. var separator = value.IndexOf('|') == -1 && value.IndexOf(';') == -1 ? new[] { ',' } : new[] { '|', ';' };
  838. value = value.Trim().Trim(separator);
  839. return string.IsNullOrWhiteSpace(value) ? new string[] { } : Split(value, separator, StringSplitOptions.RemoveEmptyEntries);
  840. }
  841. /// <summary>
  842. /// Provides an additional overload for string.split
  843. /// </summary>
  844. /// <param name="val">The val.</param>
  845. /// <param name="separators">The separators.</param>
  846. /// <param name="options">The options.</param>
  847. /// <returns>System.String[][].</returns>
  848. private static string[] Split(string val, char[] separators, StringSplitOptions options)
  849. {
  850. return val.Split(separators, options);
  851. }
  852. }
  853. }