BaseItemXmlParser.cs 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Model.Entities;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Xml;
  12. namespace MediaBrowser.Controller.Providers
  13. {
  14. /// <summary>
  15. /// Provides a base class for parsing metadata xml
  16. /// </summary>
  17. /// <typeparam name="T"></typeparam>
  18. public class BaseItemXmlParser<T>
  19. where T : BaseItem
  20. {
  21. /// <summary>
  22. /// The logger
  23. /// </summary>
  24. protected ILogger Logger { get; private set; }
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="BaseItemXmlParser{T}" /> class.
  27. /// </summary>
  28. /// <param name="logger">The logger.</param>
  29. public BaseItemXmlParser(ILogger logger)
  30. {
  31. Logger = logger;
  32. }
  33. /// <summary>
  34. /// Fetches metadata for an item from one xml file
  35. /// </summary>
  36. /// <param name="item">The item.</param>
  37. /// <param name="metadataFile">The metadata file.</param>
  38. /// <param name="cancellationToken">The cancellation token.</param>
  39. /// <exception cref="System.ArgumentNullException"></exception>
  40. public void Fetch(T item, string metadataFile, CancellationToken cancellationToken)
  41. {
  42. if (item == null)
  43. {
  44. throw new ArgumentNullException();
  45. }
  46. if (string.IsNullOrEmpty(metadataFile))
  47. {
  48. throw new ArgumentNullException();
  49. }
  50. var settings = new XmlReaderSettings
  51. {
  52. CheckCharacters = false,
  53. IgnoreProcessingInstructions = true,
  54. IgnoreComments = true,
  55. ValidationType = ValidationType.None
  56. };
  57. //Fetch(item, metadataFile, settings, Encoding.GetEncoding("ISO-8859-1"), cancellationToken);
  58. Fetch(item, metadataFile, settings, Encoding.UTF8, cancellationToken);
  59. }
  60. /// <summary>
  61. /// Fetches the specified item.
  62. /// </summary>
  63. /// <param name="item">The item.</param>
  64. /// <param name="metadataFile">The metadata file.</param>
  65. /// <param name="settings">The settings.</param>
  66. /// <param name="encoding">The encoding.</param>
  67. /// <param name="cancellationToken">The cancellation token.</param>
  68. private void Fetch(T item, string metadataFile, XmlReaderSettings settings, Encoding encoding, CancellationToken cancellationToken)
  69. {
  70. using (var streamReader = new StreamReader(metadataFile, encoding))
  71. {
  72. // Use XmlReader for best performance
  73. using (var reader = XmlReader.Create(streamReader, settings))
  74. {
  75. reader.MoveToContent();
  76. // Loop through each element
  77. while (reader.Read())
  78. {
  79. cancellationToken.ThrowIfCancellationRequested();
  80. if (reader.NodeType == XmlNodeType.Element)
  81. {
  82. FetchDataFromXmlNode(reader, item);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  89. /// <summary>
  90. /// Fetches metadata from one Xml Element
  91. /// </summary>
  92. /// <param name="reader">The reader.</param>
  93. /// <param name="item">The item.</param>
  94. protected virtual void FetchDataFromXmlNode(XmlReader reader, T item)
  95. {
  96. switch (reader.Name)
  97. {
  98. // DateCreated
  99. case "Added":
  100. {
  101. var val = reader.ReadElementContentAsString();
  102. if (!string.IsNullOrWhiteSpace(val))
  103. {
  104. DateTime added;
  105. if (DateTime.TryParse(val, out added))
  106. {
  107. item.DateCreated = added.ToUniversalTime();
  108. }
  109. else
  110. {
  111. Logger.Warn("Invalid Added value found: " + val);
  112. }
  113. }
  114. break;
  115. }
  116. case "LocalTitle":
  117. item.Name = reader.ReadElementContentAsString();
  118. break;
  119. case "Type":
  120. {
  121. var type = reader.ReadElementContentAsString();
  122. if (!string.IsNullOrWhiteSpace(type) && !type.Equals("none", StringComparison.OrdinalIgnoreCase))
  123. {
  124. item.DisplayMediaType = type;
  125. }
  126. break;
  127. }
  128. case "CriticRating":
  129. {
  130. var text = reader.ReadElementContentAsString();
  131. var hasCriticRating = item as IHasCriticRating;
  132. if (hasCriticRating != null && !string.IsNullOrEmpty(text))
  133. {
  134. float value;
  135. if (float.TryParse(text, NumberStyles.Any, _usCulture, out value))
  136. {
  137. hasCriticRating.CriticRating = value;
  138. }
  139. }
  140. break;
  141. }
  142. case "Budget":
  143. {
  144. var text = reader.ReadElementContentAsString();
  145. var hasBudget = item as IHasBudget;
  146. if (hasBudget != null)
  147. {
  148. double value;
  149. if (double.TryParse(text, NumberStyles.Any, _usCulture, out value))
  150. {
  151. hasBudget.Budget = value;
  152. }
  153. }
  154. break;
  155. }
  156. case "Revenue":
  157. {
  158. var text = reader.ReadElementContentAsString();
  159. var hasBudget = item as IHasBudget;
  160. if (hasBudget != null)
  161. {
  162. double value;
  163. if (double.TryParse(text, NumberStyles.Any, _usCulture, out value))
  164. {
  165. hasBudget.Revenue = value;
  166. }
  167. }
  168. break;
  169. }
  170. case "Metascore":
  171. {
  172. var text = reader.ReadElementContentAsString();
  173. var hasMetascore = item as IHasMetascore;
  174. if (hasMetascore != null)
  175. {
  176. float value;
  177. if (float.TryParse(text, NumberStyles.Any, _usCulture, out value))
  178. {
  179. hasMetascore.Metascore = value;
  180. }
  181. }
  182. break;
  183. }
  184. case "AwardSummary":
  185. {
  186. var text = reader.ReadElementContentAsString();
  187. var hasAwards = item as IHasAwards;
  188. if (hasAwards != null)
  189. {
  190. if (!string.IsNullOrWhiteSpace(text))
  191. {
  192. hasAwards.AwardSummary = text;
  193. }
  194. }
  195. break;
  196. }
  197. case "SortTitle":
  198. {
  199. var val = reader.ReadElementContentAsString();
  200. if (!string.IsNullOrWhiteSpace(val))
  201. {
  202. item.ForcedSortName = val;
  203. }
  204. break;
  205. }
  206. case "Overview":
  207. case "Description":
  208. {
  209. var val = reader.ReadElementContentAsString();
  210. if (!string.IsNullOrWhiteSpace(val))
  211. {
  212. item.Overview = val;
  213. }
  214. break;
  215. }
  216. case "ShortOverview":
  217. {
  218. var val = reader.ReadElementContentAsString();
  219. if (!string.IsNullOrWhiteSpace(val))
  220. {
  221. var hasShortOverview = item as IHasShortOverview;
  222. if (hasShortOverview != null)
  223. {
  224. hasShortOverview.ShortOverview = val;
  225. }
  226. }
  227. break;
  228. }
  229. case "CriticRatingSummary":
  230. {
  231. var val = reader.ReadElementContentAsString();
  232. if (!string.IsNullOrWhiteSpace(val))
  233. {
  234. var hasCriticRating = item as IHasCriticRating;
  235. if (hasCriticRating != null)
  236. {
  237. hasCriticRating.CriticRatingSummary = val;
  238. }
  239. }
  240. break;
  241. }
  242. case "Language":
  243. {
  244. var val = reader.ReadElementContentAsString();
  245. var hasLanguage = item as IHasPreferredMetadataLanguage;
  246. if (hasLanguage != null)
  247. {
  248. hasLanguage.PreferredMetadataLanguage = val;
  249. }
  250. break;
  251. }
  252. case "PlaceOfBirth":
  253. {
  254. var val = reader.ReadElementContentAsString();
  255. if (!string.IsNullOrWhiteSpace(val))
  256. {
  257. var person = item as Person;
  258. if (person != null)
  259. {
  260. person.PlaceOfBirth = val;
  261. }
  262. }
  263. break;
  264. }
  265. case "Website":
  266. {
  267. var val = reader.ReadElementContentAsString();
  268. if (!string.IsNullOrWhiteSpace(val))
  269. {
  270. item.HomePageUrl = val;
  271. }
  272. break;
  273. }
  274. case "LockedFields":
  275. {
  276. var fields = new List<MetadataFields>();
  277. var val = reader.ReadElementContentAsString();
  278. if (!string.IsNullOrWhiteSpace(val))
  279. {
  280. var list = val.Split('|').Select(i =>
  281. {
  282. MetadataFields field;
  283. if (Enum.TryParse<MetadataFields>(i, true, out field))
  284. {
  285. return (MetadataFields?)field;
  286. }
  287. return null;
  288. }).Where(i => i.HasValue).Select(i => i.Value);
  289. fields.AddRange(list);
  290. }
  291. item.LockedFields = fields;
  292. break;
  293. }
  294. case "TagLines":
  295. {
  296. using (var subtree = reader.ReadSubtree())
  297. {
  298. FetchFromTaglinesNode(subtree, item);
  299. }
  300. break;
  301. }
  302. case "Countries":
  303. {
  304. using (var subtree = reader.ReadSubtree())
  305. {
  306. FetchFromCountriesNode(subtree, item);
  307. }
  308. break;
  309. }
  310. case "ContentRating":
  311. case "MPAARating":
  312. {
  313. var rating = reader.ReadElementContentAsString();
  314. if (!string.IsNullOrWhiteSpace(rating))
  315. {
  316. item.OfficialRating = rating;
  317. }
  318. break;
  319. }
  320. case "MPAADescription":
  321. {
  322. var rating = reader.ReadElementContentAsString();
  323. if (!string.IsNullOrWhiteSpace(rating))
  324. {
  325. item.OfficialRatingDescription = rating;
  326. }
  327. break;
  328. }
  329. case "CustomRating":
  330. {
  331. var val = reader.ReadElementContentAsString();
  332. if (!string.IsNullOrWhiteSpace(val))
  333. {
  334. item.CustomRating = val;
  335. }
  336. break;
  337. }
  338. case "RunningTime":
  339. {
  340. var text = reader.ReadElementContentAsString();
  341. if (!string.IsNullOrWhiteSpace(text))
  342. {
  343. int runtime;
  344. if (int.TryParse(text.Split(' ')[0], NumberStyles.Integer, _usCulture, out runtime))
  345. {
  346. item.RunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
  347. }
  348. }
  349. break;
  350. }
  351. case "AspectRatio":
  352. {
  353. var val = reader.ReadElementContentAsString();
  354. var hasAspectRatio = item as IHasAspectRatio;
  355. if (!string.IsNullOrWhiteSpace(val) && hasAspectRatio != null)
  356. {
  357. hasAspectRatio.AspectRatio = val;
  358. }
  359. break;
  360. }
  361. case "LockData":
  362. {
  363. var val = reader.ReadElementContentAsString();
  364. if (!string.IsNullOrWhiteSpace(val))
  365. {
  366. item.IsLocked = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  367. }
  368. break;
  369. }
  370. case "Network":
  371. {
  372. foreach (var name in SplitNames(reader.ReadElementContentAsString()))
  373. {
  374. if (string.IsNullOrWhiteSpace(name))
  375. {
  376. continue;
  377. }
  378. item.AddStudio(name);
  379. }
  380. break;
  381. }
  382. case "Director":
  383. {
  384. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new Entities.PersonInfo { Name = v.Trim(), Type = PersonType.Director }))
  385. {
  386. if (string.IsNullOrWhiteSpace(p.Name))
  387. {
  388. continue;
  389. }
  390. item.AddPerson(p);
  391. }
  392. break;
  393. }
  394. case "Writer":
  395. {
  396. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new Entities.PersonInfo { Name = v.Trim(), Type = PersonType.Writer }))
  397. {
  398. if (string.IsNullOrWhiteSpace(p.Name))
  399. {
  400. continue;
  401. }
  402. item.AddPerson(p);
  403. }
  404. break;
  405. }
  406. case "Actors":
  407. {
  408. var actors = reader.ReadInnerXml();
  409. if (actors.Contains("<"))
  410. {
  411. // This is one of the mis-named "Actors" full nodes created by MB2
  412. // Create a reader and pass it to the persons node processor
  413. FetchDataFromPersonsNode(new XmlTextReader(new StringReader("<Persons>" + actors + "</Persons>")), item);
  414. }
  415. else
  416. {
  417. // Old-style piped string
  418. foreach (var p in SplitNames(actors).Select(v => new Entities.PersonInfo { Name = v.Trim(), Type = PersonType.Actor }))
  419. {
  420. if (string.IsNullOrWhiteSpace(p.Name))
  421. {
  422. continue;
  423. }
  424. item.AddPerson(p);
  425. }
  426. }
  427. break;
  428. }
  429. case "GuestStars":
  430. {
  431. foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new Entities.PersonInfo { Name = v.Trim(), Type = PersonType.GuestStar }))
  432. {
  433. if (string.IsNullOrWhiteSpace(p.Name))
  434. {
  435. continue;
  436. }
  437. item.AddPerson(p);
  438. }
  439. break;
  440. }
  441. case "Trailer":
  442. {
  443. var val = reader.ReadElementContentAsString();
  444. var hasTrailers = item as IHasTrailers;
  445. if (hasTrailers != null)
  446. {
  447. if (!string.IsNullOrWhiteSpace(val))
  448. {
  449. hasTrailers.AddTrailerUrl(val, false);
  450. }
  451. }
  452. break;
  453. }
  454. case "DisplayOrder":
  455. {
  456. var val = reader.ReadElementContentAsString();
  457. var hasDisplayOrder = item as IHasDisplayOrder;
  458. if (hasDisplayOrder != null)
  459. {
  460. if (!string.IsNullOrWhiteSpace(val))
  461. {
  462. hasDisplayOrder.DisplayOrder = val;
  463. }
  464. }
  465. break;
  466. }
  467. case "Trailers":
  468. {
  469. using (var subtree = reader.ReadSubtree())
  470. {
  471. var hasTrailers = item as IHasTrailers;
  472. if (hasTrailers != null)
  473. {
  474. FetchDataFromTrailersNode(subtree, hasTrailers);
  475. }
  476. }
  477. break;
  478. }
  479. case "ProductionYear":
  480. {
  481. var val = reader.ReadElementContentAsString();
  482. if (!string.IsNullOrWhiteSpace(val))
  483. {
  484. int productionYear;
  485. if (int.TryParse(val, out productionYear) && productionYear > 1850)
  486. {
  487. item.ProductionYear = productionYear;
  488. }
  489. }
  490. break;
  491. }
  492. case "Rating":
  493. case "IMDBrating":
  494. {
  495. var rating = reader.ReadElementContentAsString();
  496. if (!string.IsNullOrWhiteSpace(rating))
  497. {
  498. float val;
  499. // All external meta is saving this as '.' for decimal I believe...but just to be sure
  500. if (float.TryParse(rating.Replace(',', '.'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out val))
  501. {
  502. item.CommunityRating = val;
  503. }
  504. }
  505. break;
  506. }
  507. case "BirthDate":
  508. case "PremiereDate":
  509. case "FirstAired":
  510. {
  511. var firstAired = reader.ReadElementContentAsString();
  512. if (!string.IsNullOrWhiteSpace(firstAired))
  513. {
  514. DateTime airDate;
  515. if (DateTime.TryParseExact(firstAired, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out airDate) && airDate.Year > 1850)
  516. {
  517. item.PremiereDate = airDate.ToUniversalTime();
  518. item.ProductionYear = airDate.Year;
  519. }
  520. }
  521. break;
  522. }
  523. case "DeathDate":
  524. case "EndDate":
  525. {
  526. var firstAired = reader.ReadElementContentAsString();
  527. if (!string.IsNullOrWhiteSpace(firstAired))
  528. {
  529. DateTime airDate;
  530. if (DateTime.TryParseExact(firstAired, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out airDate) && airDate.Year > 1850)
  531. {
  532. item.EndDate = airDate.ToUniversalTime();
  533. }
  534. }
  535. break;
  536. }
  537. case "TvDbId":
  538. var tvdbId = reader.ReadElementContentAsString();
  539. if (!string.IsNullOrWhiteSpace(tvdbId))
  540. {
  541. item.SetProviderId(MetadataProviders.Tvdb, tvdbId);
  542. }
  543. break;
  544. case "VoteCount":
  545. {
  546. var val = reader.ReadElementContentAsString();
  547. if (!string.IsNullOrWhiteSpace(val))
  548. {
  549. int num;
  550. if (int.TryParse(val, NumberStyles.Integer, _usCulture, out num))
  551. {
  552. item.VoteCount = num;
  553. }
  554. }
  555. break;
  556. }
  557. case "MusicBrainzAlbumId":
  558. {
  559. var mbz = reader.ReadElementContentAsString();
  560. if (!string.IsNullOrWhiteSpace(mbz))
  561. {
  562. item.SetProviderId(MetadataProviders.MusicBrainzAlbum, mbz);
  563. }
  564. break;
  565. }
  566. case "MusicBrainzAlbumArtistId":
  567. {
  568. var mbz = reader.ReadElementContentAsString();
  569. if (!string.IsNullOrWhiteSpace(mbz))
  570. {
  571. item.SetProviderId(MetadataProviders.MusicBrainzAlbumArtist, mbz);
  572. }
  573. break;
  574. }
  575. case "MusicBrainzArtistId":
  576. {
  577. var mbz = reader.ReadElementContentAsString();
  578. if (!string.IsNullOrWhiteSpace(mbz))
  579. {
  580. item.SetProviderId(MetadataProviders.MusicBrainzArtist, mbz);
  581. }
  582. break;
  583. }
  584. case "MusicBrainzReleaseGroupId":
  585. {
  586. var mbz = reader.ReadElementContentAsString();
  587. if (!string.IsNullOrWhiteSpace(mbz))
  588. {
  589. item.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, mbz);
  590. }
  591. break;
  592. }
  593. case "TVRageId":
  594. {
  595. var id = reader.ReadElementContentAsString();
  596. if (!string.IsNullOrWhiteSpace(id))
  597. {
  598. item.SetProviderId(MetadataProviders.TvRage, id);
  599. }
  600. break;
  601. }
  602. case "AudioDbArtistId":
  603. {
  604. var id = reader.ReadElementContentAsString();
  605. if (!string.IsNullOrWhiteSpace(id))
  606. {
  607. item.SetProviderId(MetadataProviders.AudioDbArtist, id);
  608. }
  609. break;
  610. }
  611. case "AudioDbAlbumId":
  612. {
  613. var id = reader.ReadElementContentAsString();
  614. if (!string.IsNullOrWhiteSpace(id))
  615. {
  616. item.SetProviderId(MetadataProviders.AudioDbAlbum, id);
  617. }
  618. break;
  619. }
  620. case "RottenTomatoesId":
  621. var rtId = reader.ReadElementContentAsString();
  622. if (!string.IsNullOrWhiteSpace(rtId))
  623. {
  624. item.SetProviderId(MetadataProviders.RottenTomatoes, rtId);
  625. }
  626. break;
  627. case "TMDbId":
  628. var tmdb = reader.ReadElementContentAsString();
  629. if (!string.IsNullOrWhiteSpace(tmdb))
  630. {
  631. item.SetProviderId(MetadataProviders.Tmdb, tmdb);
  632. }
  633. break;
  634. case "TMDbCollectionId":
  635. var tmdbCollection = reader.ReadElementContentAsString();
  636. if (!string.IsNullOrWhiteSpace(tmdbCollection))
  637. {
  638. item.SetProviderId(MetadataProviders.TmdbCollection, tmdbCollection);
  639. }
  640. break;
  641. case "TVcomId":
  642. var TVcomId = reader.ReadElementContentAsString();
  643. if (!string.IsNullOrWhiteSpace(TVcomId))
  644. {
  645. item.SetProviderId(MetadataProviders.Tvcom, TVcomId);
  646. }
  647. break;
  648. case "Zap2ItId":
  649. var zap2ItId = reader.ReadElementContentAsString();
  650. if (!string.IsNullOrWhiteSpace(zap2ItId))
  651. {
  652. item.SetProviderId(MetadataProviders.Zap2It, zap2ItId);
  653. }
  654. break;
  655. case "IMDB":
  656. var imDbId = reader.ReadElementContentAsString();
  657. if (!string.IsNullOrWhiteSpace(imDbId))
  658. {
  659. item.SetProviderId(MetadataProviders.Imdb, imDbId);
  660. }
  661. break;
  662. case "Genres":
  663. {
  664. using (var subtree = reader.ReadSubtree())
  665. {
  666. FetchFromGenresNode(subtree, item);
  667. }
  668. break;
  669. }
  670. case "Tags":
  671. {
  672. using (var subtree = reader.ReadSubtree())
  673. {
  674. var hasTags = item as IHasTags;
  675. if (hasTags != null)
  676. {
  677. FetchFromTagsNode(subtree, hasTags);
  678. }
  679. }
  680. break;
  681. }
  682. case "PlotKeywords":
  683. {
  684. using (var subtree = reader.ReadSubtree())
  685. {
  686. var hasTags = item as IHasKeywords;
  687. if (hasTags != null)
  688. {
  689. FetchFromKeywordsNode(subtree, hasTags);
  690. }
  691. }
  692. break;
  693. }
  694. case "Persons":
  695. {
  696. using (var subtree = reader.ReadSubtree())
  697. {
  698. FetchDataFromPersonsNode(subtree, item);
  699. }
  700. break;
  701. }
  702. case "Studios":
  703. {
  704. using (var subtree = reader.ReadSubtree())
  705. {
  706. FetchFromStudiosNode(subtree, item);
  707. }
  708. break;
  709. }
  710. case "Shares":
  711. {
  712. using (var subtree = reader.ReadSubtree())
  713. {
  714. var hasShares = item as IHasShares;
  715. if (hasShares != null)
  716. {
  717. FetchFromSharesNode(subtree, hasShares);
  718. }
  719. }
  720. break;
  721. }
  722. case "Format3D":
  723. {
  724. var video = item as Video;
  725. if (video != null)
  726. {
  727. var val = reader.ReadElementContentAsString();
  728. if (string.Equals("HSBS", val, StringComparison.OrdinalIgnoreCase))
  729. {
  730. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  731. }
  732. else if (string.Equals("HTAB", val, StringComparison.OrdinalIgnoreCase))
  733. {
  734. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  735. }
  736. else if (string.Equals("FTAB", val, StringComparison.OrdinalIgnoreCase))
  737. {
  738. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  739. }
  740. else if (string.Equals("FSBS", val, StringComparison.OrdinalIgnoreCase))
  741. {
  742. video.Video3DFormat = Video3DFormat.FullSideBySide;
  743. }
  744. }
  745. break;
  746. }
  747. default:
  748. reader.Skip();
  749. break;
  750. }
  751. }
  752. private void FetchFromSharesNode(XmlReader reader, IHasShares item)
  753. {
  754. reader.MoveToContent();
  755. while (reader.Read())
  756. {
  757. if (reader.NodeType == XmlNodeType.Element)
  758. {
  759. switch (reader.Name)
  760. {
  761. case "Share":
  762. {
  763. using (var subtree = reader.ReadSubtree())
  764. {
  765. var share = GetShareFromNode(subtree);
  766. if (share != null)
  767. {
  768. item.Shares.Add(share);
  769. }
  770. }
  771. break;
  772. }
  773. default:
  774. reader.Skip();
  775. break;
  776. }
  777. }
  778. }
  779. }
  780. private Share GetShareFromNode(XmlReader reader)
  781. {
  782. var share = new Share();
  783. reader.MoveToContent();
  784. while (reader.Read())
  785. {
  786. if (reader.NodeType == XmlNodeType.Element)
  787. {
  788. switch (reader.Name)
  789. {
  790. case "UserId":
  791. {
  792. share.UserId = reader.ReadElementContentAsString();
  793. break;
  794. }
  795. case "CanEdit":
  796. {
  797. share.CanEdit = string.Equals(reader.ReadElementContentAsString(), true.ToString(), StringComparison.OrdinalIgnoreCase);
  798. break;
  799. }
  800. default:
  801. reader.Skip();
  802. break;
  803. }
  804. }
  805. }
  806. return share;
  807. }
  808. private void FetchFromCountriesNode(XmlReader reader, T item)
  809. {
  810. reader.MoveToContent();
  811. while (reader.Read())
  812. {
  813. if (reader.NodeType == XmlNodeType.Element)
  814. {
  815. switch (reader.Name)
  816. {
  817. case "Country":
  818. {
  819. var val = reader.ReadElementContentAsString();
  820. if (!string.IsNullOrWhiteSpace(val))
  821. {
  822. var hasProductionLocations = item as IHasProductionLocations;
  823. if (hasProductionLocations != null)
  824. {
  825. if (!string.IsNullOrWhiteSpace(val))
  826. {
  827. hasProductionLocations.AddProductionLocation(val);
  828. }
  829. }
  830. }
  831. break;
  832. }
  833. default:
  834. reader.Skip();
  835. break;
  836. }
  837. }
  838. }
  839. }
  840. /// <summary>
  841. /// Fetches from taglines node.
  842. /// </summary>
  843. /// <param name="reader">The reader.</param>
  844. /// <param name="item">The item.</param>
  845. private void FetchFromTaglinesNode(XmlReader reader, T item)
  846. {
  847. reader.MoveToContent();
  848. while (reader.Read())
  849. {
  850. if (reader.NodeType == XmlNodeType.Element)
  851. {
  852. switch (reader.Name)
  853. {
  854. case "Tagline":
  855. {
  856. var val = reader.ReadElementContentAsString();
  857. if (!string.IsNullOrWhiteSpace(val))
  858. {
  859. var hasTaglines = item as IHasTaglines;
  860. if (hasTaglines != null)
  861. {
  862. if (!string.IsNullOrWhiteSpace(val))
  863. {
  864. hasTaglines.AddTagline(val);
  865. }
  866. }
  867. }
  868. break;
  869. }
  870. default:
  871. reader.Skip();
  872. break;
  873. }
  874. }
  875. }
  876. }
  877. /// <summary>
  878. /// Fetches from genres node.
  879. /// </summary>
  880. /// <param name="reader">The reader.</param>
  881. /// <param name="item">The item.</param>
  882. private void FetchFromGenresNode(XmlReader reader, T item)
  883. {
  884. reader.MoveToContent();
  885. while (reader.Read())
  886. {
  887. if (reader.NodeType == XmlNodeType.Element)
  888. {
  889. switch (reader.Name)
  890. {
  891. case "Genre":
  892. {
  893. var genre = reader.ReadElementContentAsString();
  894. if (!string.IsNullOrWhiteSpace(genre))
  895. {
  896. item.AddGenre(genre);
  897. }
  898. break;
  899. }
  900. default:
  901. reader.Skip();
  902. break;
  903. }
  904. }
  905. }
  906. }
  907. private void FetchFromTagsNode(XmlReader reader, IHasTags item)
  908. {
  909. reader.MoveToContent();
  910. while (reader.Read())
  911. {
  912. if (reader.NodeType == XmlNodeType.Element)
  913. {
  914. switch (reader.Name)
  915. {
  916. case "Tag":
  917. {
  918. var tag = reader.ReadElementContentAsString();
  919. if (!string.IsNullOrWhiteSpace(tag))
  920. {
  921. item.AddTag(tag);
  922. }
  923. break;
  924. }
  925. default:
  926. reader.Skip();
  927. break;
  928. }
  929. }
  930. }
  931. }
  932. private void FetchFromKeywordsNode(XmlReader reader, IHasKeywords item)
  933. {
  934. reader.MoveToContent();
  935. while (reader.Read())
  936. {
  937. if (reader.NodeType == XmlNodeType.Element)
  938. {
  939. switch (reader.Name)
  940. {
  941. case "PlotKeyword":
  942. {
  943. var tag = reader.ReadElementContentAsString();
  944. if (!string.IsNullOrWhiteSpace(tag))
  945. {
  946. item.AddKeyword(tag);
  947. }
  948. break;
  949. }
  950. default:
  951. reader.Skip();
  952. break;
  953. }
  954. }
  955. }
  956. }
  957. /// <summary>
  958. /// Fetches the data from persons node.
  959. /// </summary>
  960. /// <param name="reader">The reader.</param>
  961. /// <param name="item">The item.</param>
  962. private void FetchDataFromPersonsNode(XmlReader reader, T item)
  963. {
  964. reader.MoveToContent();
  965. while (reader.Read())
  966. {
  967. if (reader.NodeType == XmlNodeType.Element)
  968. {
  969. switch (reader.Name)
  970. {
  971. case "Person":
  972. case "Actor":
  973. {
  974. using (var subtree = reader.ReadSubtree())
  975. {
  976. foreach (var person in GetPersonsFromXmlNode(subtree))
  977. {
  978. if (string.IsNullOrWhiteSpace(person.Name))
  979. {
  980. continue;
  981. }
  982. item.AddPerson(person);
  983. }
  984. }
  985. break;
  986. }
  987. default:
  988. reader.Skip();
  989. break;
  990. }
  991. }
  992. }
  993. }
  994. private void FetchDataFromTrailersNode(XmlReader reader, IHasTrailers item)
  995. {
  996. reader.MoveToContent();
  997. while (reader.Read())
  998. {
  999. if (reader.NodeType == XmlNodeType.Element)
  1000. {
  1001. switch (reader.Name)
  1002. {
  1003. case "Trailer":
  1004. {
  1005. var val = reader.ReadElementContentAsString();
  1006. if (!string.IsNullOrWhiteSpace(val))
  1007. {
  1008. item.AddTrailerUrl(val, false);
  1009. }
  1010. break;
  1011. }
  1012. default:
  1013. reader.Skip();
  1014. break;
  1015. }
  1016. }
  1017. }
  1018. }
  1019. protected List<ChapterInfo> FetchChaptersFromXmlNode(BaseItem item, XmlReader reader)
  1020. {
  1021. using (reader)
  1022. {
  1023. return GetChaptersFromXmlNode(reader)
  1024. .Where(i => i.StartPositionTicks >= 0)
  1025. .ToList();
  1026. }
  1027. }
  1028. private IEnumerable<ChapterInfo> GetChaptersFromXmlNode(XmlReader reader)
  1029. {
  1030. var chapters = new List<ChapterInfo>();
  1031. reader.MoveToContent();
  1032. while (reader.Read())
  1033. {
  1034. if (reader.NodeType == XmlNodeType.Element)
  1035. {
  1036. switch (reader.Name)
  1037. {
  1038. case "Chapter":
  1039. {
  1040. using (var subtree = reader.ReadSubtree())
  1041. {
  1042. chapters.Add(GetChapterInfoFromXmlNode(subtree));
  1043. }
  1044. break;
  1045. }
  1046. default:
  1047. reader.Skip();
  1048. break;
  1049. }
  1050. }
  1051. }
  1052. return chapters;
  1053. }
  1054. private ChapterInfo GetChapterInfoFromXmlNode(XmlReader reader)
  1055. {
  1056. var chapter = new ChapterInfo();
  1057. reader.MoveToContent();
  1058. while (reader.Read())
  1059. {
  1060. if (reader.NodeType == XmlNodeType.Element)
  1061. {
  1062. switch (reader.Name)
  1063. {
  1064. case "StartPositionMs":
  1065. {
  1066. var val = reader.ReadElementContentAsString();
  1067. var ms = long.Parse(val, _usCulture);
  1068. chapter.StartPositionTicks = TimeSpan.FromMilliseconds(ms).Ticks;
  1069. break;
  1070. }
  1071. case "Name":
  1072. {
  1073. chapter.Name = reader.ReadElementContentAsString();
  1074. break;
  1075. }
  1076. default:
  1077. reader.Skip();
  1078. break;
  1079. }
  1080. }
  1081. }
  1082. return chapter;
  1083. }
  1084. /// <summary>
  1085. /// Fetches from studios node.
  1086. /// </summary>
  1087. /// <param name="reader">The reader.</param>
  1088. /// <param name="item">The item.</param>
  1089. private void FetchFromStudiosNode(XmlReader reader, T item)
  1090. {
  1091. reader.MoveToContent();
  1092. while (reader.Read())
  1093. {
  1094. if (reader.NodeType == XmlNodeType.Element)
  1095. {
  1096. switch (reader.Name)
  1097. {
  1098. case "Studio":
  1099. {
  1100. var studio = reader.ReadElementContentAsString();
  1101. if (!string.IsNullOrWhiteSpace(studio))
  1102. {
  1103. item.AddStudio(studio);
  1104. }
  1105. break;
  1106. }
  1107. default:
  1108. reader.Skip();
  1109. break;
  1110. }
  1111. }
  1112. }
  1113. }
  1114. /// <summary>
  1115. /// Gets the persons from XML node.
  1116. /// </summary>
  1117. /// <param name="reader">The reader.</param>
  1118. /// <returns>IEnumerable{PersonInfo}.</returns>
  1119. private IEnumerable<PersonInfo> GetPersonsFromXmlNode(XmlReader reader)
  1120. {
  1121. var name = string.Empty;
  1122. var type = PersonType.Actor; // If type is not specified assume actor
  1123. var role = string.Empty;
  1124. int? sortOrder = null;
  1125. reader.MoveToContent();
  1126. while (reader.Read())
  1127. {
  1128. if (reader.NodeType == XmlNodeType.Element)
  1129. {
  1130. switch (reader.Name)
  1131. {
  1132. case "Name":
  1133. name = reader.ReadElementContentAsString() ?? string.Empty;
  1134. break;
  1135. case "Type":
  1136. {
  1137. var val = reader.ReadElementContentAsString();
  1138. if (!string.IsNullOrWhiteSpace(val))
  1139. {
  1140. type = val;
  1141. }
  1142. break;
  1143. }
  1144. case "Role":
  1145. {
  1146. var val = reader.ReadElementContentAsString();
  1147. if (!string.IsNullOrWhiteSpace(val))
  1148. {
  1149. role = val;
  1150. }
  1151. break;
  1152. }
  1153. case "SortOrder":
  1154. {
  1155. var val = reader.ReadElementContentAsString();
  1156. if (!string.IsNullOrWhiteSpace(val))
  1157. {
  1158. int intVal;
  1159. if (int.TryParse(val, NumberStyles.Integer, _usCulture, out intVal))
  1160. {
  1161. sortOrder = intVal;
  1162. }
  1163. }
  1164. break;
  1165. }
  1166. default:
  1167. reader.Skip();
  1168. break;
  1169. }
  1170. }
  1171. }
  1172. var personInfo = new PersonInfo
  1173. {
  1174. Name = name.Trim(),
  1175. Role = role,
  1176. Type = type,
  1177. SortOrder = sortOrder
  1178. };
  1179. return new[] { personInfo };
  1180. }
  1181. protected LinkedChild GetLinkedChild(XmlReader reader)
  1182. {
  1183. reader.MoveToContent();
  1184. var linkedItem = new LinkedChild
  1185. {
  1186. Type = LinkedChildType.Manual
  1187. };
  1188. while (reader.Read())
  1189. {
  1190. if (reader.NodeType == XmlNodeType.Element)
  1191. {
  1192. switch (reader.Name)
  1193. {
  1194. case "Name":
  1195. {
  1196. linkedItem.ItemName = reader.ReadElementContentAsString();
  1197. break;
  1198. }
  1199. case "Path":
  1200. {
  1201. linkedItem.Path = reader.ReadElementContentAsString();
  1202. break;
  1203. }
  1204. case "Type":
  1205. {
  1206. linkedItem.ItemType = reader.ReadElementContentAsString();
  1207. break;
  1208. }
  1209. case "Year":
  1210. {
  1211. var val = reader.ReadElementContentAsString();
  1212. if (!string.IsNullOrWhiteSpace(val))
  1213. {
  1214. int rval;
  1215. if (int.TryParse(val, NumberStyles.Integer, _usCulture, out rval))
  1216. {
  1217. linkedItem.ItemYear = rval;
  1218. }
  1219. }
  1220. break;
  1221. }
  1222. default:
  1223. reader.Skip();
  1224. break;
  1225. }
  1226. }
  1227. }
  1228. // This is valid
  1229. if (!string.IsNullOrWhiteSpace(linkedItem.Path))
  1230. {
  1231. return linkedItem;
  1232. }
  1233. return string.IsNullOrWhiteSpace(linkedItem.ItemName) || string.IsNullOrWhiteSpace(linkedItem.ItemType) ? null : linkedItem;
  1234. }
  1235. /// <summary>
  1236. /// Used to split names of comma or pipe delimeted genres and people
  1237. /// </summary>
  1238. /// <param name="value">The value.</param>
  1239. /// <returns>IEnumerable{System.String}.</returns>
  1240. private IEnumerable<string> SplitNames(string value)
  1241. {
  1242. value = value ?? string.Empty;
  1243. // Only split by comma if there is no pipe in the string
  1244. // We have to be careful to not split names like Matthew, Jr.
  1245. var separator = value.IndexOf('|') == -1 && value.IndexOf(';') == -1 ? new[] { ',' } : new[] { '|', ';' };
  1246. value = value.Trim().Trim(separator);
  1247. return string.IsNullOrWhiteSpace(value) ? new string[] { } : Split(value, separator, StringSplitOptions.RemoveEmptyEntries);
  1248. }
  1249. /// <summary>
  1250. /// Provides an additional overload for string.split
  1251. /// </summary>
  1252. /// <param name="val">The val.</param>
  1253. /// <param name="separators">The separators.</param>
  1254. /// <param name="options">The options.</param>
  1255. /// <returns>System.String[][].</returns>
  1256. private static string[] Split(string val, char[] separators, StringSplitOptions options)
  1257. {
  1258. return val.Split(separators, options);
  1259. }
  1260. }
  1261. }