BaseItemXmlParser.cs 46 KB

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