BaseItemXmlParser.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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 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. // For audio and video don't replace ffmpeg data
  354. if (!(item is Video || item is Audio))
  355. {
  356. item.RunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
  357. }
  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 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 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 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 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. item.SetProviderId(MetadataProviders.Musicbrainz, mbz);
  588. }
  589. break;
  590. }
  591. case "MusicBrainzReleaseGroupId":
  592. {
  593. var mbz = reader.ReadElementContentAsString();
  594. if (!string.IsNullOrWhiteSpace(mbz))
  595. {
  596. item.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, mbz);
  597. }
  598. break;
  599. }
  600. case "RottenTomatoesId":
  601. var rtId = reader.ReadElementContentAsString();
  602. if (!string.IsNullOrWhiteSpace(rtId))
  603. {
  604. item.SetProviderId(MetadataProviders.RottenTomatoes, rtId);
  605. }
  606. break;
  607. case "TMDbId":
  608. var tmdb = reader.ReadElementContentAsString();
  609. if (!string.IsNullOrWhiteSpace(tmdb))
  610. {
  611. item.SetProviderId(MetadataProviders.Tmdb, tmdb);
  612. }
  613. break;
  614. case "TMDbCollectionId":
  615. case "CollectionNumber":
  616. var tmdbCollection = reader.ReadElementContentAsString();
  617. if (!string.IsNullOrWhiteSpace(tmdbCollection))
  618. {
  619. item.SetProviderId(MetadataProviders.TmdbCollection, tmdbCollection);
  620. }
  621. break;
  622. case "TVcomId":
  623. var TVcomId = reader.ReadElementContentAsString();
  624. if (!string.IsNullOrWhiteSpace(TVcomId))
  625. {
  626. item.SetProviderId(MetadataProviders.Tvcom, TVcomId);
  627. }
  628. break;
  629. case "Zap2ItId":
  630. var zap2ItId = reader.ReadElementContentAsString();
  631. if (!string.IsNullOrWhiteSpace(zap2ItId))
  632. {
  633. item.SetProviderId(MetadataProviders.Zap2It, zap2ItId);
  634. }
  635. break;
  636. case "IMDB_ID":
  637. case "IMDB":
  638. case "IMDbId":
  639. var imDbId = reader.ReadElementContentAsString();
  640. if (!string.IsNullOrWhiteSpace(imDbId))
  641. {
  642. item.SetProviderId(MetadataProviders.Imdb, imDbId);
  643. }
  644. break;
  645. case "Genres":
  646. {
  647. using (var subtree = reader.ReadSubtree())
  648. {
  649. FetchFromGenresNode(subtree, item);
  650. }
  651. break;
  652. }
  653. case "Tags":
  654. {
  655. using (var subtree = reader.ReadSubtree())
  656. {
  657. var hasTags = item as IHasTags;
  658. if (hasTags != null)
  659. {
  660. FetchFromTagsNode(subtree, hasTags);
  661. }
  662. }
  663. break;
  664. }
  665. case "PlotKeywords":
  666. {
  667. using (var subtree = reader.ReadSubtree())
  668. {
  669. var hasTags = item as IHasKeywords;
  670. if (hasTags != null)
  671. {
  672. FetchFromKeywordsNode(subtree, hasTags);
  673. }
  674. }
  675. break;
  676. }
  677. case "Persons":
  678. {
  679. using (var subtree = reader.ReadSubtree())
  680. {
  681. FetchDataFromPersonsNode(subtree, item);
  682. }
  683. break;
  684. }
  685. case "ParentalRating":
  686. {
  687. using (var subtree = reader.ReadSubtree())
  688. {
  689. FetchFromParentalRatingNode(subtree, item);
  690. }
  691. break;
  692. }
  693. case "Studios":
  694. {
  695. using (var subtree = reader.ReadSubtree())
  696. {
  697. FetchFromStudiosNode(subtree, item);
  698. }
  699. break;
  700. }
  701. case "Format3D":
  702. {
  703. var video = item as Video;
  704. if (video != null)
  705. {
  706. var val = reader.ReadElementContentAsString();
  707. if (string.Equals("HSBS", val))
  708. {
  709. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  710. }
  711. else if (string.Equals("HTAB", val))
  712. {
  713. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  714. }
  715. else if (string.Equals("FTAB", val))
  716. {
  717. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  718. }
  719. else if (string.Equals("FSBS", val))
  720. {
  721. video.Video3DFormat = Video3DFormat.FullSideBySide;
  722. }
  723. }
  724. break;
  725. }
  726. default:
  727. reader.Skip();
  728. break;
  729. }
  730. }
  731. /// <summary>
  732. /// Fetches from taglines node.
  733. /// </summary>
  734. /// <param name="reader">The reader.</param>
  735. /// <param name="item">The item.</param>
  736. private void FetchFromTaglinesNode(XmlReader reader, T item)
  737. {
  738. reader.MoveToContent();
  739. while (reader.Read())
  740. {
  741. if (reader.NodeType == XmlNodeType.Element)
  742. {
  743. switch (reader.Name)
  744. {
  745. case "Tagline":
  746. {
  747. var val = reader.ReadElementContentAsString();
  748. if (!string.IsNullOrWhiteSpace(val))
  749. {
  750. var hasTaglines = item as IHasTaglines;
  751. if (hasTaglines != null)
  752. {
  753. if (!string.IsNullOrWhiteSpace(val))
  754. {
  755. hasTaglines.AddTagline(val);
  756. }
  757. }
  758. }
  759. break;
  760. }
  761. default:
  762. reader.Skip();
  763. break;
  764. }
  765. }
  766. }
  767. }
  768. /// <summary>
  769. /// Fetches from genres node.
  770. /// </summary>
  771. /// <param name="reader">The reader.</param>
  772. /// <param name="item">The item.</param>
  773. private void FetchFromGenresNode(XmlReader reader, T item)
  774. {
  775. reader.MoveToContent();
  776. while (reader.Read())
  777. {
  778. if (reader.NodeType == XmlNodeType.Element)
  779. {
  780. switch (reader.Name)
  781. {
  782. case "Genre":
  783. {
  784. var genre = reader.ReadElementContentAsString();
  785. if (!string.IsNullOrWhiteSpace(genre))
  786. {
  787. item.AddGenre(genre);
  788. }
  789. break;
  790. }
  791. default:
  792. reader.Skip();
  793. break;
  794. }
  795. }
  796. }
  797. }
  798. private void FetchFromTagsNode(XmlReader reader, IHasTags item)
  799. {
  800. reader.MoveToContent();
  801. while (reader.Read())
  802. {
  803. if (reader.NodeType == XmlNodeType.Element)
  804. {
  805. switch (reader.Name)
  806. {
  807. case "Tag":
  808. {
  809. var tag = reader.ReadElementContentAsString();
  810. if (!string.IsNullOrWhiteSpace(tag))
  811. {
  812. item.AddTag(tag);
  813. }
  814. break;
  815. }
  816. default:
  817. reader.Skip();
  818. break;
  819. }
  820. }
  821. }
  822. }
  823. private void FetchFromKeywordsNode(XmlReader reader, IHasKeywords item)
  824. {
  825. reader.MoveToContent();
  826. while (reader.Read())
  827. {
  828. if (reader.NodeType == XmlNodeType.Element)
  829. {
  830. switch (reader.Name)
  831. {
  832. case "PlotKeyword":
  833. {
  834. var tag = reader.ReadElementContentAsString();
  835. if (!string.IsNullOrWhiteSpace(tag))
  836. {
  837. item.AddKeyword(tag);
  838. }
  839. break;
  840. }
  841. default:
  842. reader.Skip();
  843. break;
  844. }
  845. }
  846. }
  847. }
  848. /// <summary>
  849. /// Fetches the data from persons node.
  850. /// </summary>
  851. /// <param name="reader">The reader.</param>
  852. /// <param name="item">The item.</param>
  853. private void FetchDataFromPersonsNode(XmlReader reader, T item)
  854. {
  855. reader.MoveToContent();
  856. while (reader.Read())
  857. {
  858. if (reader.NodeType == XmlNodeType.Element)
  859. {
  860. switch (reader.Name)
  861. {
  862. case "Person":
  863. case "Actor":
  864. {
  865. using (var subtree = reader.ReadSubtree())
  866. {
  867. foreach (var person in GetPersonsFromXmlNode(subtree))
  868. {
  869. if (string.IsNullOrWhiteSpace(person.Name))
  870. {
  871. continue;
  872. }
  873. item.AddPerson(person);
  874. }
  875. }
  876. break;
  877. }
  878. default:
  879. reader.Skip();
  880. break;
  881. }
  882. }
  883. }
  884. }
  885. private void FetchDataFromTrailersNode(XmlReader reader, IHasTrailers item)
  886. {
  887. reader.MoveToContent();
  888. while (reader.Read())
  889. {
  890. if (reader.NodeType == XmlNodeType.Element)
  891. {
  892. switch (reader.Name)
  893. {
  894. case "Trailer":
  895. {
  896. var val = reader.ReadElementContentAsString();
  897. if (!string.IsNullOrWhiteSpace(val))
  898. {
  899. item.AddTrailerUrl(val, false);
  900. }
  901. break;
  902. }
  903. default:
  904. reader.Skip();
  905. break;
  906. }
  907. }
  908. }
  909. }
  910. protected async Task FetchChaptersFromXmlNode(BaseItem item, XmlReader reader, IItemRepository repository, CancellationToken cancellationToken)
  911. {
  912. var runtime = item.RunTimeTicks ?? 0;
  913. using (reader)
  914. {
  915. var chapters = GetChaptersFromXmlNode(reader)
  916. .Where(i => i.StartPositionTicks >= 0 && i.StartPositionTicks < runtime);
  917. await repository.SaveChapters(item.Id, chapters, cancellationToken).ConfigureAwait(false);
  918. }
  919. }
  920. private IEnumerable<ChapterInfo> GetChaptersFromXmlNode(XmlReader reader)
  921. {
  922. var chapters = new List<ChapterInfo>();
  923. reader.MoveToContent();
  924. while (reader.Read())
  925. {
  926. if (reader.NodeType == XmlNodeType.Element)
  927. {
  928. switch (reader.Name)
  929. {
  930. case "Chapter":
  931. {
  932. using (var subtree = reader.ReadSubtree())
  933. {
  934. chapters.Add(GetChapterInfoFromXmlNode(subtree));
  935. }
  936. break;
  937. }
  938. default:
  939. reader.Skip();
  940. break;
  941. }
  942. }
  943. }
  944. return chapters;
  945. }
  946. private ChapterInfo GetChapterInfoFromXmlNode(XmlReader reader)
  947. {
  948. var chapter = new ChapterInfo();
  949. reader.MoveToContent();
  950. while (reader.Read())
  951. {
  952. if (reader.NodeType == XmlNodeType.Element)
  953. {
  954. switch (reader.Name)
  955. {
  956. case "StartPositionMs":
  957. {
  958. var val = reader.ReadElementContentAsString();
  959. var ms = long.Parse(val, _usCulture);
  960. chapter.StartPositionTicks = TimeSpan.FromMilliseconds(ms).Ticks;
  961. break;
  962. }
  963. case "Name":
  964. {
  965. chapter.Name = reader.ReadElementContentAsString();
  966. break;
  967. }
  968. default:
  969. reader.Skip();
  970. break;
  971. }
  972. }
  973. }
  974. return chapter;
  975. }
  976. /// <summary>
  977. /// Fetches from studios node.
  978. /// </summary>
  979. /// <param name="reader">The reader.</param>
  980. /// <param name="item">The item.</param>
  981. private void FetchFromStudiosNode(XmlReader reader, T item)
  982. {
  983. reader.MoveToContent();
  984. while (reader.Read())
  985. {
  986. if (reader.NodeType == XmlNodeType.Element)
  987. {
  988. switch (reader.Name)
  989. {
  990. case "Studio":
  991. {
  992. var studio = reader.ReadElementContentAsString();
  993. if (!string.IsNullOrWhiteSpace(studio))
  994. {
  995. item.AddStudio(studio);
  996. }
  997. break;
  998. }
  999. default:
  1000. reader.Skip();
  1001. break;
  1002. }
  1003. }
  1004. }
  1005. }
  1006. /// <summary>
  1007. /// Fetches from parental rating node.
  1008. /// </summary>
  1009. /// <param name="reader">The reader.</param>
  1010. /// <param name="item">The item.</param>
  1011. private void FetchFromParentalRatingNode(XmlReader reader, T item)
  1012. {
  1013. reader.MoveToContent();
  1014. while (reader.Read())
  1015. {
  1016. if (reader.NodeType == XmlNodeType.Element)
  1017. {
  1018. switch (reader.Name)
  1019. {
  1020. // Removed support for "Value" tag as it conflicted with MPAA rating but leaving this function for possible
  1021. // future support of "Description" -ebr
  1022. default:
  1023. reader.Skip();
  1024. break;
  1025. }
  1026. }
  1027. }
  1028. }
  1029. /// <summary>
  1030. /// Gets the persons from XML node.
  1031. /// </summary>
  1032. /// <param name="reader">The reader.</param>
  1033. /// <returns>IEnumerable{PersonInfo}.</returns>
  1034. private IEnumerable<PersonInfo> GetPersonsFromXmlNode(XmlReader reader)
  1035. {
  1036. var name = string.Empty;
  1037. var type = "Actor"; // If type is not specified assume actor
  1038. var role = string.Empty;
  1039. int? sortOrder = null;
  1040. reader.MoveToContent();
  1041. while (reader.Read())
  1042. {
  1043. if (reader.NodeType == XmlNodeType.Element)
  1044. {
  1045. switch (reader.Name)
  1046. {
  1047. case "Name":
  1048. name = reader.ReadElementContentAsString() ?? string.Empty;
  1049. break;
  1050. case "Type":
  1051. {
  1052. var val = reader.ReadElementContentAsString();
  1053. if (!string.IsNullOrWhiteSpace(val))
  1054. {
  1055. type = val;
  1056. }
  1057. break;
  1058. }
  1059. case "Role":
  1060. {
  1061. var val = reader.ReadElementContentAsString();
  1062. if (!string.IsNullOrWhiteSpace(val))
  1063. {
  1064. role = val;
  1065. }
  1066. break;
  1067. }
  1068. case "SortOrder":
  1069. {
  1070. var val = reader.ReadElementContentAsString();
  1071. if (!string.IsNullOrWhiteSpace(val))
  1072. {
  1073. int intVal;
  1074. if (int.TryParse(val, NumberStyles.Integer, _usCulture, out intVal))
  1075. {
  1076. sortOrder = intVal;
  1077. }
  1078. }
  1079. break;
  1080. }
  1081. default:
  1082. reader.Skip();
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. var personInfo = new PersonInfo
  1088. {
  1089. Name = name.Trim(),
  1090. Role = role,
  1091. Type = type,
  1092. SortOrder = sortOrder
  1093. };
  1094. return new[] { personInfo };
  1095. }
  1096. /// <summary>
  1097. /// Used to split names of comma or pipe delimeted genres and people
  1098. /// </summary>
  1099. /// <param name="value">The value.</param>
  1100. /// <returns>IEnumerable{System.String}.</returns>
  1101. private IEnumerable<string> SplitNames(string value)
  1102. {
  1103. value = value ?? string.Empty;
  1104. // Only split by comma if there is no pipe in the string
  1105. // We have to be careful to not split names like Matthew, Jr.
  1106. var separator = value.IndexOf('|') == -1 && value.IndexOf(';') == -1 ? new[] { ',' } : new[] { '|', ';' };
  1107. value = value.Trim().Trim(separator);
  1108. return string.IsNullOrWhiteSpace(value) ? new string[] { } : Split(value, separator, StringSplitOptions.RemoveEmptyEntries);
  1109. }
  1110. /// <summary>
  1111. /// Provides an additional overload for string.split
  1112. /// </summary>
  1113. /// <param name="val">The val.</param>
  1114. /// <param name="separators">The separators.</param>
  1115. /// <param name="options">The options.</param>
  1116. /// <returns>System.String[][].</returns>
  1117. private static string[] Split(string val, char[] separators, StringSplitOptions options)
  1118. {
  1119. return val.Split(separators, options);
  1120. }
  1121. }
  1122. }