XmlSaverHelpers.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.TV;
  3. using MediaBrowser.Model.Entities;
  4. using System;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Security;
  8. using System.Text;
  9. using System.Xml;
  10. namespace MediaBrowser.Providers.Savers
  11. {
  12. /// <summary>
  13. /// Class XmlHelpers
  14. /// </summary>
  15. public static class XmlSaverHelpers
  16. {
  17. /// <summary>
  18. /// The us culture
  19. /// </summary>
  20. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  21. /// <summary>
  22. /// Saves the specified XML.
  23. /// </summary>
  24. /// <param name="xml">The XML.</param>
  25. /// <param name="path">The path.</param>
  26. public static void Save(StringBuilder xml, string path)
  27. {
  28. var xmlDocument = new XmlDocument();
  29. xmlDocument.LoadXml(xml.ToString());
  30. //Add the new node to the document.
  31. xmlDocument.InsertBefore(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", "yes"), xmlDocument.DocumentElement);
  32. var parentPath = Path.GetDirectoryName(path);
  33. if (!Directory.Exists(parentPath))
  34. {
  35. Directory.CreateDirectory(parentPath);
  36. }
  37. using (var streamWriter = new StreamWriter(path, false, Encoding.UTF8))
  38. {
  39. xmlDocument.Save(streamWriter);
  40. }
  41. }
  42. /// <summary>
  43. /// Adds the common nodes.
  44. /// </summary>
  45. /// <param name="item">The item.</param>
  46. /// <param name="builder">The builder.</param>
  47. public static void AddCommonNodes(BaseItem item, StringBuilder builder)
  48. {
  49. if (!string.IsNullOrEmpty(item.OfficialRating))
  50. {
  51. builder.Append("<ContentRating>" + SecurityElement.Escape(item.OfficialRating) + "</ContentRating>");
  52. builder.Append("<MPAARating>" + SecurityElement.Escape(item.OfficialRating) + "</MPAARating>");
  53. builder.Append("<certification>" + SecurityElement.Escape(item.OfficialRating) + "</certification>");
  54. }
  55. if (item.People.Count > 0)
  56. {
  57. builder.Append("<Persons>");
  58. foreach (var person in item.People)
  59. {
  60. builder.Append("<Person>");
  61. builder.Append("<Name>" + SecurityElement.Escape(person.Name) + "</Name>");
  62. builder.Append("<Type>" + SecurityElement.Escape(person.Type) + "</Type>");
  63. builder.Append("<Role>" + SecurityElement.Escape(person.Role) + "</Role>");
  64. builder.Append("</Person>");
  65. }
  66. builder.Append("</Persons>");
  67. }
  68. if (!string.IsNullOrEmpty(item.DisplayMediaType))
  69. {
  70. builder.Append("<Type>" + SecurityElement.Escape(item.DisplayMediaType) + "</Type>");
  71. }
  72. if (!string.IsNullOrEmpty(item.Overview))
  73. {
  74. builder.Append("<Overview><![CDATA[" + item.Overview + "]]></Overview>");
  75. }
  76. if (!string.IsNullOrEmpty(item.CustomRating))
  77. {
  78. builder.Append("<CustomRating>" + SecurityElement.Escape(item.CustomRating) + "</CustomRating>");
  79. }
  80. if (!string.IsNullOrEmpty(item.Name) && !(item is Episode))
  81. {
  82. builder.Append("<LocalTitle>" + SecurityElement.Escape(item.Name) + "</LocalTitle>");
  83. }
  84. if (!string.IsNullOrEmpty(item.ForcedSortName))
  85. {
  86. builder.Append("<SortTitle>" + SecurityElement.Escape(item.ForcedSortName) + "</SortTitle>");
  87. }
  88. if (item.PremiereDate.HasValue)
  89. {
  90. builder.Append("<PremiereDate>" + SecurityElement.Escape(item.PremiereDate.Value.ToString("yyyy-MM-dd")) + "</PremiereDate>");
  91. }
  92. if (item.Budget.HasValue)
  93. {
  94. builder.Append("<Budget>" + SecurityElement.Escape(item.Budget.Value.ToString(UsCulture)) + "</Budget>");
  95. }
  96. if (item.Revenue.HasValue)
  97. {
  98. builder.Append("<Revenue>" + SecurityElement.Escape(item.Revenue.Value.ToString(UsCulture)) + "</Revenue>");
  99. }
  100. if (item.CommunityRating.HasValue)
  101. {
  102. builder.Append("<Rating>" + SecurityElement.Escape(item.CommunityRating.Value.ToString(UsCulture)) + "</Rating>");
  103. }
  104. if (item.ProductionYear.HasValue)
  105. {
  106. builder.Append("<ProductionYear>" + SecurityElement.Escape(item.ProductionYear.Value.ToString(UsCulture)) + "</ProductionYear>");
  107. }
  108. if (!string.IsNullOrEmpty(item.HomePageUrl))
  109. {
  110. builder.Append("<Website>" + SecurityElement.Escape(item.HomePageUrl) + "</Website>");
  111. }
  112. if (!string.IsNullOrEmpty(item.AspectRatio))
  113. {
  114. builder.Append("<AspectRatio>" + SecurityElement.Escape(item.AspectRatio) + "</AspectRatio>");
  115. }
  116. if (!string.IsNullOrEmpty(item.Language))
  117. {
  118. builder.Append("<Language>" + SecurityElement.Escape(item.Language) + "</Language>");
  119. }
  120. if (item.RunTimeTicks.HasValue)
  121. {
  122. var timespan = TimeSpan.FromTicks(item.RunTimeTicks.Value);
  123. builder.Append("<RunningTime>" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "</RunningTime>");
  124. builder.Append("<Runtime>" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "</Runtime>");
  125. }
  126. if (item.Taglines.Count > 0)
  127. {
  128. builder.Append("<TagLine>" + SecurityElement.Escape(item.Taglines[0]) + "</TagLine>");
  129. builder.Append("<TagLines>");
  130. foreach (var tagline in item.Taglines)
  131. {
  132. builder.Append("<Tagline>" + SecurityElement.Escape(tagline) + "</Tagline>");
  133. }
  134. builder.Append("</TagLines>");
  135. }
  136. var imdb = item.GetProviderId(MetadataProviders.Imdb);
  137. if (!string.IsNullOrEmpty(imdb))
  138. {
  139. builder.Append("<IMDB_ID>" + SecurityElement.Escape(imdb) + "</IMDB_ID>");
  140. builder.Append("<IMDB>" + SecurityElement.Escape(imdb) + "</IMDB>");
  141. builder.Append("<IMDbId>" + SecurityElement.Escape(imdb) + "</IMDbId>");
  142. }
  143. var tmdb = item.GetProviderId(MetadataProviders.Tmdb);
  144. if (!string.IsNullOrEmpty(tmdb))
  145. {
  146. builder.Append("<TMDbId>" + SecurityElement.Escape(tmdb) + "</TMDbId>");
  147. }
  148. var tvcom = item.GetProviderId(MetadataProviders.Tvcom);
  149. if (!string.IsNullOrEmpty(tvcom))
  150. {
  151. builder.Append("<TVcomId>" + SecurityElement.Escape(tvcom) + "</TVcomId>");
  152. }
  153. var rt = item.GetProviderId(MetadataProviders.RottenTomatoes);
  154. if (!string.IsNullOrEmpty(rt))
  155. {
  156. builder.Append("<RottenTomatoesId>" + SecurityElement.Escape(rt) + "</RottenTomatoesId>");
  157. }
  158. var mbz = item.GetProviderId(MetadataProviders.Musicbrainz);
  159. if (!string.IsNullOrEmpty(mbz))
  160. {
  161. builder.Append("<MusicbrainzId>" + SecurityElement.Escape(mbz) + "</MusicbrainzId>");
  162. }
  163. var tmdbCollection = item.GetProviderId(MetadataProviders.TmdbCollection);
  164. if (!string.IsNullOrEmpty(tmdbCollection))
  165. {
  166. builder.Append("<CollectionNumber>" + SecurityElement.Escape(tmdbCollection) + "</CollectionNumber>");
  167. }
  168. if (item.Genres.Count > 0)
  169. {
  170. builder.Append("<Genres>");
  171. foreach (var genre in item.Genres)
  172. {
  173. builder.Append("<Genre>" + SecurityElement.Escape(genre) + "</Genre>");
  174. }
  175. builder.Append("</Genres>");
  176. }
  177. if (item.Studios.Count > 0)
  178. {
  179. builder.Append("<Studios>");
  180. foreach (var studio in item.Studios)
  181. {
  182. builder.Append("<Studio>" + SecurityElement.Escape(studio) + "</Studio>");
  183. }
  184. builder.Append("</Studios>");
  185. }
  186. if (item.Tags.Count > 0)
  187. {
  188. builder.Append("<Tags>");
  189. foreach (var tag in item.Tags)
  190. {
  191. builder.Append("<Tag>" + SecurityElement.Escape(tag) + "</Tag>");
  192. }
  193. builder.Append("</Tags>");
  194. }
  195. builder.Append("<Added>" + SecurityElement.Escape(item.DateCreated.ToString(UsCulture)) + "</Added>");
  196. }
  197. /// <summary>
  198. /// Appends the media info.
  199. /// </summary>
  200. /// <typeparam name="T"></typeparam>
  201. /// <param name="item">The item.</param>
  202. /// <param name="builder">The builder.</param>
  203. public static void AppendMediaInfo<T>(T item, StringBuilder builder)
  204. where T : BaseItem, IHasMediaStreams
  205. {
  206. builder.Append("<MediaInfo>");
  207. foreach (var stream in item.MediaStreams)
  208. {
  209. if (stream.Type == MediaStreamType.Video)
  210. {
  211. builder.Append("<Video>");
  212. if (!string.IsNullOrEmpty(stream.Codec))
  213. {
  214. builder.Append("<Codec>" + SecurityElement.Escape(stream.Codec) + "</Codec>");
  215. builder.Append("<FFCodec>" + SecurityElement.Escape(stream.Codec) + "</FFCodec>");
  216. }
  217. if (stream.BitRate.HasValue)
  218. {
  219. builder.Append("<BitRate>" + stream.BitRate.Value.ToString(UsCulture) + "</BitRate>");
  220. }
  221. if (stream.Width.HasValue)
  222. {
  223. builder.Append("<Width>" + stream.Width.Value.ToString(UsCulture) + "</Width>");
  224. }
  225. if (stream.Height.HasValue)
  226. {
  227. builder.Append("<Height>" + stream.Height.Value.ToString(UsCulture) + "</Height>");
  228. }
  229. if (!string.IsNullOrEmpty(stream.AspectRatio))
  230. {
  231. builder.Append("<AspectRatio>" + SecurityElement.Escape(stream.AspectRatio) + "</AspectRatio>");
  232. }
  233. var framerate = stream.AverageFrameRate ?? stream.RealFrameRate;
  234. if (framerate.HasValue)
  235. {
  236. builder.Append("<FrameRate>" + framerate.Value.ToString(UsCulture) + "</FrameRate>");
  237. }
  238. if (!string.IsNullOrEmpty(stream.Language))
  239. {
  240. builder.Append("<Language>" + SecurityElement.Escape(stream.Language) + "</Language>");
  241. }
  242. if (!string.IsNullOrEmpty(stream.ScanType))
  243. {
  244. builder.Append("<ScanType>" + SecurityElement.Escape(stream.ScanType) + "</ScanType>");
  245. }
  246. if (item.RunTimeTicks.HasValue)
  247. {
  248. var timespan = TimeSpan.FromTicks(item.RunTimeTicks.Value);
  249. builder.Append("<Duration>" + Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture) + "</Duration>");
  250. builder.Append("<DurationSeconds>" + Convert.ToInt32(timespan.TotalSeconds).ToString(UsCulture) + "</DurationSeconds>");
  251. }
  252. builder.Append("<Default>" + SecurityElement.Escape(stream.IsDefault.ToString()) + "</Default>");
  253. builder.Append("<Forced>" + SecurityElement.Escape(stream.IsForced.ToString()) + "</Forced>");
  254. builder.Append("</Video>");
  255. }
  256. else if (stream.Type == MediaStreamType.Audio)
  257. {
  258. builder.Append("<Audio>");
  259. if (!string.IsNullOrEmpty(stream.Codec))
  260. {
  261. builder.Append("<Codec>" + SecurityElement.Escape(stream.Codec) + "</Codec>");
  262. builder.Append("<FFCodec>" + SecurityElement.Escape(stream.Codec) + "</FFCodec>");
  263. }
  264. if (stream.Channels.HasValue)
  265. {
  266. builder.Append("<Channels>" + stream.Channels.Value.ToString(UsCulture) + "</Channels>");
  267. }
  268. if (stream.BitRate.HasValue)
  269. {
  270. builder.Append("<BitRate>" + stream.BitRate.Value.ToString(UsCulture) + "</BitRate>");
  271. }
  272. if (stream.SampleRate.HasValue)
  273. {
  274. builder.Append("<SamplingRate>" + stream.SampleRate.Value.ToString(UsCulture) + "</SamplingRate>");
  275. }
  276. if (!string.IsNullOrEmpty(stream.Language))
  277. {
  278. builder.Append("<Language>" + SecurityElement.Escape(stream.Language) + "</Language>");
  279. }
  280. builder.Append("<Default>" + SecurityElement.Escape(stream.IsDefault.ToString()) + "</Default>");
  281. builder.Append("<Forced>" + SecurityElement.Escape(stream.IsForced.ToString()) + "</Forced>");
  282. builder.Append("</Audio>");
  283. }
  284. else if (stream.Type == MediaStreamType.Subtitle)
  285. {
  286. builder.Append("<Subtitle>");
  287. if (!string.IsNullOrEmpty(stream.Language))
  288. {
  289. builder.Append("<Language>" + SecurityElement.Escape(stream.Language) + "</Language>");
  290. }
  291. builder.Append("<Default>" + SecurityElement.Escape(stream.IsDefault.ToString()) + "</Default>");
  292. builder.Append("<Forced>" + SecurityElement.Escape(stream.IsForced.ToString()) + "</Forced>");
  293. builder.Append("</Subtitle>");
  294. }
  295. }
  296. builder.Append("</MediaInfo>");
  297. }
  298. }
  299. }