using System; using System.Collections.Generic; namespace MahTweets.Extensions.Twitter { internal static class TwitterHelper { public const string RETWEET_PREFIX = "RT @"; public const string MENTION_PREFIX = "@"; public const string DIRECTMESSAGE_PREFIX = "d "; public const int TWEET_LENGTH = 140; public const string TIMELINE_SETTING = "TimelineFrequency"; public const string DIRECTMSG_SETTING = "DirectMessageFrequency"; public const string MENTION_SETTING = "MentionFrequency"; public const string RETWEET_SETTING = "RetweetFrequency"; public const string LISTS_SETTING = "ListsFrequency"; public const string TWEETSPERCALL_SETTING = "TweetsPerCall"; public const string GEOTAG_SETTING = "GeoTag"; public const string USERSTREAM_SETTING = "UserStream"; public static IList SplitIntoMessages(string text) { var outputTweets = new List(); // don't need to split, return single item in list if (text.Length <= TWEET_LENGTH) { outputTweets.Add(text.Trim()); return outputTweets; } // if we have a Twitter-specific message, extract it so that // we can add it to multiple messages string contents; string prefix = GetTweetPrefix(text, out contents); string[] array = contents.Split(new[] {' '}); int maximumLength = TWEET_LENGTH - 2 - prefix.Length; string str = string.Empty; for (int i = 0; i < array.Length; i++) { if ((str.Length + 1 + array[i].Length) <= maximumLength) { str = string.Concat(str, " ", array[i]); } else { outputTweets.Add(prefix + str.Trim()); str = array[i]; } } // add final tweet outputTweets.Add(prefix + str.Trim()); return outputTweets; } public static string GetTweetPrefix(string text, out string contents) { string prefix = string.Empty; if (text.StartsWith(MENTION_PREFIX, StringComparison.InvariantCultureIgnoreCase)) { // check each word and remove multiple '@'s if they're found string[] allWords = text.Split(new[] {' '}); var mentionNames = new List(); for (int i = 0; i < allWords.Length; i++) { if (allWords[i].StartsWith("@") && allWords[i].Length > 1) mentionNames.Add(allWords[i]); else break; } // we have a mention //prefix = text.Substring(0, text.IndexOf(" ", TwitterHelper.MENTION_PREFIX.Length + 1)); prefix = string.Join(" ", mentionNames.ToArray()); } else if (text.StartsWith(DIRECTMESSAGE_PREFIX, StringComparison.InvariantCultureIgnoreCase)) { // we have a DM - get the prefix and the name prefix = text.Substring(0, text.IndexOf(" ", DIRECTMESSAGE_PREFIX.Length + 1)); } else if (text.StartsWith(RETWEET_PREFIX, StringComparison.InvariantCultureIgnoreCase)) { // we have a RT - get the prefix and the name prefix = text.Substring(0, text.IndexOf(" ", RETWEET_PREFIX.Length + 1)); } if (prefix != string.Empty) { prefix = string.Concat(prefix, " "); contents = text.Replace(prefix, string.Empty); } else { contents = text; } return prefix; } } }