Exemplo n.º 1
0
        /// <summary>
        /// Parses a Facebook feed
        /// </summary>
        public static FacebookFeed ParseFeed(JToken token, Dictionary<ulong, FacebookUser> people)
        {
            FacebookFeed result = new FacebookFeed();

              // Get id, time and actors (source and target)
              result.ID = (string)token["post_id"];
              result.Date = GeneralUtils.UnixTimestampToDateTime((Int64)token["created_time"]);
              if (tokenHasValue(token["actor_id"]))
            result.SourceUser = people[(UInt64)token["actor_id"]];
              if (tokenHasValue(token["target_id"]))
            result.TargetUser = people[(UInt64)token["target_id"]];

              // Get the core message
              if (tokenHasValue(token["message"]))
              {
            // Get the message tags, null if no tags
            result.MessageTags = getTags(token["message_tags"]);

            // Format our message
            result.Message = removeTabs((string)token["message"]);
            result.XmlMessage = RichTextBoxParser.
              ParseStringToXamlWithTags(result.Message, result.MessageTags);

            result.HasMessage = true; // Used for binding message visibility
              }

              // Description + Description tags + xmal description
              if (tokenHasValue(token["description"]))
              {
            // Get description tags, null if no tags
            result.DescriptionTags = getTags(token["description_tags"]);

            // Format description
            result.Description = (string)token["description"];
            result.XmlDescription = RichTextBoxParser.
              ParseStringToXamlWithTags(result.Description, result.DescriptionTags, true);
              }

              #region Facebook Properties (Like, Comment, Share)

              FacebookItem fbItem = new FacebookItem();

              // Likes
              if (tokenHasValue(token["likes"]["count"]))
              {
            fbItem.LikesCount = (int)token["likes"]["count"];

            // Grab friend likes
            IList<FacebookUser> flikes = new List<FacebookUser>();
            foreach (JToken person in token["likes"]["friends"].Children())
            {
              FacebookUser likedUser = null;
              people.TryGetValue((ulong)person, out likedUser);
              if (likedUser != null)
            flikes.Add(people[(ulong)person]);
            }
            if (flikes.Count > 0)
              fbItem.FriendLikes = flikes;
              }

              // Comments
              if (tokenHasValue(token["comments"]["count"]))
              {
            fbItem.CommentsCount = (int)token["comments"]["count"];

            // Grab some comments
            List<FacebookComment> comments = new List<FacebookComment>();
            foreach (JToken comment in token["comments"]["comment_list"])
              comments.Add(ParseFacebookComment(comment, ref people));
            if (comments.Count > 0)
              fbItem.Comments = comments;
              }

              result.SocialProperties = fbItem;

              #endregion

              #region Facebook Attachment

              // Attachment
              if (tokenHasValue(token["attachment"]) && token.HasValues)
              {
            FacebookAttachment fbAttach = new FacebookAttachment();
            JToken attachment = token["attachment"];

            // Figure out attachment type, href and name
            fbAttach.Type = FacebookAttachment.MediaType.Link; // Defaults to Link type
            if (tokenHasValue(attachment["href"]))
              fbAttach.Source = new Uri((string)attachment["href"]);
            if (tokenHasValue(attachment["name"]))
              fbAttach.Name = removeTabs((string)attachment["name"]);

            // Adjust values if we have some media in the attachment
            if (tokenHasValue(attachment["media"]) && attachment["media"].HasValues)
            {
              // We only use the first media for now
              JToken media = attachment["media"][0];

              // Adjust type
              if (tokenHasValue(media["type"]))
              {
            string type = (string)media["type"];
            if (type.Equals("photo")) fbAttach.Type = FacebookAttachment.MediaType.Image;
            else if (type.Equals("link")) fbAttach.Type = FacebookAttachment.MediaType.Link;
            // TODO: Treat videos as links for now
            else if (type.Equals("video")) fbAttach.Type = FacebookAttachment.MediaType.Link;
            else fbAttach.Type = FacebookAttachment.MediaType.NotSupported;
              }

              // Add Src
              if (tokenHasValue(media["src"]))
              {
            fbAttach.Icon = new Uri((string)media["src"]);
              }

              // Use media href instead of attachment href
              if (tokenHasValue(media["href"]))
              {
            fbAttach.Source = new Uri((string)media["href"]);

            // Adjust source if attachment is a facebook image
            if (fbAttach.Type == FacebookAttachment.MediaType.Image)
              fbAttach.Source = guessLargeImageFromIcon(fbAttach.Icon);
              }

              // Add description if this attachment is a link
              if (fbAttach.Type == FacebookAttachment.MediaType.Link &&
            tokenHasValue(attachment["description"]))
            fbAttach.Description = GeneralUtils.TrimWords((string)attachment["description"], maxLinkLength);
            }

            // Add this attachment to our feed if the attachment is valid (has a source)
            if (fbAttach.Source != null)
              result.Attachment = fbAttach;
              }

              #endregion

              // Finally add the current feedtype
              if (token["typle"] != null)
            result.FeedType = DetermineFacebookFeedType(result, (int)token["type"]);

              // Ignore any type of feeds that we currently do not support
              if ((result.Attachment != null && result.Attachment.Type == FacebookAttachment.MediaType.NotSupported) ||
            (result.Message == null && result.Description == null && result.Attachment == null))
            return null;

              return result;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Guess the type of the facebook feed from the content is has
        /// </summary>
        private static FeedType DetermineFacebookFeedType(FacebookFeed feed, int type)
        {
            FeedType result = FeedType.NotSupported;

              // Figure out type from supplied type param
              if      (type == 56 && feed.TargetUser != null)     result = FeedType.Conversation;
              else if ((type == 80 || type == 128 || type == 247) &&
                feed.Attachment != null)                  result = FeedType.Attachment;
              else if (type == 46 || feed.Message != null)        result = FeedType.Text;
              else                                                result = FeedType.Action;
              return result;
        }