BaseItemXmlParser.cs 46 KB

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