XmlHelpers.cs 13 KB

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