BaseItemXmlParser.cs 48 KB

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