BaseNfoParser.cs 46 KB

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