BaseItemXmlParser.cs 46 KB

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