BaseNfoParser.cs 45 KB

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