BaseNfoParser.cs 47 KB

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