BaseItemXmlParser.cs 43 KB

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