Exemple #1
0
        public static Tweets GetMany(string[] ids, bool embedMedia = false)
        {
            var tweets = new Tweets();

            var statusList = DbTwitterStatus.SelectMany(ids);

            foreach (var twitterStatus in statusList)
            {
                var status = Json.Deserialize<StatusJson>(Encoding.UTF8.GetString(twitterStatus.Data));

                var formattedText = TwitterResources.retweetTrackerContainer;
                var avatarLink = "<img src=\"{0}\"/>";
                var retweetAvatarLink = "<div class=\"retweetAvatar\"><img src=\"{0}\"/></div>";
                var retweeterAvatarLink = "<div class=\"retweeterAvatar\"><img src=\"{0}\"/></div>";
                var userLink = "<a href=\"https://twitter.com/#!/{0}\" target=\"_blank\" class=\"{2}\">@{1}</a>";

                // Retweets are handled differently
                var retweet = "<img class=\"retweetIcon\" src=\"/images/retweet.png\"/>";
                formattedText = formattedText.Replace("{retweet}", (status.Retweet) ? retweet : "");
                if (status.Retweet)
                {
                    var retweetAvatars = string.Format(retweetAvatarLink,
                        (status.User.ProfileImageUrl.Contains(".gif") ? "" : status.User.ProfileImageUrl));
                    retweetAvatars += string.Format(retweeterAvatarLink,
                        (status.RetweetedBy.ProfileImageUrl.Contains(".gif") ? "" : status.RetweetedBy.ProfileImageUrl));
                    formattedText = formattedText.Replace("{avatarLink}", retweetAvatars);

                    formattedText = formattedText.Replace("{avatarLink}",
                        string.Format(avatarLink, status.User.ProfileImageUrl));

                    formattedText =
                        formattedText.Replace("{username}",
                        string.Format(userLink, status.User.ScreenName, status.User.ScreenName,
                        "userProfileLink") + " (RT by " +
                        string.Format(userLink, status.RetweetedBy.ScreenName, status.RetweetedBy.ScreenName, "userProfileLink") + ")");

                    //status = status.RetweetedStatus;
                    // still need to do more work to have this display properly
                }
                else
                {
                    formattedText = formattedText.Replace("{username}",
                                  string.Format(userLink, status.User.ScreenName, status.User.ScreenName,
                                                "userProfileLink"));

                    formattedText = formattedText.Replace("{avatarLink}",
                        string.Format(avatarLink,
                        (status.User.ProfileImageUrl.Contains(".gif")) ? "" : status.User.ProfileImageUrl));
                }

                var text = status.Text;
                var tweet = new Tweet()
                {
                    Id = status.Id,
                    ProfileImageUrl = status.User.ProfileImageUrl,
                    ScreenName = status.User.ScreenName,
                    //RetweetCount = status.RetweetCount,
                    UserId = status.User.Id,
                    UserName = status.User.Name
                };

                var urlLink = "<a href=\"{0}\" target=\"_blank\" class=\"{2}\">{1}</a>";
                var hashtagLink = "<a href=\"https://twitter.com/#!/search?q={0}\" target=\"_blank\" class=\"{2}\">#{1}</a>";
                var twitpicSrc = "<img class=\"twitpicEmbed\" src=\"http://twitpic.com/show/thumb/{0}\"/>";
                var twitterPicSrc = "<img class=\"twitterPicEmbed\" src=\"{0}\"/>";
                var imgurPicSrc = "<img class=\"imgurPicEmbed\" src=\"{0}\"/>";

                string imageEmbeds = "";

                if (status.Entities != null)
                {
                    if (embedMedia &&  status.Entities.Media != null && status.Entities.Media.Count > 0)
                    {
                        foreach (var photo in status.Entities.Media)
                        {
                            imageEmbeds += string.Format(twitterPicSrc, photo.media_url + ":thumb");
                        }
                    }

                    foreach (var url in status.Entities.Urls)
                    {
                        // all urls are validated -- especially when text matching to make
                        // sure it really is a link to the specific site and not a path
                        if (embedMedia && url.expanded_url != null && url.expanded_url.Contains("twitpic.com"))
                        {
                            Uri twitpicUri;
                            if (Uri.TryCreate(url.expanded_url, UriKind.Absolute, out twitpicUri) &&
                                twitpicUri.Host.Contains("twitpic.com"))
                            {
                                var twitpicImage = string.Format(twitpicSrc, new Uri(url.expanded_url).Segments[1]);
                                imageEmbeds += twitpicImage;
                            }
                        }
                        else if (embedMedia && url.expanded_url != null && url.expanded_url.Contains("imgur.com"))
                        {
                            Uri imgurUri;
                            if (Uri.TryCreate(url.expanded_url, UriKind.Absolute, out imgurUri) &&
                                imgurUri.Host.Contains("imgur.com"))
                            {
                                imageEmbeds += string.Format(imgurPicSrc, url.expanded_url);
                            }
                        }
                        else if (embedMedia && url.expanded_url != null && url.expanded_url.Contains("lockerz.com"))
                        {
                            var lockerzImage = new Uri(url.expanded_url);

                        }
                        else
                        {
                            Uri validatedUrl;
                            string displayUrl = "";
                            // validate the url. The only one we care about is the one doing
                            // the actual linking, but we may have to fall back to the expanded url,
                            // and finally the display url. On top of all that, twitter may not have
                            // added the protocol, so we have to try that.
                            if (Uri.TryCreate(
                                (url.url != null && (url.url.Contains("http:")
                                                     || url.url.Contains("https:")))
                                    ? url.url
                                    : "http://" + url.url,
                                UriKind.Absolute, out validatedUrl))
                            {
                                displayUrl = url.display_url ?? url.url;
                            }
                            else if (Uri.TryCreate(
                                (url.expanded_url != null && (url.expanded_url.Contains("http:")
                                                              || url.expanded_url.Contains("https:")))
                                    ? url.expanded_url
                                    : "http://" + url.expanded_url
                                , UriKind.Absolute, out validatedUrl))
                            {
                                displayUrl = url.display_url ?? url.expanded_url;
                            }
                            else if (Uri.TryCreate(
                                (url.display_url != null && (url.display_url.Contains("http:")
                                                             || url.display_url.Contains("https:")))
                                    ? url.display_url
                                    : "http://" + url.display_url
                                , UriKind.Absolute, out validatedUrl))
                            {
                                displayUrl = url.display_url;
                            }
                            else
                            {
                                //bad url, log an error
                                break;
                            }
                            // Truncate the display url if it's too long
                            displayUrl = (displayUrl.Length > 40) ? displayUrl.Substring(0, 40) + "..." : displayUrl;

                            var formattedLink = string.Format(urlLink, validatedUrl, displayUrl, "tweetLink");
                            text = text.Replace(url.url, formattedLink);
                        }
                    }

                    foreach (var user in status.Entities.UserMentions)
                    {
                        var userProfile = string.Format(userLink, user.screen_name, user.screen_name, "userProfileLink");
                        text = text.Replace("@" + user.screen_name, userProfile);
                    }

                    foreach (var hashtag in status.Entities.Hashtags)
                    {
                        var tagLink = string.Format(hashtagLink, hashtag.Text, hashtag.Text, "hashtagLink");
                        text = text.Replace("#" + hashtag.Text, tagLink);
                    }
                }

                var words = text.Split(' ');
                var ignoreHtml = false;
                for (int i = 0; i < words.Length; i++)
                {
                    if (words[i].Contains("<"))
                        ignoreHtml = true;
                    else if (words[i].Contains(">"))
                        ignoreHtml = false;

                    if (words[i].Length > 30)
                    {
                        var word = words[i];
                        var sbWord = new StringBuilder();
                        for (int j = 0; j < word.Length; j++)
                        {

                            sbWord.Append(word[j]);

                            if (!ignoreHtml)
                            {
                                if (j > 0 && j % 30 == 0) sbWord.Append(' ');
                            }
                        }
                        words[i] = sbWord.ToString();
                    }
                }
                text = string.Join(" ", words);
                formattedText = formattedText.Replace("{formattedTweetText}", text);
                formattedText = formattedText.Replace("{images}", imageEmbeds);

                //var parsedTime = GeneralUtils.ParseDateTime(status.CreatedAt.FromEpoch());
                formattedText = formattedText.Replace("{timeSent}", status.CreatedAt.FromEpoch().ToLocalTime().ToString("MM/dd HH:mm"));

                formattedText = formattedText.Replace("\n", "");
                formattedText = formattedText.Replace("\t", "");
                tweet.FormattedText = formattedText;
                tweets.Tweet.Add(tweet);
                tweets.Since = twitterStatus.Id.ToString();
            }

            return tweets;
        }
