BaseItemXmlParser.cs 52 KB

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