Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using System.Collections.Generic;
- using System.Xml.Linq;
- using Tumblife.Model;
- using Tumblife.Util;
- namespace Tumblife.Tumblr
- {
- public class TLPostParser
- {
- public static ICollection<TLPost> Parse(String xml)
- {
- Log.debug("TLPostParser / Parse");
- ICollection<TLPost> posts = new List<TLPost>(50);
- XDocument document = XDocument.Parse(xml);
- foreach (XElement _post in document.Descendants("post")) {
- TLPost post = new TLPost();
- XElement element;
- post.Id = (long)_post.Attribute("id");
- post.Url = (String)_post.Attribute("url");
- post.UrlWithSlug = (String)_post.Attribute("url-with-slug");
- post.Type = (String)_post.Attribute("type");
- post.DateGmt = (String)_post.Attribute("date-gmt");
- post.Date = (String)_post.Attribute("date");
- post.UnixTimestamp = (int)_post.Attribute("unix-timestamp");
- post.Format = (String)_post.Attribute("format");
- post.ReblogKey = (String)_post.Attribute("reblog-key");
- post.Slug = (String)_post.Attribute("slug");
- post.NoteCount = (int)_post.Attribute("note-count");
- var tumblelog = _post.Element("tumblelog");
- post.TumblelogTitle = (String)tumblelog.Attribute("title");
- post.TumblelogName = (String)tumblelog.Attribute("name");
- post.TumblelogUrl = (String)tumblelog.Attribute("url");
- post.TumblelogTimezone = (String)tumblelog.Attribute("timezone");
- element = _post.Element("tag");
- post.Tag = (element == null) ? null : (String)element.Value;
- switch (post.Type) {
- case "quote":
- element = _post.Element("quote-text");
- post.QuoteText = (element == null) ? null : (String)element.Value;
- element = _post.Element("quote-source");
- post.QuoteSource = (element == null) ? null : (String)element.Value;
- break;
- case "photo":
- post.IsPhoto = true;
- element = _post.Element("photo-caption");
- post.PhotoCaption = (element == null) ? null : (String)element.Value;
- element = _post.Element("photo-link-url");
- post.PhotoLinkUrl = (element == null) ? null : (String)element.Value;
- foreach (XElement photoUrl in _post.Descendants("photo-url")) {
- String maxWidth = (String)photoUrl.Attribute("max-width");
- String value = (String)photoUrl.Value;
- switch (maxWidth) {
- case "1280":
- post.PhotoUrlMaxWidth1280 = value;
- break;
- case "500":
- post.PhotoUrlMaxWidth500 = value;
- break;
- case "400":
- post.PhotoUrlMaxWidth400 = value;
- break;
- case "250":
- post.PhotoUrlMaxWidth250 = value;
- break;
- case "100":
- post.PhotoUrlMaxWidth100 = value;
- break;
- case "75":
- post.PhotoUrlMaxWidth75 = value;
- break;
- }
- }
- break;
- case "regular":
- element = _post.Element("regular-title");
- post.RegularTitle = (element == null) ? null : (String)element.Value;
- element = _post.Element("regular-body");
- post.RegularBody = (element == null) ? null : (String)element.Value;
- break;
- case "link":
- element = _post.Element("link-text");
- post.LinkText = (element == null) ? null : (String)element.Value;
- element = _post.Element("link-url");
- post.LinkUrl = (element == null) ? null : (String)element.Value;
- element = _post.Element("link-description");
- post.LinkDescription = (element == null) ? null : (String)element.Value;
- break;
- case "video":
- element = _post.Element("video-caption");
- post.VideoCaption = (element == null) ? null : (String)element.Value;
- element = _post.Element("video-source");
- post.VideoSource = (element == null) ? null : (String)element.Value;
- element = _post.Element("video-player");
- post.VideoPlayer = (element == null) ? null : (String)element.Value;
- break;
- case "audio":
- element = _post.Element("audio-caption");
- post.AudioCaption = (element == null) ? null : (String)element.Value;
- element = _post.Element("audio-player");
- post.AudioPlayer = (element == null) ? null : (String)element.Value;
- element = _post.Element("download-url");
- post.DownloadUrl = (element == null) ? null : (String)element.Value;
- break;
- case "conversation":
- element = _post.Element("conversation-title");
- post.ConversationTitle = (element == null) ? null : (String)element.Value;
- element = _post.Element("conversation-text");
- post.ConversationText = (element == null) ? null : (String)element.Value;
- String lines = "";
- foreach (XElement line in _post.Descendants("line")) {
- String label = (String)line.Attribute("label");
- String value = (String)line.Value;
- lines += "<p>" + label + value + "</p>";
- }
- post.Conversation = lines;
- break;
- }
- posts.Add(post);
- }
- return posts;
- }
- }
- }
Add Comment
Please, Sign In to add comment