BaseNfoParser.cs 38 KB

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