BaseNfoParser.cs 43 KB

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