using System; using System.Collections.Generic; namespace MahTweets.Extensions.Twitter.Mango { internal static class TwitterHelper { public const string RetweetPrefix = "RT @"; public const string MentionPrefix = "@"; public const string DirectmessagePrefix = "d "; public const int TweetLength = 140; public const string TimelineSetting = "TimelineFrequency"; public const string DirectmsgSetting = "DirectMessageFrequency"; public const string MentionSetting = "MentionFrequency"; public const string RetweetSetting = "RetweetFrequency"; public const string ListsSetting = "ListsFrequency"; public const string TweetsPerCallSetting = "TweetsPerCall"; public const string GeotagSetting = "GeoTag"; public const string UserstreamSetting = "UserStream"; public static IList SplitIntoMessages(string text) { var outputTweets = new List(); // don't need to split, return single item in list if (text.Length <= TweetLength) { 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; var prefix = GetTweetPrefix(text, out contents); var array = contents.Split(new[] {' '}); var maximumLength = TweetLength - 2 - prefix.Length; var str = string.Empty; foreach (var t in array) { if ((str.Length + 1 + t.Length) <= maximumLength) { str = string.Concat(str, " ", t); } else { outputTweets.Add(prefix + str.Trim()); str = t; } } // add final tweet outputTweets.Add(prefix + str.Trim()); return outputTweets; } public static string GetTweetPrefix(string text, out string contents) { var prefix = string.Empty; if (text.StartsWith(MentionPrefix, StringComparison.InvariantCultureIgnoreCase)) { // check each word and remove multiple '@'s if they're found var allWords = text.Split(new[] {' '}); var mentionNames = new List(); foreach (var t in allWords) { if (t.StartsWith("@") && t.Length > 1) mentionNames.Add(t); 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(DirectmessagePrefix, StringComparison.InvariantCultureIgnoreCase)) { // we have a DM - get the prefix and the name prefix = text.Substring(0, text.IndexOf(" ", DirectmessagePrefix.Length + 1)); } else if (text.StartsWith(RetweetPrefix, StringComparison.InvariantCultureIgnoreCase)) { // we have a RT - get the prefix and the name prefix = text.Substring(0, text.IndexOf(" ", RetweetPrefix.Length + 1)); } if (prefix != string.Empty) { prefix = string.Concat(prefix, " "); contents = text.Replace(prefix, string.Empty); } else { contents = text; } return prefix; } } }