Guest User

Untitled

a guest
Jun 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using System.Collections.Generic;
  12. using System.Xml.Linq;
  13. using Tumblife.Model;
  14. using Tumblife.Util;
  15.  
  16. namespace Tumblife.Tumblr
  17. {
  18. public class TLPostParser
  19. {
  20. public static ICollection<TLPost> Parse(String xml)
  21. {
  22. Log.debug("TLPostParser / Parse");
  23.  
  24. ICollection<TLPost> posts = new List<TLPost>(50);
  25.  
  26. XDocument document = XDocument.Parse(xml);
  27.  
  28. foreach (XElement _post in document.Descendants("post")) {
  29.  
  30. TLPost post = new TLPost();
  31. XElement element;
  32.  
  33. post.Id = (long)_post.Attribute("id");
  34. post.Url = (String)_post.Attribute("url");
  35. post.UrlWithSlug = (String)_post.Attribute("url-with-slug");
  36. post.Type = (String)_post.Attribute("type");
  37. post.DateGmt = (String)_post.Attribute("date-gmt");
  38. post.Date = (String)_post.Attribute("date");
  39. post.UnixTimestamp = (int)_post.Attribute("unix-timestamp");
  40. post.Format = (String)_post.Attribute("format");
  41. post.ReblogKey = (String)_post.Attribute("reblog-key");
  42. post.Slug = (String)_post.Attribute("slug");
  43. post.NoteCount = (int)_post.Attribute("note-count");
  44.  
  45. var tumblelog = _post.Element("tumblelog");
  46. post.TumblelogTitle = (String)tumblelog.Attribute("title");
  47. post.TumblelogName = (String)tumblelog.Attribute("name");
  48. post.TumblelogUrl = (String)tumblelog.Attribute("url");
  49. post.TumblelogTimezone = (String)tumblelog.Attribute("timezone");
  50.  
  51. element = _post.Element("tag");
  52. post.Tag = (element == null) ? null : (String)element.Value;
  53.  
  54. switch (post.Type) {
  55. case "quote":
  56. element = _post.Element("quote-text");
  57. post.QuoteText = (element == null) ? null : (String)element.Value;
  58.  
  59. element = _post.Element("quote-source");
  60. post.QuoteSource = (element == null) ? null : (String)element.Value;
  61.  
  62. break;
  63.  
  64. case "photo":
  65. post.IsPhoto = true;
  66.  
  67. element = _post.Element("photo-caption");
  68. post.PhotoCaption = (element == null) ? null : (String)element.Value;
  69.  
  70. element = _post.Element("photo-link-url");
  71. post.PhotoLinkUrl = (element == null) ? null : (String)element.Value;
  72.  
  73. foreach (XElement photoUrl in _post.Descendants("photo-url")) {
  74. String maxWidth = (String)photoUrl.Attribute("max-width");
  75. String value = (String)photoUrl.Value;
  76. switch (maxWidth) {
  77. case "1280":
  78. post.PhotoUrlMaxWidth1280 = value;
  79. break;
  80. case "500":
  81. post.PhotoUrlMaxWidth500 = value;
  82. break;
  83. case "400":
  84. post.PhotoUrlMaxWidth400 = value;
  85. break;
  86. case "250":
  87. post.PhotoUrlMaxWidth250 = value;
  88. break;
  89. case "100":
  90. post.PhotoUrlMaxWidth100 = value;
  91. break;
  92. case "75":
  93. post.PhotoUrlMaxWidth75 = value;
  94. break;
  95. }
  96. }
  97.  
  98. break;
  99.  
  100. case "regular":
  101. element = _post.Element("regular-title");
  102. post.RegularTitle = (element == null) ? null : (String)element.Value;
  103.  
  104. element = _post.Element("regular-body");
  105. post.RegularBody = (element == null) ? null : (String)element.Value;
  106.  
  107. break;
  108.  
  109. case "link":
  110. element = _post.Element("link-text");
  111. post.LinkText = (element == null) ? null : (String)element.Value;
  112.  
  113. element = _post.Element("link-url");
  114. post.LinkUrl = (element == null) ? null : (String)element.Value;
  115.  
  116. element = _post.Element("link-description");
  117. post.LinkDescription = (element == null) ? null : (String)element.Value;
  118.  
  119. break;
  120.  
  121. case "video":
  122. element = _post.Element("video-caption");
  123. post.VideoCaption = (element == null) ? null : (String)element.Value;
  124.  
  125. element = _post.Element("video-source");
  126. post.VideoSource = (element == null) ? null : (String)element.Value;
  127.  
  128. element = _post.Element("video-player");
  129. post.VideoPlayer = (element == null) ? null : (String)element.Value;
  130.  
  131. break;
  132.  
  133. case "audio":
  134. element = _post.Element("audio-caption");
  135. post.AudioCaption = (element == null) ? null : (String)element.Value;
  136.  
  137. element = _post.Element("audio-player");
  138. post.AudioPlayer = (element == null) ? null : (String)element.Value;
  139.  
  140. element = _post.Element("download-url");
  141. post.DownloadUrl = (element == null) ? null : (String)element.Value;
  142.  
  143. break;
  144.  
  145. case "conversation":
  146. element = _post.Element("conversation-title");
  147. post.ConversationTitle = (element == null) ? null : (String)element.Value;
  148.  
  149. element = _post.Element("conversation-text");
  150. post.ConversationText = (element == null) ? null : (String)element.Value;
  151.  
  152. String lines = "";
  153. foreach (XElement line in _post.Descendants("line")) {
  154. String label = (String)line.Attribute("label");
  155. String value = (String)line.Value;
  156. lines += "<p>" + label + value + "</p>";
  157. }
  158. post.Conversation = lines;
  159.  
  160. break;
  161. }
  162.  
  163. posts.Add(post);
  164. }
  165.  
  166. return posts;
  167. }
  168. }
  169. }
Add Comment
Please, Sign In to add comment