diff Agendas/trunk/src/Agendas.Twitter/TwitterPublicador.cs @ 234:cc71f96ac134

Combinar
author juanjose.montesdeocaarbos
date Tue, 04 Oct 2011 20:36:27 -0300
parents 9f61e8555114
children e2c6610b14c7
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Twitter/TwitterPublicador.cs	Tue Oct 04 20:34:46 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Twitter/TwitterPublicador.cs	Tue Oct 04 20:36:27 2011 -0300
@@ -30,7 +30,7 @@
 			_twitterAdapter = twitterAdapter;
 		}
 
-		private const int LIMITE_MENSAJE = 140;
+		private const int LIMITE_MENSAJE = 134;
 
 		public void Publicar(IEnumerable<Track> tracks)
 		{
@@ -42,12 +42,19 @@
 				if (track.Logs.Any(l => l.Propietario == TrackLogPropietario.Twitter && l.Successful)) continue;
 
 				var twitt = BuildTwitt(track);
-				if (!string.IsNullOrWhiteSpace(twitt))
+				if (twitt.Length>0)
 				{
 					try
 					{
-						string message;
-						bool success = _twitterAdapter.Update(twitt, out message);
+						bool success=true;
+						string message=string.Empty;
+						
+						foreach (var t in twitt)
+						{
+							string m;
+							success &= _twitterAdapter.Update(t, out m);
+							message += m;
+						}
 						track.LogAdd(new TrackLog(TrackLogPropietario.Twitter, message, track.Usuario, success));
 					}
 					catch(Exception ex)
@@ -58,15 +65,43 @@
 			}
 		}
 
-		private string BuildTwitt(Track track)
+		private string[] BuildTwitt(Track track)
 		{
 			ITwitterWriter writer;
 			if (!_writers.TryGetValue(track.Accion, out writer))
-				return string.Empty;
+				return new[] {string.Empty};
+
+			var partes = writer.Write(track).Where(t => !string.IsNullOrWhiteSpace(t)).ToArray();
+
+			var twitters = new List<string>();
+
+			string twitter = string.Empty;
+			for (int index = 0; index < partes.Length; index++)
+			{
+				string parte = partes[index];
 
-			string body = writer.Write(track);
+				var nuevo = twitter + parte;
+				if (nuevo.Length < LIMITE_MENSAJE)
+					twitter = nuevo;
+				else
+				{
+					if (index<parte.Length-1)
+						twitter += "...";
+					if (twitters.Any())
+						twitter = "..." + twitter;
 
-			return body.Length > LIMITE_MENSAJE ? body.Substring(0, LIMITE_MENSAJE - 1) : body;
+					twitters.Add(twitter);
+					twitter = parte.Trim();
+				}
+			}
+			if (!string.IsNullOrWhiteSpace(twitter))
+			{
+				if (twitters.Any())
+					twitter = "..." + twitter;
+				twitters.Add(twitter);
+			}
+
+			return twitters.ToArray();
 		}
 	}
 }
\ No newline at end of file