BaseNfoParser.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. using System.IO;
  2. using System.Text;
  3. using MediaBrowser.Common.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Model.Entities;
  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.Linq;
  13. using System.Threading;
  14. using System.Xml;
  15. namespace MediaBrowser.XbmcMetadata.Parsers
  16. {
  17. public class BaseNfoParser<T>
  18. where T : BaseItem
  19. {
  20. /// <summary>
  21. /// The logger
  22. /// </summary>
  23. protected ILogger Logger { get; private set; }
  24. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  25. private readonly IConfigurationManager _config;
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="BaseNfoParser{T}" /> class.
  28. /// </summary>
  29. /// <param name="logger">The logger.</param>
  30. /// <param name="config">The configuration.</param>
  31. public BaseNfoParser(ILogger logger, IConfigurationManager config)
  32. {
  33. Logger = logger;
  34. _config = config;
  35. }
  36. /// <summary>
  37. /// Fetches metadata for an item from one xml file
  38. /// </summary>
  39. /// <param name="item">The item.</param>
  40. /// <param name="userDataList">The user data list.</param>
  41. /// <param name="metadataFile">The metadata file.</param>
  42. /// <param name="cancellationToken">The cancellation token.</param>
  43. /// <exception cref="System.ArgumentNullException">
  44. /// </exception>
  45. public void Fetch(T item, List<UserItemData> userDataList, string metadataFile, CancellationToken cancellationToken)
  46. {
  47. if (item == null)
  48. {
  49. throw new ArgumentNullException();
  50. }
  51. if (string.IsNullOrEmpty(metadataFile))
  52. {
  53. throw new ArgumentNullException();
  54. }
  55. var settings = new XmlReaderSettings
  56. {
  57. CheckCharacters = false,
  58. IgnoreProcessingInstructions = true,
  59. IgnoreComments = true,
  60. ValidationType = ValidationType.None
  61. };
  62. Fetch(item, userDataList, metadataFile, settings, cancellationToken);
  63. }
  64. /// <summary>
  65. /// Fetches the specified item.
  66. /// </summary>
  67. /// <param name="item">The item.</param>
  68. /// <param name="userDataList">The user data list.</param>
  69. /// <param name="metadataFile">The metadata file.</param>
  70. /// <param name="settings">The settings.</param>
  71. /// <param name="cancellationToken">The cancellation token.</param>
  72. private void Fetch(T item, List<UserItemData> userDataList, string metadataFile, XmlReaderSettings settings, CancellationToken cancellationToken)
  73. {
  74. using (var streamReader = BaseNfoSaver.GetStreamReader(metadataFile))
  75. {
  76. // Need to handle a url after the xml data
  77. // http://kodi.wiki/view/NFO_files/movies
  78. var xml = streamReader.ReadToEnd();
  79. var index = xml.LastIndexOf('>');
  80. if (index != -1)
  81. {
  82. xml = xml.Substring(0, index + 1);
  83. }
  84. using (var ms = new MemoryStream())
  85. {
  86. var bytes = Encoding.UTF8.GetBytes(xml);
  87. ms.Write(bytes, 0, bytes.Length);
  88. ms.Position = 0;
  89. // Use XmlReader for best performance
  90. using (var reader = XmlReader.Create(ms, settings))
  91. {
  92. reader.MoveToContent();
  93. // Loop through each element
  94. while (reader.Read())
  95. {
  96. cancellationToken.ThrowIfCancellationRequested();
  97. if (reader.NodeType == XmlNodeType.Element)
  98. {
  99. FetchDataFromXmlNode(reader, item, userDataList);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. protected virtual void FetchDataFromXmlNode(XmlReader reader, T item, List<UserItemData> userDataList)
  107. {
  108. var userDataUserId = _config.GetNfoConfiguration().UserId;
  109. switch (reader.Name)
  110. {
  111. // DateCreated
  112. case "dateadded":
  113. {
  114. var val = reader.ReadElementContentAsString();
  115. if (!string.IsNullOrWhiteSpace(val))
  116. {
  117. DateTime added;
  118. if (DateTime.TryParse(val, out added))
  119. {
  120. item.DateCreated = added.ToUniversalTime();
  121. }
  122. else
  123. {
  124. Logger.Warn("Invalid Added value found: " + val);
  125. }
  126. }
  127. break;
  128. }
  129. case "title":
  130. case "localtitle":
  131. item.Name = reader.ReadElementContentAsString();
  132. break;
  133. case "criticrating":
  134. {
  135. var text = reader.ReadElementContentAsString();
  136. var hasCriticRating = item as IHasCriticRating;
  137. if (hasCriticRating != null && !string.IsNullOrEmpty(text))
  138. {
  139. float value;
  140. if (float.TryParse(text, NumberStyles.Any, _usCulture, out value))
  141. {
  142. hasCriticRating.CriticRating = value;
  143. }
  144. }
  145. break;
  146. }
  147. case "budget":
  148. {
  149. var text = reader.ReadElementContentAsString();
  150. var hasBudget = item as IHasBudget;
  151. if (hasBudget != null)
  152. {
  153. double value;
  154. if (double.TryParse(text, NumberStyles.Any, _usCulture, out value))
  155. {
  156. hasBudget.Budget = value;
  157. }
  158. }
  159. break;
  160. }
  161. case "revenue":
  162. {
  163. var text = reader.ReadElementContentAsString();
  164. var hasBudget = item as IHasBudget;
  165. if (hasBudget != null)
  166. {
  167. double value;
  168. if (double.TryParse(text, NumberStyles.Any, _usCulture, out value))
  169. {
  170. hasBudget.Revenue = value;
  171. }
  172. }
  173. break;
  174. }
  175. case "metascore":
  176. {
  177. var text = reader.ReadElementContentAsString();
  178. var hasMetascore = item as IHasMetascore;
  179. if (hasMetascore != null)
  180. {
  181. float value;
  182. if (float.TryParse(text, NumberStyles.Any, _usCulture, out value))
  183. {
  184. hasMetascore.Metascore = value;
  185. }
  186. }
  187. break;
  188. }
  189. case "awardsummary":
  190. {
  191. var text = reader.ReadElementContentAsString();
  192. var hasAwards = item as IHasAwards;
  193. if (hasAwards != null)
  194. {
  195. if (!string.IsNullOrWhiteSpace(text))
  196. {
  197. hasAwards.AwardSummary = text;
  198. }
  199. }
  200. break;
  201. }
  202. case "sorttitle":
  203. {
  204. var val = reader.ReadElementContentAsString();
  205. if (!string.IsNullOrWhiteSpace(val))
  206. {
  207. item.ForcedSortName = val;
  208. }
  209. break;
  210. }
  211. case "outline":
  212. {
  213. var val = reader.ReadElementContentAsString();
  214. if (!string.IsNullOrWhiteSpace(val))
  215. {
  216. var hasShortOverview = item as IHasShortOverview;
  217. if (hasShortOverview != null)
  218. {
  219. hasShortOverview.ShortOverview = val;
  220. }
  221. }
  222. break;
  223. }
  224. case "biography":
  225. case "plot":
  226. case "review":
  227. {
  228. var val = reader.ReadElementContentAsString();
  229. if (!string.IsNullOrWhiteSpace(val))
  230. {
  231. item.Overview = val;
  232. }
  233. break;
  234. }
  235. case "criticratingsummary":
  236. {
  237. var val = reader.ReadElementContentAsString();
  238. if (!string.IsNullOrWhiteSpace(val))
  239. {
  240. var hasCriticRating = item as IHasCriticRating;
  241. if (hasCriticRating != null)
  242. {
  243. hasCriticRating.CriticRatingSummary = val;
  244. }
  245. }
  246. break;
  247. }
  248. case "language":
  249. {
  250. var val = reader.ReadElementContentAsString();
  251. var hasLanguage = item as IHasPreferredMetadataLanguage;
  252. if (hasLanguage != null)
  253. {
  254. hasLanguage.PreferredMetadataLanguage = val;
  255. }
  256. break;
  257. }
  258. case "website":
  259. {
  260. var val = reader.ReadElementContentAsString();
  261. if (!string.IsNullOrWhiteSpace(val))
  262. {
  263. item.HomePageUrl = val;
  264. }
  265. break;
  266. }
  267. case "lockedfields":
  268. {
  269. var fields = new List<MetadataFields>();
  270. var val = reader.ReadElementContentAsString();
  271. if (!string.IsNullOrWhiteSpace(val))
  272. {
  273. var list = val.Split('|').Select(i =>
  274. {
  275. MetadataFields field;
  276. if (Enum.TryParse<MetadataFields>(i, true, out field))
  277. {
  278. return (MetadataFields?)field;
  279. }
  280. return null;
  281. }).Where(i => i.HasValue).Select(i => i.Value);
  282. fields.AddRange(list);
  283. }
  284. item.LockedFields = fields;
  285. break;
  286. }
  287. case "tagline":
  288. {
  289. var val = reader.ReadElementContentAsString();
  290. var hasTagline = item as IHasTaglines;
  291. if (hasTagline != null)
  292. {
  293. if (!string.IsNullOrWhiteSpace(val))
  294. {
  295. hasTagline.AddTagline(val);
  296. }
  297. }
  298. break;
  299. }
  300. case "country":
  301. {
  302. var val = reader.ReadElementContentAsString();
  303. var hasProductionLocations = item as IHasProductionLocations;
  304. if (hasProductionLocations != null)
  305. {
  306. if (!string.IsNullOrWhiteSpace(val))
  307. {
  308. var parts = val.Split('/')
  309. .Select(i => i.Trim())
  310. .Where(i => !string.IsNullOrWhiteSpace(i));
  311. foreach (var p in parts)
  312. {
  313. hasProductionLocations.AddProductionLocation(p);
  314. }
  315. }
  316. }
  317. break;
  318. }
  319. case "mpaa":
  320. {
  321. var rating = reader.ReadElementContentAsString();
  322. if (!string.IsNullOrWhiteSpace(rating))
  323. {
  324. item.OfficialRating = rating;
  325. }
  326. break;
  327. }
  328. case "mpaadescription":
  329. {
  330. var rating = reader.ReadElementContentAsString();
  331. if (!string.IsNullOrWhiteSpace(rating))
  332. {
  333. item.OfficialRatingDescription = rating;
  334. }
  335. break;
  336. }
  337. case "customrating":
  338. {
  339. var val = reader.ReadElementContentAsString();
  340. if (!string.IsNullOrWhiteSpace(val))
  341. {
  342. item.CustomRating = val;
  343. }
  344. break;
  345. }
  346. case "runtime":
  347. {
  348. var text = reader.ReadElementContentAsString();
  349. if (!string.IsNullOrWhiteSpace(text))
  350. {
  351. int runtime;
  352. if (int.TryParse(text.Split(' ')[0], NumberStyles.Integer, _usCulture, out runtime))
  353. {
  354. item.RunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
  355. }
  356. }
  357. break;
  358. }
  359. case "aspectratio":
  360. {
  361. var val = reader.ReadElementContentAsString();
  362. var hasAspectRatio = item as IHasAspectRatio;
  363. if (!string.IsNullOrWhiteSpace(val) && hasAspectRatio != null)
  364. {
  365. hasAspectRatio.AspectRatio = val;
  366. }
  367. break;
  368. }
  369. case "lockdata":
  370. {
  371. var val = reader.ReadElementContentAsString();
  372. if (!string.IsNullOrWhiteSpace(val))
  373. {
  374. item.IsLocked = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  375. }
  376. break;
  377. }
  378. case "studio":
  379. {
  380. var val = reader.ReadElementContentAsString();
  381. if (!string.IsNullOrWhiteSpace(val))
  382. {
  383. var parts = val.Split('/')
  384. .Select(i => i.Trim())
  385. .Where(i => !string.IsNullOrWhiteSpace(i));
  386. foreach (var p in parts)
  387. {
  388. item.AddStudio(p);
  389. }
  390. }
  391. break;
  392. }
  393. case "director":
  394. {
  395. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Director }))
  396. {
  397. if (string.IsNullOrWhiteSpace(p.Name))
  398. {
  399. continue;
  400. }
  401. item.AddPerson(p);
  402. }
  403. break;
  404. }
  405. case "credits":
  406. {
  407. var val = reader.ReadElementContentAsString();
  408. if (!string.IsNullOrWhiteSpace(val))
  409. {
  410. var parts = val.Split('/').Select(i => i.Trim())
  411. .Where(i => !string.IsNullOrEmpty(i));
  412. foreach (var p in parts.Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Writer }))
  413. {
  414. if (string.IsNullOrWhiteSpace(p.Name))
  415. {
  416. continue;
  417. }
  418. item.AddPerson(p);
  419. }
  420. }
  421. break;
  422. }
  423. case "writer":
  424. {
  425. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonType.Writer }))
  426. {
  427. if (string.IsNullOrWhiteSpace(p.Name))
  428. {
  429. continue;
  430. }
  431. item.AddPerson(p);
  432. }
  433. break;
  434. }
  435. case "actor":
  436. {
  437. using (var subtree = reader.ReadSubtree())
  438. {
  439. var person = GetPersonFromXmlNode(subtree);
  440. item.AddPerson(person);
  441. }
  442. break;
  443. }
  444. case "trailer":
  445. {
  446. var val = reader.ReadElementContentAsString();
  447. var hasTrailer = item as IHasTrailers;
  448. if (hasTrailer != null)
  449. {
  450. if (!string.IsNullOrWhiteSpace(val))
  451. {
  452. hasTrailer.AddTrailerUrl(val, false);
  453. }
  454. }
  455. break;
  456. }
  457. case "displayorder":
  458. {
  459. var val = reader.ReadElementContentAsString();
  460. var hasDisplayOrder = item as IHasDisplayOrder;
  461. if (hasDisplayOrder != null)
  462. {
  463. if (!string.IsNullOrWhiteSpace(val))
  464. {
  465. hasDisplayOrder.DisplayOrder = val;
  466. }
  467. }
  468. break;
  469. }
  470. case "year":
  471. {
  472. var val = reader.ReadElementContentAsString();
  473. if (!string.IsNullOrWhiteSpace(val))
  474. {
  475. int productionYear;
  476. if (int.TryParse(val, out productionYear) && productionYear > 1850)
  477. {
  478. item.ProductionYear = productionYear;
  479. }
  480. }
  481. break;
  482. }
  483. case "rating":
  484. {
  485. var rating = reader.ReadElementContentAsString();
  486. if (!string.IsNullOrWhiteSpace(rating))
  487. {
  488. float val;
  489. // All external meta is saving this as '.' for decimal I believe...but just to be sure
  490. if (float.TryParse(rating.Replace(',', '.'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out val))
  491. {
  492. item.CommunityRating = val;
  493. }
  494. }
  495. break;
  496. }
  497. case "aired":
  498. case "formed":
  499. case "premiered":
  500. case "releasedate":
  501. {
  502. var formatString = _config.GetNfoConfiguration().ReleaseDateFormat;
  503. var val = reader.ReadElementContentAsString();
  504. if (!string.IsNullOrWhiteSpace(val))
  505. {
  506. DateTime date;
  507. if (DateTime.TryParseExact(val, formatString, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out date) && date.Year > 1850)
  508. {
  509. item.PremiereDate = date.ToUniversalTime();
  510. item.ProductionYear = date.Year;
  511. }
  512. }
  513. break;
  514. }
  515. case "enddate":
  516. {
  517. var formatString = _config.GetNfoConfiguration().ReleaseDateFormat;
  518. var val = reader.ReadElementContentAsString();
  519. if (!string.IsNullOrWhiteSpace(val))
  520. {
  521. DateTime date;
  522. if (DateTime.TryParseExact(val, formatString, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out date) && date.Year > 1850)
  523. {
  524. item.EndDate = date.ToUniversalTime();
  525. }
  526. }
  527. break;
  528. }
  529. case "tvdbid":
  530. var tvdbId = reader.ReadElementContentAsString();
  531. if (!string.IsNullOrWhiteSpace(tvdbId))
  532. {
  533. item.SetProviderId(MetadataProviders.Tvdb, tvdbId);
  534. }
  535. break;
  536. case "votes":
  537. {
  538. var val = reader.ReadElementContentAsString();
  539. if (!string.IsNullOrWhiteSpace(val))
  540. {
  541. int num;
  542. if (int.TryParse(val, NumberStyles.Integer, _usCulture, out num))
  543. {
  544. item.VoteCount = num;
  545. }
  546. }
  547. break;
  548. }
  549. case "musicbrainzalbumid":
  550. {
  551. var mbz = reader.ReadElementContentAsString();
  552. if (!string.IsNullOrWhiteSpace(mbz))
  553. {
  554. item.SetProviderId(MetadataProviders.MusicBrainzAlbum, mbz);
  555. }
  556. break;
  557. }
  558. case "musicbrainzalbumartistid":
  559. {
  560. var mbz = reader.ReadElementContentAsString();
  561. if (!string.IsNullOrWhiteSpace(mbz))
  562. {
  563. item.SetProviderId(MetadataProviders.MusicBrainzAlbumArtist, mbz);
  564. }
  565. break;
  566. }
  567. case "musicbrainzartistid":
  568. {
  569. var mbz = reader.ReadElementContentAsString();
  570. if (!string.IsNullOrWhiteSpace(mbz))
  571. {
  572. item.SetProviderId(MetadataProviders.MusicBrainzArtist, mbz);
  573. }
  574. break;
  575. }
  576. case "musicbrainzreleasegroupid":
  577. {
  578. var mbz = reader.ReadElementContentAsString();
  579. if (!string.IsNullOrWhiteSpace(mbz))
  580. {
  581. item.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, mbz);
  582. }
  583. break;
  584. }
  585. case "tvrageid":
  586. {
  587. var id = reader.ReadElementContentAsString();
  588. if (!string.IsNullOrWhiteSpace(id))
  589. {
  590. item.SetProviderId(MetadataProviders.TvRage, id);
  591. }
  592. break;
  593. }
  594. case "audiodbartistid":
  595. {
  596. var id = reader.ReadElementContentAsString();
  597. if (!string.IsNullOrWhiteSpace(id))
  598. {
  599. item.SetProviderId(MetadataProviders.AudioDbArtist, id);
  600. }
  601. break;
  602. }
  603. case "audiodbalbumid":
  604. {
  605. var id = reader.ReadElementContentAsString();
  606. if (!string.IsNullOrWhiteSpace(id))
  607. {
  608. item.SetProviderId(MetadataProviders.AudioDbAlbum, id);
  609. }
  610. break;
  611. }
  612. case "rottentomatoesid":
  613. var rtId = reader.ReadElementContentAsString();
  614. if (!string.IsNullOrWhiteSpace(rtId))
  615. {
  616. item.SetProviderId(MetadataProviders.RottenTomatoes, rtId);
  617. }
  618. break;
  619. case "tmdbid":
  620. var tmdb = reader.ReadElementContentAsString();
  621. if (!string.IsNullOrWhiteSpace(tmdb))
  622. {
  623. item.SetProviderId(MetadataProviders.Tmdb, tmdb);
  624. }
  625. break;
  626. case "collectionnumber":
  627. var tmdbCollection = reader.ReadElementContentAsString();
  628. if (!string.IsNullOrWhiteSpace(tmdbCollection))
  629. {
  630. item.SetProviderId(MetadataProviders.TmdbCollection, tmdbCollection);
  631. }
  632. break;
  633. case "tvcomid":
  634. var TVcomId = reader.ReadElementContentAsString();
  635. if (!string.IsNullOrWhiteSpace(TVcomId))
  636. {
  637. item.SetProviderId(MetadataProviders.Tvcom, TVcomId);
  638. }
  639. break;
  640. case "zap2itid":
  641. var zap2ItId = reader.ReadElementContentAsString();
  642. if (!string.IsNullOrWhiteSpace(zap2ItId))
  643. {
  644. item.SetProviderId(MetadataProviders.Zap2It, zap2ItId);
  645. }
  646. break;
  647. case "imdb_id":
  648. case "imdbid":
  649. var imDbId = reader.ReadElementContentAsString();
  650. if (!string.IsNullOrWhiteSpace(imDbId))
  651. {
  652. item.SetProviderId(MetadataProviders.Imdb, imDbId);
  653. }
  654. break;
  655. case "genre":
  656. {
  657. var val = reader.ReadElementContentAsString();
  658. if (!string.IsNullOrWhiteSpace(val))
  659. {
  660. var parts = val.Split('/')
  661. .Select(i => i.Trim())
  662. .Where(i => !string.IsNullOrWhiteSpace(i));
  663. foreach (var p in parts)
  664. {
  665. item.AddGenre(p);
  666. }
  667. }
  668. break;
  669. }
  670. case "style":
  671. case "tag":
  672. {
  673. var val = reader.ReadElementContentAsString();
  674. if (!string.IsNullOrWhiteSpace(val))
  675. {
  676. var hasTags = item as IHasTags;
  677. if (hasTags != null)
  678. {
  679. hasTags.AddTag(val);
  680. }
  681. }
  682. break;
  683. }
  684. case "plotkeyword":
  685. {
  686. var val = reader.ReadElementContentAsString();
  687. var hasKeywords = item as IHasKeywords;
  688. if (hasKeywords != null)
  689. {
  690. if (!string.IsNullOrWhiteSpace(val))
  691. {
  692. hasKeywords.AddKeyword(val);
  693. }
  694. }
  695. break;
  696. }
  697. case "fileinfo":
  698. {
  699. using (var subtree = reader.ReadSubtree())
  700. {
  701. FetchFromFileInfoNode(subtree, item);
  702. }
  703. break;
  704. }
  705. case "watched":
  706. {
  707. var val = reader.ReadElementContentAsString();
  708. if (!string.IsNullOrWhiteSpace(val))
  709. {
  710. bool parsedValue;
  711. if (bool.TryParse(val, out parsedValue))
  712. {
  713. if (!string.IsNullOrWhiteSpace(userDataUserId))
  714. {
  715. var userData = GetOrAdd(userDataList, userDataUserId);
  716. userData.Played = parsedValue;
  717. }
  718. }
  719. }
  720. break;
  721. }
  722. case "playcount":
  723. {
  724. var val = reader.ReadElementContentAsString();
  725. if (!string.IsNullOrWhiteSpace(val))
  726. {
  727. int parsedValue;
  728. if (int.TryParse(val, NumberStyles.Integer, _usCulture, out parsedValue))
  729. {
  730. if (!string.IsNullOrWhiteSpace(userDataUserId))
  731. {
  732. var userData = GetOrAdd(userDataList, userDataUserId);
  733. userData.PlayCount = parsedValue;
  734. }
  735. }
  736. }
  737. break;
  738. }
  739. case "lastplayed":
  740. {
  741. var val = reader.ReadElementContentAsString();
  742. if (!string.IsNullOrWhiteSpace(val))
  743. {
  744. DateTime parsedValue;
  745. if (DateTime.TryParseExact(val, "yyyy-MM-dd HH:mm:ss", _usCulture, DateTimeStyles.None, out parsedValue))
  746. {
  747. if (!string.IsNullOrWhiteSpace(userDataUserId))
  748. {
  749. var userData = GetOrAdd(userDataList, userDataUserId);
  750. userData.LastPlayedDate = parsedValue;
  751. }
  752. }
  753. }
  754. break;
  755. }
  756. case "resume":
  757. {
  758. using (var subtree = reader.ReadSubtree())
  759. {
  760. if (!string.IsNullOrWhiteSpace(userDataUserId))
  761. {
  762. var userData = GetOrAdd(userDataList, userDataUserId);
  763. FetchFromResumeNode(subtree, item, userData);
  764. }
  765. }
  766. break;
  767. }
  768. case "isuserfavorite":
  769. {
  770. var val = reader.ReadElementContentAsString();
  771. if (!string.IsNullOrWhiteSpace(val))
  772. {
  773. bool parsedValue;
  774. if (bool.TryParse(val, out parsedValue))
  775. {
  776. if (!string.IsNullOrWhiteSpace(userDataUserId))
  777. {
  778. var userData = GetOrAdd(userDataList, userDataUserId);
  779. userData.IsFavorite = parsedValue;
  780. }
  781. }
  782. }
  783. break;
  784. }
  785. case "userrating":
  786. {
  787. var val = reader.ReadElementContentAsString();
  788. if (!string.IsNullOrWhiteSpace(val))
  789. {
  790. double parsedValue;
  791. if (double.TryParse(val, NumberStyles.Any, _usCulture, out parsedValue))
  792. {
  793. if (!string.IsNullOrWhiteSpace(userDataUserId))
  794. {
  795. var userData = GetOrAdd(userDataList, userDataUserId);
  796. userData.Rating = parsedValue;
  797. }
  798. }
  799. }
  800. break;
  801. }
  802. default:
  803. reader.Skip();
  804. break;
  805. }
  806. }
  807. private UserItemData GetOrAdd(List<UserItemData> userDataList, string userId)
  808. {
  809. var userData = userDataList.FirstOrDefault(i => string.Equals(userId, i.UserId.ToString("N"), StringComparison.OrdinalIgnoreCase));
  810. if (userData == null)
  811. {
  812. userData = new UserItemData()
  813. {
  814. UserId = new Guid(userId)
  815. };
  816. userDataList.Add(userData);
  817. }
  818. return userData;
  819. }
  820. private void FetchFromResumeNode(XmlReader reader, T item, UserItemData userData)
  821. {
  822. reader.MoveToContent();
  823. while (reader.Read())
  824. {
  825. if (reader.NodeType == XmlNodeType.Element)
  826. {
  827. switch (reader.Name)
  828. {
  829. case "position":
  830. {
  831. var val = reader.ReadElementContentAsString();
  832. if (!string.IsNullOrWhiteSpace(val))
  833. {
  834. double parsedValue;
  835. if (double.TryParse(val, NumberStyles.Any, _usCulture, out parsedValue))
  836. {
  837. userData.PlaybackPositionTicks = TimeSpan.FromSeconds(parsedValue).Ticks;
  838. }
  839. }
  840. break;
  841. }
  842. default:
  843. reader.Skip();
  844. break;
  845. }
  846. }
  847. }
  848. }
  849. private void FetchFromFileInfoNode(XmlReader reader, T item)
  850. {
  851. reader.MoveToContent();
  852. while (reader.Read())
  853. {
  854. if (reader.NodeType == XmlNodeType.Element)
  855. {
  856. switch (reader.Name)
  857. {
  858. case "streamdetails":
  859. {
  860. using (var subtree = reader.ReadSubtree())
  861. {
  862. FetchFromStreamDetailsNode(subtree, item);
  863. }
  864. break;
  865. }
  866. default:
  867. reader.Skip();
  868. break;
  869. }
  870. }
  871. }
  872. }
  873. private void FetchFromStreamDetailsNode(XmlReader reader, T item)
  874. {
  875. reader.MoveToContent();
  876. while (reader.Read())
  877. {
  878. if (reader.NodeType == XmlNodeType.Element)
  879. {
  880. switch (reader.Name)
  881. {
  882. case "video":
  883. {
  884. using (var subtree = reader.ReadSubtree())
  885. {
  886. FetchFromVideoNode(subtree, item);
  887. }
  888. break;
  889. }
  890. default:
  891. reader.Skip();
  892. break;
  893. }
  894. }
  895. }
  896. }
  897. private void FetchFromVideoNode(XmlReader reader, T item)
  898. {
  899. reader.MoveToContent();
  900. while (reader.Read())
  901. {
  902. if (reader.NodeType == XmlNodeType.Element)
  903. {
  904. switch (reader.Name)
  905. {
  906. case "format3d":
  907. {
  908. var video = item as Video;
  909. if (video != null)
  910. {
  911. var val = reader.ReadElementContentAsString();
  912. if (string.Equals("HSBS", val, StringComparison.CurrentCulture))
  913. {
  914. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  915. }
  916. else if (string.Equals("HTAB", val, StringComparison.CurrentCulture))
  917. {
  918. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  919. }
  920. else if (string.Equals("FTAB", val, StringComparison.CurrentCulture))
  921. {
  922. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  923. }
  924. else if (string.Equals("FSBS", val, StringComparison.CurrentCulture))
  925. {
  926. video.Video3DFormat = Video3DFormat.FullSideBySide;
  927. }
  928. }
  929. break;
  930. }
  931. default:
  932. reader.Skip();
  933. break;
  934. }
  935. }
  936. }
  937. }
  938. /// <summary>
  939. /// Gets the persons from XML node.
  940. /// </summary>
  941. /// <param name="reader">The reader.</param>
  942. /// <returns>IEnumerable{PersonInfo}.</returns>
  943. private PersonInfo GetPersonFromXmlNode(XmlReader reader)
  944. {
  945. var name = string.Empty;
  946. var type = PersonType.Actor; // If type is not specified assume actor
  947. var role = string.Empty;
  948. int? sortOrder = null;
  949. reader.MoveToContent();
  950. while (reader.Read())
  951. {
  952. if (reader.NodeType == XmlNodeType.Element)
  953. {
  954. switch (reader.Name)
  955. {
  956. case "name":
  957. name = reader.ReadElementContentAsString() ?? string.Empty;
  958. break;
  959. case "type":
  960. {
  961. var val = reader.ReadElementContentAsString();
  962. if (!string.IsNullOrWhiteSpace(val))
  963. {
  964. type = val;
  965. }
  966. break;
  967. }
  968. case "role":
  969. {
  970. var val = reader.ReadElementContentAsString();
  971. if (!string.IsNullOrWhiteSpace(val))
  972. {
  973. role = val;
  974. }
  975. break;
  976. }
  977. case "sortorder":
  978. {
  979. var val = reader.ReadElementContentAsString();
  980. if (!string.IsNullOrWhiteSpace(val))
  981. {
  982. int intVal;
  983. if (int.TryParse(val, NumberStyles.Integer, _usCulture, out intVal))
  984. {
  985. sortOrder = intVal;
  986. }
  987. }
  988. break;
  989. }
  990. default:
  991. reader.Skip();
  992. break;
  993. }
  994. }
  995. }
  996. return new PersonInfo
  997. {
  998. Name = name.Trim(),
  999. Role = role,
  1000. Type = type,
  1001. SortOrder = sortOrder
  1002. };
  1003. }
  1004. /// <summary>
  1005. /// Used to split names of comma or pipe delimeted genres and people
  1006. /// </summary>
  1007. /// <param name="value">The value.</param>
  1008. /// <returns>IEnumerable{System.String}.</returns>
  1009. private IEnumerable<string> SplitNames(string value)
  1010. {
  1011. value = value ?? string.Empty;
  1012. // Only split by comma if there is no pipe in the string
  1013. // We have to be careful to not split names like Matthew, Jr.
  1014. var separator = value.IndexOf('|') == -1 && value.IndexOf(';') == -1 ? new[] { ',' } : new[] { '|', ';' };
  1015. value = value.Trim().Trim(separator);
  1016. return string.IsNullOrWhiteSpace(value) ? new string[] { } : Split(value, separator, StringSplitOptions.RemoveEmptyEntries);
  1017. }
  1018. /// <summary>
  1019. /// Provides an additional overload for string.split
  1020. /// </summary>
  1021. /// <param name="val">The val.</param>
  1022. /// <param name="separators">The separators.</param>
  1023. /// <param name="options">The options.</param>
  1024. /// <returns>System.String[][].</returns>
  1025. private static string[] Split(string val, char[] separators, StringSplitOptions options)
  1026. {
  1027. return val.Split(separators, options);
  1028. }
  1029. }
  1030. }