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