BaseNfoParser.cs 45 KB

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