BaseItemXmlParser.cs 44 KB

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