BaseItemXmlParser.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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. if (!string.IsNullOrWhiteSpace(val))
  217. {
  218. item.ForcedSortName = val;
  219. }
  220. break;
  221. }
  222. case "Overview":
  223. case "Description":
  224. {
  225. var val = reader.ReadElementContentAsString();
  226. if (!string.IsNullOrWhiteSpace(val))
  227. {
  228. item.Overview = val;
  229. }
  230. break;
  231. }
  232. case "CriticRatingSummary":
  233. {
  234. var val = reader.ReadElementContentAsString();
  235. if (!string.IsNullOrWhiteSpace(val))
  236. {
  237. var hasCriticRating = item as IHasCriticRating;
  238. if (hasCriticRating != null)
  239. {
  240. hasCriticRating.CriticRatingSummary = val;
  241. }
  242. }
  243. break;
  244. }
  245. case "TagLine":
  246. {
  247. var tagline = reader.ReadElementContentAsString();
  248. var hasTaglines = item as IHasTaglines;
  249. if (hasTaglines != null)
  250. {
  251. if (!string.IsNullOrWhiteSpace(tagline))
  252. {
  253. hasTaglines.AddTagline(tagline);
  254. }
  255. }
  256. break;
  257. }
  258. case "Language":
  259. {
  260. var val = reader.ReadElementContentAsString();
  261. var hasLanguage = item as IHasPreferredMetadataLanguage;
  262. if (hasLanguage != null)
  263. {
  264. hasLanguage.PreferredMetadataLanguage = val;
  265. }
  266. break;
  267. }
  268. case "PlaceOfBirth":
  269. {
  270. var val = reader.ReadElementContentAsString();
  271. if (!string.IsNullOrWhiteSpace(val))
  272. {
  273. var person = item as Person;
  274. if (person != null)
  275. {
  276. person.PlaceOfBirth = val;
  277. }
  278. }
  279. break;
  280. }
  281. case "Website":
  282. {
  283. var val = reader.ReadElementContentAsString();
  284. if (!string.IsNullOrWhiteSpace(val))
  285. {
  286. item.HomePageUrl = val;
  287. }
  288. break;
  289. }
  290. case "LockedFields":
  291. {
  292. var fields = new List<MetadataFields>();
  293. var val = reader.ReadElementContentAsString();
  294. if (!string.IsNullOrWhiteSpace(val))
  295. {
  296. var list = val.Split('|').Select(i =>
  297. {
  298. MetadataFields field;
  299. if (Enum.TryParse<MetadataFields>(i, true, out field))
  300. {
  301. return (MetadataFields?)field;
  302. }
  303. return null;
  304. }).Where(i => i.HasValue).Select(i => i.Value);
  305. fields.AddRange(list);
  306. }
  307. item.LockedFields = fields;
  308. break;
  309. }
  310. case "TagLines":
  311. {
  312. using (var subtree = reader.ReadSubtree())
  313. {
  314. FetchFromTaglinesNode(subtree, item);
  315. }
  316. break;
  317. }
  318. case "ContentRating":
  319. case "certification":
  320. case "MPAARating":
  321. case "ESRBRating":
  322. {
  323. var rating = reader.ReadElementContentAsString();
  324. if (!string.IsNullOrWhiteSpace(rating))
  325. {
  326. item.OfficialRating = rating;
  327. }
  328. break;
  329. }
  330. case "MPAADescription":
  331. {
  332. var rating = reader.ReadElementContentAsString();
  333. if (!string.IsNullOrWhiteSpace(rating))
  334. {
  335. item.OfficialRatingDescription = rating;
  336. }
  337. break;
  338. }
  339. case "CustomRating":
  340. {
  341. var val = reader.ReadElementContentAsString();
  342. if (!string.IsNullOrWhiteSpace(val))
  343. {
  344. item.CustomRating = val;
  345. }
  346. break;
  347. }
  348. case "Runtime":
  349. case "RunningTime":
  350. {
  351. var text = reader.ReadElementContentAsString();
  352. if (!string.IsNullOrWhiteSpace(text))
  353. {
  354. int runtime;
  355. if (int.TryParse(text.Split(' ')[0], NumberStyles.Integer, _usCulture, out runtime))
  356. {
  357. item.RunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
  358. }
  359. }
  360. break;
  361. }
  362. case "Genre":
  363. {
  364. foreach (var name in SplitNames(reader.ReadElementContentAsString()))
  365. {
  366. if (string.IsNullOrWhiteSpace(name))
  367. {
  368. continue;
  369. }
  370. item.AddGenre(name);
  371. }
  372. break;
  373. }
  374. case "AspectRatio":
  375. {
  376. var val = reader.ReadElementContentAsString();
  377. var hasAspectRatio = item as IHasAspectRatio;
  378. if (!string.IsNullOrWhiteSpace(val) && hasAspectRatio != null)
  379. {
  380. hasAspectRatio.AspectRatio = val;
  381. }
  382. break;
  383. }
  384. case "LockData":
  385. {
  386. var val = reader.ReadElementContentAsString();
  387. if (!string.IsNullOrWhiteSpace(val))
  388. {
  389. item.DontFetchMeta = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  390. }
  391. break;
  392. }
  393. case "Network":
  394. {
  395. foreach (var name in SplitNames(reader.ReadElementContentAsString()))
  396. {
  397. if (string.IsNullOrWhiteSpace(name))
  398. {
  399. continue;
  400. }
  401. item.AddStudio(name);
  402. }
  403. break;
  404. }
  405. case "Director":
  406. {
  407. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new Entities.PersonInfo { Name = v.Trim(), Type = PersonType.Director }))
  408. {
  409. if (string.IsNullOrWhiteSpace(p.Name))
  410. {
  411. continue;
  412. }
  413. item.AddPerson(p);
  414. }
  415. break;
  416. }
  417. case "Writer":
  418. {
  419. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new Entities.PersonInfo { Name = v.Trim(), Type = PersonType.Writer }))
  420. {
  421. if (string.IsNullOrWhiteSpace(p.Name))
  422. {
  423. continue;
  424. }
  425. item.AddPerson(p);
  426. }
  427. break;
  428. }
  429. case "Actors":
  430. {
  431. var actors = reader.ReadInnerXml();
  432. if (actors.Contains("<"))
  433. {
  434. // This is one of the mis-named "Actors" full nodes created by MB2
  435. // Create a reader and pass it to the persons node processor
  436. FetchDataFromPersonsNode(new XmlTextReader(new StringReader("<Persons>" + actors + "</Persons>")), item);
  437. }
  438. else
  439. {
  440. // Old-style piped string
  441. foreach (var p in SplitNames(actors).Select(v => new Entities.PersonInfo { Name = v.Trim(), Type = PersonType.Actor }))
  442. {
  443. if (string.IsNullOrWhiteSpace(p.Name))
  444. {
  445. continue;
  446. }
  447. item.AddPerson(p);
  448. }
  449. }
  450. break;
  451. }
  452. case "GuestStars":
  453. {
  454. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new Entities.PersonInfo { Name = v.Trim(), Type = PersonType.GuestStar }))
  455. {
  456. if (string.IsNullOrWhiteSpace(p.Name))
  457. {
  458. continue;
  459. }
  460. item.AddPerson(p);
  461. }
  462. break;
  463. }
  464. case "Trailer":
  465. {
  466. var val = reader.ReadElementContentAsString();
  467. var hasTrailers = item as IHasTrailers;
  468. if (hasTrailers != null)
  469. {
  470. if (!string.IsNullOrWhiteSpace(val))
  471. {
  472. hasTrailers.AddTrailerUrl(val, false);
  473. }
  474. }
  475. break;
  476. }
  477. case "DisplayOrder":
  478. {
  479. var val = reader.ReadElementContentAsString();
  480. var hasDisplayOrder = item as IHasDisplayOrder;
  481. if (hasDisplayOrder != null)
  482. {
  483. if (!string.IsNullOrWhiteSpace(val))
  484. {
  485. hasDisplayOrder.DisplayOrder = val;
  486. }
  487. }
  488. break;
  489. }
  490. case "Trailers":
  491. {
  492. using (var subtree = reader.ReadSubtree())
  493. {
  494. var hasTrailers = item as IHasTrailers;
  495. if (hasTrailers != null)
  496. {
  497. FetchDataFromTrailersNode(subtree, hasTrailers);
  498. }
  499. }
  500. break;
  501. }
  502. case "ReleaseYear":
  503. case "ProductionYear":
  504. {
  505. var val = reader.ReadElementContentAsString();
  506. if (!string.IsNullOrWhiteSpace(val))
  507. {
  508. int productionYear;
  509. if (int.TryParse(val, out productionYear) && productionYear > 1850)
  510. {
  511. item.ProductionYear = productionYear;
  512. }
  513. }
  514. break;
  515. }
  516. case "Rating":
  517. case "IMDBrating":
  518. case "TGDBRating":
  519. {
  520. var rating = reader.ReadElementContentAsString();
  521. if (!string.IsNullOrWhiteSpace(rating))
  522. {
  523. float val;
  524. // All external meta is saving this as '.' for decimal I believe...but just to be sure
  525. if (float.TryParse(rating.Replace(',', '.'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out val))
  526. {
  527. item.CommunityRating = val;
  528. }
  529. }
  530. break;
  531. }
  532. case "BirthDate":
  533. case "PremiereDate":
  534. case "FirstAired":
  535. {
  536. var firstAired = reader.ReadElementContentAsString();
  537. if (!string.IsNullOrWhiteSpace(firstAired))
  538. {
  539. DateTime airDate;
  540. if (DateTime.TryParseExact(firstAired, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out airDate) && airDate.Year > 1850)
  541. {
  542. item.PremiereDate = airDate.ToUniversalTime();
  543. item.ProductionYear = airDate.Year;
  544. }
  545. }
  546. break;
  547. }
  548. case "DeathDate":
  549. case "EndDate":
  550. {
  551. var firstAired = reader.ReadElementContentAsString();
  552. if (!string.IsNullOrWhiteSpace(firstAired))
  553. {
  554. DateTime airDate;
  555. if (DateTime.TryParseExact(firstAired, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out airDate) && airDate.Year > 1850)
  556. {
  557. item.EndDate = airDate.ToUniversalTime();
  558. }
  559. }
  560. break;
  561. }
  562. case "TvDbId":
  563. var tvdbId = reader.ReadElementContentAsString();
  564. if (!string.IsNullOrWhiteSpace(tvdbId))
  565. {
  566. item.SetProviderId(MetadataProviders.Tvdb, tvdbId);
  567. }
  568. break;
  569. case "VoteCount":
  570. {
  571. var val = reader.ReadElementContentAsString();
  572. if (!string.IsNullOrWhiteSpace(val))
  573. {
  574. int num;
  575. if (int.TryParse(val, NumberStyles.Integer, _usCulture, out num))
  576. {
  577. item.VoteCount = num;
  578. }
  579. }
  580. break;
  581. }
  582. case "MusicbrainzId":
  583. {
  584. var mbz = reader.ReadElementContentAsString();
  585. if (!string.IsNullOrWhiteSpace(mbz))
  586. {
  587. if (item is MusicAlbum)
  588. {
  589. item.SetProviderId(MetadataProviders.MusicBrainzAlbum, mbz);
  590. }
  591. else if (item is MusicArtist)
  592. {
  593. item.SetProviderId(MetadataProviders.MusicBrainzArtist, mbz);
  594. }
  595. }
  596. break;
  597. }
  598. case "MusicBrainzAlbumId":
  599. {
  600. var mbz = reader.ReadElementContentAsString();
  601. if (!string.IsNullOrWhiteSpace(mbz))
  602. {
  603. item.SetProviderId(MetadataProviders.MusicBrainzAlbum, mbz);
  604. }
  605. break;
  606. }
  607. case "MusicBrainzAlbumArtistId":
  608. {
  609. var mbz = reader.ReadElementContentAsString();
  610. if (!string.IsNullOrWhiteSpace(mbz))
  611. {
  612. item.SetProviderId(MetadataProviders.MusicBrainzAlbumArtist, mbz);
  613. }
  614. break;
  615. }
  616. case "MusicBrainzArtistId":
  617. {
  618. var mbz = reader.ReadElementContentAsString();
  619. if (!string.IsNullOrWhiteSpace(mbz))
  620. {
  621. item.SetProviderId(MetadataProviders.MusicBrainzArtist, mbz);
  622. }
  623. break;
  624. }
  625. case "MusicBrainzReleaseGroupId":
  626. {
  627. var mbz = reader.ReadElementContentAsString();
  628. if (!string.IsNullOrWhiteSpace(mbz))
  629. {
  630. item.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, mbz);
  631. }
  632. break;
  633. }
  634. case "TVRageId":
  635. {
  636. var id = reader.ReadElementContentAsString();
  637. if (!string.IsNullOrWhiteSpace(id))
  638. {
  639. item.SetProviderId(MetadataProviders.TvRage, id);
  640. }
  641. break;
  642. }
  643. case "AudioDbArtistId":
  644. {
  645. var id = reader.ReadElementContentAsString();
  646. if (!string.IsNullOrWhiteSpace(id))
  647. {
  648. item.SetProviderId(MetadataProviders.AudioDbArtist, id);
  649. }
  650. break;
  651. }
  652. case "AudioDbAlbumId":
  653. {
  654. var id = reader.ReadElementContentAsString();
  655. if (!string.IsNullOrWhiteSpace(id))
  656. {
  657. item.SetProviderId(MetadataProviders.AudioDbAlbum, id);
  658. }
  659. break;
  660. }
  661. case "RottenTomatoesId":
  662. var rtId = reader.ReadElementContentAsString();
  663. if (!string.IsNullOrWhiteSpace(rtId))
  664. {
  665. item.SetProviderId(MetadataProviders.RottenTomatoes, rtId);
  666. }
  667. break;
  668. case "TMDbId":
  669. var tmdb = reader.ReadElementContentAsString();
  670. if (!string.IsNullOrWhiteSpace(tmdb))
  671. {
  672. item.SetProviderId(MetadataProviders.Tmdb, tmdb);
  673. }
  674. break;
  675. case "TMDbCollectionId":
  676. var tmdbCollection = reader.ReadElementContentAsString();
  677. if (!string.IsNullOrWhiteSpace(tmdbCollection))
  678. {
  679. item.SetProviderId(MetadataProviders.TmdbCollection, tmdbCollection);
  680. }
  681. break;
  682. case "TVcomId":
  683. var TVcomId = reader.ReadElementContentAsString();
  684. if (!string.IsNullOrWhiteSpace(TVcomId))
  685. {
  686. item.SetProviderId(MetadataProviders.Tvcom, TVcomId);
  687. }
  688. break;
  689. case "Zap2ItId":
  690. var zap2ItId = reader.ReadElementContentAsString();
  691. if (!string.IsNullOrWhiteSpace(zap2ItId))
  692. {
  693. item.SetProviderId(MetadataProviders.Zap2It, zap2ItId);
  694. }
  695. break;
  696. case "IMDB_ID":
  697. case "IMDB":
  698. case "IMDbId":
  699. var imDbId = reader.ReadElementContentAsString();
  700. if (!string.IsNullOrWhiteSpace(imDbId))
  701. {
  702. item.SetProviderId(MetadataProviders.Imdb, imDbId);
  703. }
  704. break;
  705. case "Genres":
  706. {
  707. using (var subtree = reader.ReadSubtree())
  708. {
  709. FetchFromGenresNode(subtree, item);
  710. }
  711. break;
  712. }
  713. case "Tags":
  714. {
  715. using (var subtree = reader.ReadSubtree())
  716. {
  717. var hasTags = item as IHasTags;
  718. if (hasTags != null)
  719. {
  720. FetchFromTagsNode(subtree, hasTags);
  721. }
  722. }
  723. break;
  724. }
  725. case "PlotKeywords":
  726. {
  727. using (var subtree = reader.ReadSubtree())
  728. {
  729. var hasTags = item as IHasKeywords;
  730. if (hasTags != null)
  731. {
  732. FetchFromKeywordsNode(subtree, hasTags);
  733. }
  734. }
  735. break;
  736. }
  737. case "Persons":
  738. {
  739. using (var subtree = reader.ReadSubtree())
  740. {
  741. FetchDataFromPersonsNode(subtree, item);
  742. }
  743. break;
  744. }
  745. case "ParentalRating":
  746. {
  747. using (var subtree = reader.ReadSubtree())
  748. {
  749. FetchFromParentalRatingNode(subtree, item);
  750. }
  751. break;
  752. }
  753. case "Studios":
  754. {
  755. using (var subtree = reader.ReadSubtree())
  756. {
  757. FetchFromStudiosNode(subtree, item);
  758. }
  759. break;
  760. }
  761. case "Format3D":
  762. {
  763. var video = item as Video;
  764. if (video != null)
  765. {
  766. var val = reader.ReadElementContentAsString();
  767. if (string.Equals("HSBS", val))
  768. {
  769. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  770. }
  771. else if (string.Equals("HTAB", val))
  772. {
  773. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  774. }
  775. else if (string.Equals("FTAB", val))
  776. {
  777. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  778. }
  779. else if (string.Equals("FSBS", val))
  780. {
  781. video.Video3DFormat = Video3DFormat.FullSideBySide;
  782. }
  783. }
  784. break;
  785. }
  786. default:
  787. reader.Skip();
  788. break;
  789. }
  790. }
  791. /// <summary>
  792. /// Fetches from taglines node.
  793. /// </summary>
  794. /// <param name="reader">The reader.</param>
  795. /// <param name="item">The item.</param>
  796. private void FetchFromTaglinesNode(XmlReader reader, T item)
  797. {
  798. reader.MoveToContent();
  799. while (reader.Read())
  800. {
  801. if (reader.NodeType == XmlNodeType.Element)
  802. {
  803. switch (reader.Name)
  804. {
  805. case "Tagline":
  806. {
  807. var val = reader.ReadElementContentAsString();
  808. if (!string.IsNullOrWhiteSpace(val))
  809. {
  810. var hasTaglines = item as IHasTaglines;
  811. if (hasTaglines != null)
  812. {
  813. if (!string.IsNullOrWhiteSpace(val))
  814. {
  815. hasTaglines.AddTagline(val);
  816. }
  817. }
  818. }
  819. break;
  820. }
  821. default:
  822. reader.Skip();
  823. break;
  824. }
  825. }
  826. }
  827. }
  828. /// <summary>
  829. /// Fetches from genres node.
  830. /// </summary>
  831. /// <param name="reader">The reader.</param>
  832. /// <param name="item">The item.</param>
  833. private void FetchFromGenresNode(XmlReader reader, T item)
  834. {
  835. reader.MoveToContent();
  836. while (reader.Read())
  837. {
  838. if (reader.NodeType == XmlNodeType.Element)
  839. {
  840. switch (reader.Name)
  841. {
  842. case "Genre":
  843. {
  844. var genre = reader.ReadElementContentAsString();
  845. if (!string.IsNullOrWhiteSpace(genre))
  846. {
  847. item.AddGenre(genre);
  848. }
  849. break;
  850. }
  851. default:
  852. reader.Skip();
  853. break;
  854. }
  855. }
  856. }
  857. }
  858. private void FetchFromTagsNode(XmlReader reader, IHasTags item)
  859. {
  860. reader.MoveToContent();
  861. while (reader.Read())
  862. {
  863. if (reader.NodeType == XmlNodeType.Element)
  864. {
  865. switch (reader.Name)
  866. {
  867. case "Tag":
  868. {
  869. var tag = reader.ReadElementContentAsString();
  870. if (!string.IsNullOrWhiteSpace(tag))
  871. {
  872. item.AddTag(tag);
  873. }
  874. break;
  875. }
  876. default:
  877. reader.Skip();
  878. break;
  879. }
  880. }
  881. }
  882. }
  883. private void FetchFromKeywordsNode(XmlReader reader, IHasKeywords item)
  884. {
  885. reader.MoveToContent();
  886. while (reader.Read())
  887. {
  888. if (reader.NodeType == XmlNodeType.Element)
  889. {
  890. switch (reader.Name)
  891. {
  892. case "PlotKeyword":
  893. {
  894. var tag = reader.ReadElementContentAsString();
  895. if (!string.IsNullOrWhiteSpace(tag))
  896. {
  897. item.AddKeyword(tag);
  898. }
  899. break;
  900. }
  901. default:
  902. reader.Skip();
  903. break;
  904. }
  905. }
  906. }
  907. }
  908. /// <summary>
  909. /// Fetches the data from persons node.
  910. /// </summary>
  911. /// <param name="reader">The reader.</param>
  912. /// <param name="item">The item.</param>
  913. private void FetchDataFromPersonsNode(XmlReader reader, T item)
  914. {
  915. reader.MoveToContent();
  916. while (reader.Read())
  917. {
  918. if (reader.NodeType == XmlNodeType.Element)
  919. {
  920. switch (reader.Name)
  921. {
  922. case "Person":
  923. case "Actor":
  924. {
  925. using (var subtree = reader.ReadSubtree())
  926. {
  927. foreach (var person in GetPersonsFromXmlNode(subtree))
  928. {
  929. if (string.IsNullOrWhiteSpace(person.Name))
  930. {
  931. continue;
  932. }
  933. item.AddPerson(person);
  934. }
  935. }
  936. break;
  937. }
  938. default:
  939. reader.Skip();
  940. break;
  941. }
  942. }
  943. }
  944. }
  945. private void FetchDataFromTrailersNode(XmlReader reader, IHasTrailers item)
  946. {
  947. reader.MoveToContent();
  948. while (reader.Read())
  949. {
  950. if (reader.NodeType == XmlNodeType.Element)
  951. {
  952. switch (reader.Name)
  953. {
  954. case "Trailer":
  955. {
  956. var val = reader.ReadElementContentAsString();
  957. if (!string.IsNullOrWhiteSpace(val))
  958. {
  959. item.AddTrailerUrl(val, false);
  960. }
  961. break;
  962. }
  963. default:
  964. reader.Skip();
  965. break;
  966. }
  967. }
  968. }
  969. }
  970. protected async Task FetchChaptersFromXmlNode(BaseItem item, XmlReader reader, IItemRepository repository, CancellationToken cancellationToken)
  971. {
  972. var runtime = item.RunTimeTicks ?? 0;
  973. using (reader)
  974. {
  975. var chapters = GetChaptersFromXmlNode(reader)
  976. .Where(i => i.StartPositionTicks >= 0 && i.StartPositionTicks < runtime);
  977. await repository.SaveChapters(item.Id, chapters, cancellationToken).ConfigureAwait(false);
  978. }
  979. }
  980. private IEnumerable<ChapterInfo> GetChaptersFromXmlNode(XmlReader reader)
  981. {
  982. var chapters = new List<ChapterInfo>();
  983. reader.MoveToContent();
  984. while (reader.Read())
  985. {
  986. if (reader.NodeType == XmlNodeType.Element)
  987. {
  988. switch (reader.Name)
  989. {
  990. case "Chapter":
  991. {
  992. using (var subtree = reader.ReadSubtree())
  993. {
  994. chapters.Add(GetChapterInfoFromXmlNode(subtree));
  995. }
  996. break;
  997. }
  998. default:
  999. reader.Skip();
  1000. break;
  1001. }
  1002. }
  1003. }
  1004. return chapters;
  1005. }
  1006. private ChapterInfo GetChapterInfoFromXmlNode(XmlReader reader)
  1007. {
  1008. var chapter = new ChapterInfo();
  1009. reader.MoveToContent();
  1010. while (reader.Read())
  1011. {
  1012. if (reader.NodeType == XmlNodeType.Element)
  1013. {
  1014. switch (reader.Name)
  1015. {
  1016. case "StartPositionMs":
  1017. {
  1018. var val = reader.ReadElementContentAsString();
  1019. var ms = long.Parse(val, _usCulture);
  1020. chapter.StartPositionTicks = TimeSpan.FromMilliseconds(ms).Ticks;
  1021. break;
  1022. }
  1023. case "Name":
  1024. {
  1025. chapter.Name = reader.ReadElementContentAsString();
  1026. break;
  1027. }
  1028. default:
  1029. reader.Skip();
  1030. break;
  1031. }
  1032. }
  1033. }
  1034. return chapter;
  1035. }
  1036. /// <summary>
  1037. /// Fetches from studios node.
  1038. /// </summary>
  1039. /// <param name="reader">The reader.</param>
  1040. /// <param name="item">The item.</param>
  1041. private void FetchFromStudiosNode(XmlReader reader, T item)
  1042. {
  1043. reader.MoveToContent();
  1044. while (reader.Read())
  1045. {
  1046. if (reader.NodeType == XmlNodeType.Element)
  1047. {
  1048. switch (reader.Name)
  1049. {
  1050. case "Studio":
  1051. {
  1052. var studio = reader.ReadElementContentAsString();
  1053. if (!string.IsNullOrWhiteSpace(studio))
  1054. {
  1055. item.AddStudio(studio);
  1056. }
  1057. break;
  1058. }
  1059. default:
  1060. reader.Skip();
  1061. break;
  1062. }
  1063. }
  1064. }
  1065. }
  1066. /// <summary>
  1067. /// Fetches from parental rating node.
  1068. /// </summary>
  1069. /// <param name="reader">The reader.</param>
  1070. /// <param name="item">The item.</param>
  1071. private void FetchFromParentalRatingNode(XmlReader reader, T item)
  1072. {
  1073. reader.MoveToContent();
  1074. while (reader.Read())
  1075. {
  1076. if (reader.NodeType == XmlNodeType.Element)
  1077. {
  1078. switch (reader.Name)
  1079. {
  1080. // Removed support for "Value" tag as it conflicted with MPAA rating but leaving this function for possible
  1081. // future support of "Description" -ebr
  1082. default:
  1083. reader.Skip();
  1084. break;
  1085. }
  1086. }
  1087. }
  1088. }
  1089. /// <summary>
  1090. /// Gets the persons from XML node.
  1091. /// </summary>
  1092. /// <param name="reader">The reader.</param>
  1093. /// <returns>IEnumerable{PersonInfo}.</returns>
  1094. private IEnumerable<Entities.PersonInfo> GetPersonsFromXmlNode(XmlReader reader)
  1095. {
  1096. var name = string.Empty;
  1097. var type = "Actor"; // If type is not specified assume actor
  1098. var role = string.Empty;
  1099. int? sortOrder = null;
  1100. reader.MoveToContent();
  1101. while (reader.Read())
  1102. {
  1103. if (reader.NodeType == XmlNodeType.Element)
  1104. {
  1105. switch (reader.Name)
  1106. {
  1107. case "Name":
  1108. name = reader.ReadElementContentAsString() ?? string.Empty;
  1109. break;
  1110. case "Type":
  1111. {
  1112. var val = reader.ReadElementContentAsString();
  1113. if (!string.IsNullOrWhiteSpace(val))
  1114. {
  1115. type = val;
  1116. }
  1117. break;
  1118. }
  1119. case "Role":
  1120. {
  1121. var val = reader.ReadElementContentAsString();
  1122. if (!string.IsNullOrWhiteSpace(val))
  1123. {
  1124. role = val;
  1125. }
  1126. break;
  1127. }
  1128. case "SortOrder":
  1129. {
  1130. var val = reader.ReadElementContentAsString();
  1131. if (!string.IsNullOrWhiteSpace(val))
  1132. {
  1133. int intVal;
  1134. if (int.TryParse(val, NumberStyles.Integer, _usCulture, out intVal))
  1135. {
  1136. sortOrder = intVal;
  1137. }
  1138. }
  1139. break;
  1140. }
  1141. default:
  1142. reader.Skip();
  1143. break;
  1144. }
  1145. }
  1146. }
  1147. var personInfo = new Entities.PersonInfo
  1148. {
  1149. Name = name.Trim(),
  1150. Role = role,
  1151. Type = type,
  1152. SortOrder = sortOrder
  1153. };
  1154. return new[] { personInfo };
  1155. }
  1156. /// <summary>
  1157. /// Used to split names of comma or pipe delimeted genres and people
  1158. /// </summary>
  1159. /// <param name="value">The value.</param>
  1160. /// <returns>IEnumerable{System.String}.</returns>
  1161. private IEnumerable<string> SplitNames(string value)
  1162. {
  1163. value = value ?? string.Empty;
  1164. // Only split by comma if there is no pipe in the string
  1165. // We have to be careful to not split names like Matthew, Jr.
  1166. var separator = value.IndexOf('|') == -1 && value.IndexOf(';') == -1 ? new[] { ',' } : new[] { '|', ';' };
  1167. value = value.Trim().Trim(separator);
  1168. return string.IsNullOrWhiteSpace(value) ? new string[] { } : Split(value, separator, StringSplitOptions.RemoveEmptyEntries);
  1169. }
  1170. /// <summary>
  1171. /// Provides an additional overload for string.split
  1172. /// </summary>
  1173. /// <param name="val">The val.</param>
  1174. /// <param name="separators">The separators.</param>
  1175. /// <param name="options">The options.</param>
  1176. /// <returns>System.String[][].</returns>
  1177. private static string[] Split(string val, char[] separators, StringSplitOptions options)
  1178. {
  1179. return val.Split(separators, options);
  1180. }
  1181. }
  1182. }