BaseNfoParser.cs 46 KB

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