BaseNfoParser.cs 47 KB

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