Exemple #2
0
        public static Tweets GetTweetsSince(string since, string where = null)
        {
            var ds = DbTwitterStatus.Select(since, where: where);

            Tweets tweets = new Tweets();

            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    //byte[] msgBytes = Encoding.Unicode.GetBytes(ds.Tables[0].Rows[i]["Data"].ToString());
                    //var ms = new MemoryStream(msgBytes); //(byte[])ds.Tables[0].Rows[i]["Data"]);
                    //var ser = new DataContractJsonSerializer(typeof(StatusJson));
                    //var status = (Status)ser.ReadObject(ms);

                    StatusJson status = Json.Deserialize<StatusJson>(Encoding.UTF8.GetString((byte[])ds.Tables[0].Rows[i]["Data"]));

                    var formattedText = TwitterResources.tweetContainer;
                    var avatarLink = "<img src=\"{0}\"/>";
                    var retweetAvatarLink = "<div class=\"retweetAvatar\"><img src=\"{0}\"/></div>";
                    var retweeterAvatarLink = "<div class=\"retweeterAvatar\"><img src=\"{0}\"/></div>";
                    var userLink = "<a href=\"https://twitter.com/#!/{0}\" target=\"_blank\" class=\"{2}\">@{1}</a>";

                    // Retweets are handled differently
                    var retweet = "<img class=\"retweetIcon\" src=\"/images/retweet.png\"/>";
                    formattedText = formattedText.Replace("{retweet}", (status.Retweet) ? retweet : "");
                    if (status.Retweet)
                    {
                        var retweetAvatars = string.Format(retweetAvatarLink,
                            (status.User.ProfileImageUrl.Contains(".gif") ? "" : status.User.ProfileImageUrl));
                        retweetAvatars += string.Format(retweeterAvatarLink,
                            (status.RetweetedBy.ProfileImageUrl.Contains(".gif") ? "" : status.RetweetedBy.ProfileImageUrl));
                        formattedText = formattedText.Replace("{avatarLink}", retweetAvatars);

                        formattedText = formattedText.Replace("{avatarLink}",
                            string.Format(avatarLink, status.User.ProfileImageUrl));

                        formattedText =
                            formattedText.Replace("{username}",
                            string.Format(userLink, status.User.ScreenName, status.User.ScreenName,
                            "userProfileLink") + " (RT by " +
                            string.Format(userLink, status.RetweetedBy.ScreenName, status.RetweetedBy.ScreenName, "userProfileLink") + ")");

                        //status = status.RetweetedStatus;
                        // still need to do more work to have this display properly
                    }
                    else
                    {
                        formattedText = formattedText.Replace("{username}",
                                      string.Format(userLink, status.User.ScreenName, status.User.ScreenName,
                                                    "userProfileLink"));

                        formattedText = formattedText.Replace("{avatarLink}",
                            string.Format(avatarLink,
                            (status.User.ProfileImageUrl.Contains(".gif")) ? "" : status.User.ProfileImageUrl));
                    }

                    var text = status.Text;
                    var tweet = new Tweet()
                                    {
                                        Id = status.Id,
                                        ProfileImageUrl = status.User.ProfileImageUrl,
                                        ScreenName = status.User.ScreenName,
                                        //RetweetCount = status.RetweetCount,
                                        UserId = status.User.Id,
                                        UserName = status.User.Name
                                    };

                    var urlLink = "<a href=\"{0}\" target=\"_blank\" class=\"{2}\">{1}</a>";
                    var hashtagLink = "<a href=\"https://twitter.com/#!/search?q={0}\" target=\"_blank\" class=\"{2}\">#{1}</a>";
                    var twitpicSrc = "<a href=\"http://twitpic.com/show/large/{0}\" rel=\"prettyPhoto\"><img class=\"twitpicEmbed\" src=\"http://twitpic.com/show/thumb/{0}\"/></a>";
                    var twitterPicSrc = "<img class=\"twitterPicEmbed\" src=\"{0}\"/>";
                    var imgurPicSrc = "<img class=\"imgurPicEmbed\" src=\"{0}\"/>";

                    string imageEmbeds = "";
                    if (status.Entities.Media != null && status.Entities.Media.Count > 0)
                    {
                        foreach (var photo in status.Entities.Media)
                        {
                            imageEmbeds += string.Format(twitterPicSrc, photo.media_url + ":thumb");
                        }
                    }

                    foreach (var url in status.Entities.Urls)
                    {
                        // all urls are validated -- especially when text matching to make
                        // sure it really is a link to the specific site and not a path
                        if (url.expanded_url != null && url.expanded_url.Contains("twitpic.com"))
                        {
                            Uri twitpicUri;
                            if (Uri.TryCreate(url.expanded_url, UriKind.Absolute, out twitpicUri) &&
                                twitpicUri.Host.Contains("twitpic.com"))
                            {
                                var twitpicImage = string.Format(twitpicSrc, new Uri(url.expanded_url).Segments[1]);
                                imageEmbeds += twitpicImage;
                            }
                        }
                        else if (url.expanded_url != null && url.expanded_url.Contains("imgur.com"))
                        {
                            Uri imgurUri;
                            if (Uri.TryCreate(url.expanded_url, UriKind.Absolute, out imgurUri) &&
                                imgurUri.Host.Contains("imgur.com"))
                            {
                                imageEmbeds += string.Format(imgurPicSrc, url.expanded_url);
                            }
                        }
                        else if (url.expanded_url != null && url.expanded_url.Contains("lockerz.com"))
                        {
                            var lockerzImage = new Uri(url.expanded_url);

                        }
                        else
                        {
                            Uri validatedUrl;
                            string displayUrl = "";
                            // validate the url. The only one we care about is the one doing
                            // the actual linking, but we may have to fall back to the expanded url,
                            // and finally the display url. On top of all that, twitter may not have
                            // added the protocol, so we have to try that.
                            if (Uri.TryCreate(
                                (url.url != null && (url.url.Contains("http:")
                                    || url.url.Contains("https:"))) ? url.url : "http://" + url.url,
                                UriKind.Absolute, out validatedUrl))
                            {
                                displayUrl = (url.display_url != null) ? url.display_url : url.url;
                            }
                            else if (Uri.TryCreate(
                                (url.expanded_url != null && (url.expanded_url.Contains("http:")
                                    || url.expanded_url.Contains("https:"))) ? url.expanded_url : "http://" + url.expanded_url
                                , UriKind.Absolute, out validatedUrl))
                            {
                                displayUrl = (url.display_url != null) ? url.display_url : url.expanded_url;
                            }
                            else if (Uri.TryCreate(
                                (url.display_url != null && (url.display_url.Contains("http:")
                                    || url.display_url.Contains("https:"))) ? url.display_url : "http://" + url.display_url
                                , UriKind.Absolute, out validatedUrl))
                            {
                                displayUrl = url.display_url;
                            }
                            else
                            {
                                //bad url, log an error
                                break;
                            }
                            // Truncate the display url if it's too long
                            displayUrl = (displayUrl.Length > 40) ? displayUrl.Substring(0, 40) + "..." : displayUrl;

                            var formattedLink = string.Format(urlLink, validatedUrl, displayUrl, "tweetLink");
                            text = text.Replace(url.url, formattedLink);
                        }
                    }

                    foreach (var user in status.Entities.UserMentions)
                    {
                        var userProfile = string.Format(userLink, user.screen_name, user.screen_name, "userProfileLink");
                        text = text.Replace("@" + user.screen_name, userProfile);
                    }

                    foreach (var hashtag in status.Entities.Hashtags)
                    {
                        var tagLink = string.Format(hashtagLink, hashtag.Text, hashtag.Text, "hashtagLink");
                        text = text.Replace("#" + hashtag.Text, tagLink);
                    }

                    formattedText = formattedText.Replace("{formattedTweetText}", text);
                    formattedText = formattedText.Replace("{images}", imageEmbeds);

                    //var parsedTime = GeneralUtils.ParseDateTime(status.CreatedAt.FromEpoch());
                    formattedText = formattedText.Replace("{timeSent}", status.CreatedAt.FromEpoch().ToLocalTime().ToString("MM/dd HH:mm"));

                    formattedText = formattedText.Replace("\n", "");
                    formattedText = formattedText.Replace("\t", "");
                    tweet.FormattedText = formattedText;
                    tweets.Tweet.Add(tweet);
                    tweets.Since = ds.Tables[0].Rows[i]["Id"].ToString();
                }
            }
            else
            {
                tweets = GetMaxSince();
            }

            return tweets;
        }