BaseItemXmlParser.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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 IHasLanguage;
  230. if (hasLanguage != null)
  231. {
  232. hasLanguage.Language = 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 "Trailers":
  454. {
  455. using (var subtree = reader.ReadSubtree())
  456. {
  457. var hasTrailers = item as IHasTrailers;
  458. if (hasTrailers != null)
  459. {
  460. FetchDataFromTrailersNode(subtree, hasTrailers);
  461. }
  462. }
  463. break;
  464. }
  465. case "ReleaseYear":
  466. case "ProductionYear":
  467. {
  468. var val = reader.ReadElementContentAsString();
  469. if (!string.IsNullOrWhiteSpace(val))
  470. {
  471. int productionYear;
  472. if (int.TryParse(val, out productionYear) && productionYear > 1850)
  473. {
  474. item.ProductionYear = productionYear;
  475. }
  476. }
  477. break;
  478. }
  479. case "Rating":
  480. case "IMDBrating":
  481. case "TGDBRating":
  482. {
  483. var rating = reader.ReadElementContentAsString();
  484. if (!string.IsNullOrWhiteSpace(rating))
  485. {
  486. float val;
  487. // All external meta is saving this as '.' for decimal I believe...but just to be sure
  488. if (float.TryParse(rating.Replace(',', '.'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out val))
  489. {
  490. item.CommunityRating = val;
  491. }
  492. }
  493. break;
  494. }
  495. case "BirthDate":
  496. case "PremiereDate":
  497. case "FirstAired":
  498. {
  499. var firstAired = reader.ReadElementContentAsString();
  500. if (!string.IsNullOrWhiteSpace(firstAired))
  501. {
  502. DateTime airDate;
  503. if (DateTime.TryParseExact(firstAired, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out airDate) && airDate.Year > 1850)
  504. {
  505. item.PremiereDate = airDate.ToUniversalTime();
  506. item.ProductionYear = airDate.Year;
  507. }
  508. }
  509. break;
  510. }
  511. case "DeathDate":
  512. case "EndDate":
  513. {
  514. var firstAired = reader.ReadElementContentAsString();
  515. if (!string.IsNullOrWhiteSpace(firstAired))
  516. {
  517. DateTime airDate;
  518. if (DateTime.TryParseExact(firstAired, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out airDate) && airDate.Year > 1850)
  519. {
  520. item.EndDate = airDate.ToUniversalTime();
  521. }
  522. }
  523. break;
  524. }
  525. case "TvDbId":
  526. var tvdbId = reader.ReadElementContentAsString();
  527. if (!string.IsNullOrWhiteSpace(tvdbId))
  528. {
  529. item.SetProviderId(MetadataProviders.Tvdb, tvdbId);
  530. }
  531. break;
  532. case "VoteCount":
  533. {
  534. var val = reader.ReadElementContentAsString();
  535. if (!string.IsNullOrWhiteSpace(val))
  536. {
  537. int num;
  538. if (int.TryParse(val, NumberStyles.Integer, _usCulture, out num))
  539. {
  540. item.VoteCount = num;
  541. }
  542. }
  543. break;
  544. }
  545. case "MusicbrainzId":
  546. {
  547. var mbz = reader.ReadElementContentAsString();
  548. if (!string.IsNullOrWhiteSpace(mbz))
  549. {
  550. item.SetProviderId(MetadataProviders.Musicbrainz, mbz);
  551. }
  552. break;
  553. }
  554. case "MusicBrainzReleaseGroupId":
  555. {
  556. var mbz = reader.ReadElementContentAsString();
  557. if (!string.IsNullOrWhiteSpace(mbz))
  558. {
  559. item.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, mbz);
  560. }
  561. break;
  562. }
  563. case "RottenTomatoesId":
  564. var rtId = reader.ReadElementContentAsString();
  565. if (!string.IsNullOrWhiteSpace(rtId))
  566. {
  567. item.SetProviderId(MetadataProviders.RottenTomatoes, rtId);
  568. }
  569. break;
  570. case "TMDbId":
  571. var tmdb = reader.ReadElementContentAsString();
  572. if (!string.IsNullOrWhiteSpace(tmdb))
  573. {
  574. item.SetProviderId(MetadataProviders.Tmdb, tmdb);
  575. }
  576. break;
  577. case "TMDbCollectionId":
  578. case "CollectionNumber":
  579. var tmdbCollection = reader.ReadElementContentAsString();
  580. if (!string.IsNullOrWhiteSpace(tmdbCollection))
  581. {
  582. item.SetProviderId(MetadataProviders.TmdbCollection, tmdbCollection);
  583. }
  584. break;
  585. case "TVcomId":
  586. var TVcomId = reader.ReadElementContentAsString();
  587. if (!string.IsNullOrWhiteSpace(TVcomId))
  588. {
  589. item.SetProviderId(MetadataProviders.Tvcom, TVcomId);
  590. }
  591. break;
  592. case "Zap2ItId":
  593. var zap2ItId = reader.ReadElementContentAsString();
  594. if (!string.IsNullOrWhiteSpace(zap2ItId))
  595. {
  596. item.SetProviderId(MetadataProviders.Zap2It, zap2ItId);
  597. }
  598. break;
  599. case "IMDB_ID":
  600. case "IMDB":
  601. case "IMDbId":
  602. var imDbId = reader.ReadElementContentAsString();
  603. if (!string.IsNullOrWhiteSpace(imDbId))
  604. {
  605. item.SetProviderId(MetadataProviders.Imdb, imDbId);
  606. }
  607. break;
  608. case "Genres":
  609. {
  610. using (var subtree = reader.ReadSubtree())
  611. {
  612. FetchFromGenresNode(subtree, item);
  613. }
  614. break;
  615. }
  616. case "Tags":
  617. {
  618. using (var subtree = reader.ReadSubtree())
  619. {
  620. var hasTags = item as IHasTags;
  621. if (hasTags != null)
  622. {
  623. FetchFromTagsNode(subtree, hasTags);
  624. }
  625. }
  626. break;
  627. }
  628. case "Persons":
  629. {
  630. using (var subtree = reader.ReadSubtree())
  631. {
  632. FetchDataFromPersonsNode(subtree, item);
  633. }
  634. break;
  635. }
  636. case "ParentalRating":
  637. {
  638. using (var subtree = reader.ReadSubtree())
  639. {
  640. FetchFromParentalRatingNode(subtree, item);
  641. }
  642. break;
  643. }
  644. case "Studios":
  645. {
  646. using (var subtree = reader.ReadSubtree())
  647. {
  648. FetchFromStudiosNode(subtree, item);
  649. }
  650. break;
  651. }
  652. case "MediaInfo":
  653. {
  654. using (var subtree = reader.ReadSubtree())
  655. {
  656. FetchFromMediaInfoNode(subtree, item);
  657. }
  658. break;
  659. }
  660. default:
  661. reader.Skip();
  662. break;
  663. }
  664. }
  665. /// <summary>
  666. /// Fetches from media info node.
  667. /// </summary>
  668. /// <param name="reader">The reader.</param>
  669. /// <param name="item">The item.</param>
  670. private void FetchFromMediaInfoNode(XmlReader reader, T item)
  671. {
  672. reader.MoveToContent();
  673. while (reader.Read())
  674. {
  675. if (reader.NodeType == XmlNodeType.Element)
  676. {
  677. switch (reader.Name)
  678. {
  679. case "Video":
  680. {
  681. using (var subtree = reader.ReadSubtree())
  682. {
  683. FetchFromMediaInfoVideoNode(subtree, item);
  684. }
  685. break;
  686. }
  687. default:
  688. reader.Skip();
  689. break;
  690. }
  691. }
  692. }
  693. }
  694. /// <summary>
  695. /// Fetches from media info video node.
  696. /// </summary>
  697. /// <param name="reader">The reader.</param>
  698. /// <param name="item">The item.</param>
  699. private void FetchFromMediaInfoVideoNode(XmlReader reader, T item)
  700. {
  701. reader.MoveToContent();
  702. while (reader.Read())
  703. {
  704. if (reader.NodeType == XmlNodeType.Element)
  705. {
  706. switch (reader.Name)
  707. {
  708. case "Format3D":
  709. {
  710. var video = item as Video;
  711. if (video != null)
  712. {
  713. var val = reader.ReadElementContentAsString();
  714. if (string.Equals("HSBS", val))
  715. {
  716. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  717. }
  718. else if (string.Equals("HTAB", val))
  719. {
  720. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  721. }
  722. else if (string.Equals("FTAB", val))
  723. {
  724. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  725. }
  726. else if (string.Equals("FSBS", val))
  727. {
  728. video.Video3DFormat = Video3DFormat.FullSideBySide;
  729. }
  730. }
  731. break;
  732. }
  733. default:
  734. reader.Skip();
  735. break;
  736. }
  737. }
  738. }
  739. }
  740. /// <summary>
  741. /// Fetches from taglines node.
  742. /// </summary>
  743. /// <param name="reader">The reader.</param>
  744. /// <param name="item">The item.</param>
  745. private void FetchFromTaglinesNode(XmlReader reader, T item)
  746. {
  747. reader.MoveToContent();
  748. while (reader.Read())
  749. {
  750. if (reader.NodeType == XmlNodeType.Element)
  751. {
  752. switch (reader.Name)
  753. {
  754. case "Tagline":
  755. {
  756. var val = reader.ReadElementContentAsString();
  757. if (!string.IsNullOrWhiteSpace(val))
  758. {
  759. var hasTaglines = item as IHasTaglines;
  760. if (hasTaglines != null)
  761. {
  762. if (!string.IsNullOrWhiteSpace(val))
  763. {
  764. hasTaglines.AddTagline(val);
  765. }
  766. }
  767. }
  768. break;
  769. }
  770. default:
  771. reader.Skip();
  772. break;
  773. }
  774. }
  775. }
  776. }
  777. /// <summary>
  778. /// Fetches from genres node.
  779. /// </summary>
  780. /// <param name="reader">The reader.</param>
  781. /// <param name="item">The item.</param>
  782. private void FetchFromGenresNode(XmlReader reader, T item)
  783. {
  784. reader.MoveToContent();
  785. while (reader.Read())
  786. {
  787. if (reader.NodeType == XmlNodeType.Element)
  788. {
  789. switch (reader.Name)
  790. {
  791. case "Genre":
  792. {
  793. var genre = reader.ReadElementContentAsString();
  794. if (!string.IsNullOrWhiteSpace(genre))
  795. {
  796. item.AddGenre(genre);
  797. }
  798. break;
  799. }
  800. default:
  801. reader.Skip();
  802. break;
  803. }
  804. }
  805. }
  806. }
  807. private void FetchFromTagsNode(XmlReader reader, IHasTags item)
  808. {
  809. reader.MoveToContent();
  810. while (reader.Read())
  811. {
  812. if (reader.NodeType == XmlNodeType.Element)
  813. {
  814. switch (reader.Name)
  815. {
  816. case "Tag":
  817. {
  818. var tag = reader.ReadElementContentAsString();
  819. if (!string.IsNullOrWhiteSpace(tag))
  820. {
  821. item.AddTag(tag);
  822. }
  823. break;
  824. }
  825. default:
  826. reader.Skip();
  827. break;
  828. }
  829. }
  830. }
  831. }
  832. /// <summary>
  833. /// Fetches the data from persons node.
  834. /// </summary>
  835. /// <param name="reader">The reader.</param>
  836. /// <param name="item">The item.</param>
  837. private void FetchDataFromPersonsNode(XmlReader reader, T item)
  838. {
  839. reader.MoveToContent();
  840. while (reader.Read())
  841. {
  842. if (reader.NodeType == XmlNodeType.Element)
  843. {
  844. switch (reader.Name)
  845. {
  846. case "Person":
  847. case "Actor":
  848. {
  849. using (var subtree = reader.ReadSubtree())
  850. {
  851. foreach (var person in GetPersonsFromXmlNode(subtree))
  852. {
  853. if (string.IsNullOrWhiteSpace(person.Name))
  854. {
  855. continue;
  856. }
  857. item.AddPerson(person);
  858. }
  859. }
  860. break;
  861. }
  862. default:
  863. reader.Skip();
  864. break;
  865. }
  866. }
  867. }
  868. }
  869. private void FetchDataFromTrailersNode(XmlReader reader, IHasTrailers item)
  870. {
  871. reader.MoveToContent();
  872. while (reader.Read())
  873. {
  874. if (reader.NodeType == XmlNodeType.Element)
  875. {
  876. switch (reader.Name)
  877. {
  878. case "Trailer":
  879. {
  880. var val = reader.ReadElementContentAsString();
  881. if (!string.IsNullOrWhiteSpace(val))
  882. {
  883. item.AddTrailerUrl(val, false);
  884. }
  885. break;
  886. }
  887. default:
  888. reader.Skip();
  889. break;
  890. }
  891. }
  892. }
  893. }
  894. protected async Task FetchChaptersFromXmlNode(BaseItem item, XmlReader reader, IItemRepository repository, CancellationToken cancellationToken)
  895. {
  896. var runtime = item.RunTimeTicks ?? 0;
  897. using (reader)
  898. {
  899. var chapters = GetChaptersFromXmlNode(reader)
  900. .Where(i => i.StartPositionTicks >= 0 && i.StartPositionTicks < runtime);
  901. await repository.SaveChapters(item.Id, chapters, cancellationToken).ConfigureAwait(false);
  902. }
  903. }
  904. private IEnumerable<ChapterInfo> GetChaptersFromXmlNode(XmlReader reader)
  905. {
  906. var chapters = new List<ChapterInfo>();
  907. reader.MoveToContent();
  908. while (reader.Read())
  909. {
  910. if (reader.NodeType == XmlNodeType.Element)
  911. {
  912. switch (reader.Name)
  913. {
  914. case "Chapter":
  915. {
  916. using (var subtree = reader.ReadSubtree())
  917. {
  918. chapters.Add(GetChapterInfoFromXmlNode(subtree));
  919. }
  920. break;
  921. }
  922. default:
  923. reader.Skip();
  924. break;
  925. }
  926. }
  927. }
  928. return chapters;
  929. }
  930. private ChapterInfo GetChapterInfoFromXmlNode(XmlReader reader)
  931. {
  932. var chapter = new ChapterInfo();
  933. reader.MoveToContent();
  934. while (reader.Read())
  935. {
  936. if (reader.NodeType == XmlNodeType.Element)
  937. {
  938. switch (reader.Name)
  939. {
  940. case "StartPositionMs":
  941. {
  942. var val = reader.ReadElementContentAsString();
  943. var ms = long.Parse(val, _usCulture);
  944. chapter.StartPositionTicks = TimeSpan.FromMilliseconds(ms).Ticks;
  945. break;
  946. }
  947. case "Name":
  948. {
  949. chapter.Name = reader.ReadElementContentAsString();
  950. break;
  951. }
  952. default:
  953. reader.Skip();
  954. break;
  955. }
  956. }
  957. }
  958. return chapter;
  959. }
  960. /// <summary>
  961. /// Fetches from studios node.
  962. /// </summary>
  963. /// <param name="reader">The reader.</param>
  964. /// <param name="item">The item.</param>
  965. private void FetchFromStudiosNode(XmlReader reader, T item)
  966. {
  967. reader.MoveToContent();
  968. while (reader.Read())
  969. {
  970. if (reader.NodeType == XmlNodeType.Element)
  971. {
  972. switch (reader.Name)
  973. {
  974. case "Studio":
  975. {
  976. var studio = reader.ReadElementContentAsString();
  977. if (!string.IsNullOrWhiteSpace(studio))
  978. {
  979. item.AddStudio(studio);
  980. }
  981. break;
  982. }
  983. default:
  984. reader.Skip();
  985. break;
  986. }
  987. }
  988. }
  989. }
  990. /// <summary>
  991. /// Fetches from parental rating node.
  992. /// </summary>
  993. /// <param name="reader">The reader.</param>
  994. /// <param name="item">The item.</param>
  995. private void FetchFromParentalRatingNode(XmlReader reader, T item)
  996. {
  997. reader.MoveToContent();
  998. while (reader.Read())
  999. {
  1000. if (reader.NodeType == XmlNodeType.Element)
  1001. {
  1002. switch (reader.Name)
  1003. {
  1004. // Removed support for "Value" tag as it conflicted with MPAA rating but leaving this function for possible
  1005. // future support of "Description" -ebr
  1006. default:
  1007. reader.Skip();
  1008. break;
  1009. }
  1010. }
  1011. }
  1012. }
  1013. /// <summary>
  1014. /// Gets the persons from XML node.
  1015. /// </summary>
  1016. /// <param name="reader">The reader.</param>
  1017. /// <returns>IEnumerable{PersonInfo}.</returns>
  1018. private IEnumerable<PersonInfo> GetPersonsFromXmlNode(XmlReader reader)
  1019. {
  1020. var name = string.Empty;
  1021. var type = "Actor"; // If type is not specified assume actor
  1022. var role = string.Empty;
  1023. int? sortOrder = null;
  1024. reader.MoveToContent();
  1025. while (reader.Read())
  1026. {
  1027. if (reader.NodeType == XmlNodeType.Element)
  1028. {
  1029. switch (reader.Name)
  1030. {
  1031. case "Name":
  1032. name = reader.ReadElementContentAsString() ?? string.Empty;
  1033. break;
  1034. case "Type":
  1035. {
  1036. var val = reader.ReadElementContentAsString();
  1037. if (!string.IsNullOrWhiteSpace(val))
  1038. {
  1039. type = val;
  1040. }
  1041. break;
  1042. }
  1043. case "Role":
  1044. {
  1045. var val = reader.ReadElementContentAsString();
  1046. if (!string.IsNullOrWhiteSpace(val))
  1047. {
  1048. role = val;
  1049. }
  1050. break;
  1051. }
  1052. case "SortOrder":
  1053. {
  1054. var val = reader.ReadElementContentAsString();
  1055. if (!string.IsNullOrWhiteSpace(val))
  1056. {
  1057. int intVal;
  1058. if (int.TryParse(val, NumberStyles.Integer, _usCulture, out intVal))
  1059. {
  1060. sortOrder = intVal;
  1061. }
  1062. }
  1063. break;
  1064. }
  1065. default:
  1066. reader.Skip();
  1067. break;
  1068. }
  1069. }
  1070. }
  1071. var personInfo = new PersonInfo
  1072. {
  1073. Name = name.Trim(),
  1074. Role = role,
  1075. Type = type,
  1076. SortOrder = sortOrder
  1077. };
  1078. return new[] { personInfo };
  1079. }
  1080. /// <summary>
  1081. /// Used to split names of comma or pipe delimeted genres and people
  1082. /// </summary>
  1083. /// <param name="value">The value.</param>
  1084. /// <returns>IEnumerable{System.String}.</returns>
  1085. private IEnumerable<string> SplitNames(string value)
  1086. {
  1087. value = value ?? string.Empty;
  1088. // Only split by comma if there is no pipe in the string
  1089. // We have to be careful to not split names like Matthew, Jr.
  1090. var separator = value.IndexOf('|') == -1 && value.IndexOf(';') == -1 ? new[] { ',' } : new[] { '|', ';' };
  1091. value = value.Trim().Trim(separator);
  1092. return string.IsNullOrWhiteSpace(value) ? new string[] { } : Split(value, separator, StringSplitOptions.RemoveEmptyEntries);
  1093. }
  1094. /// <summary>
  1095. /// Provides an additional overload for string.split
  1096. /// </summary>
  1097. /// <param name="val">The val.</param>
  1098. /// <param name="separators">The separators.</param>
  1099. /// <param name="options">The options.</param>
  1100. /// <returns>System.String[][].</returns>
  1101. private static string[] Split(string val, char[] separators, StringSplitOptions options)
  1102. {
  1103. return val.Split(separators, options);
  1104. }
  1105. }
  1106. }