BaseNfoParser.cs 45 KB

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