BaseNfoParser.cs 37 KB

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