Mercurial > altnet-hispano
comparison 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 |
comparison
equal
deleted
inserted
replaced
233:32e4e0e7a140 | 234:cc71f96ac134 |
---|---|
28 }; | 28 }; |
29 | 29 |
30 _twitterAdapter = twitterAdapter; | 30 _twitterAdapter = twitterAdapter; |
31 } | 31 } |
32 | 32 |
33 private const int LIMITE_MENSAJE = 140; | 33 private const int LIMITE_MENSAJE = 134; |
34 | 34 |
35 public void Publicar(IEnumerable<Track> tracks) | 35 public void Publicar(IEnumerable<Track> tracks) |
36 { | 36 { |
37 if (!AgendasConfigurationManager.Publicadores.Twitter.Enabled) return; | 37 if (!AgendasConfigurationManager.Publicadores.Twitter.Enabled) return; |
38 | 38 |
40 { | 40 { |
41 //Si ya hicimos el twitt de este track, no volvemos a hacerlo | 41 //Si ya hicimos el twitt de este track, no volvemos a hacerlo |
42 if (track.Logs.Any(l => l.Propietario == TrackLogPropietario.Twitter && l.Successful)) continue; | 42 if (track.Logs.Any(l => l.Propietario == TrackLogPropietario.Twitter && l.Successful)) continue; |
43 | 43 |
44 var twitt = BuildTwitt(track); | 44 var twitt = BuildTwitt(track); |
45 if (!string.IsNullOrWhiteSpace(twitt)) | 45 if (twitt.Length>0) |
46 { | 46 { |
47 try | 47 try |
48 { | 48 { |
49 string message; | 49 bool success=true; |
50 bool success = _twitterAdapter.Update(twitt, out message); | 50 string message=string.Empty; |
51 | |
52 foreach (var t in twitt) | |
53 { | |
54 string m; | |
55 success &= _twitterAdapter.Update(t, out m); | |
56 message += m; | |
57 } | |
51 track.LogAdd(new TrackLog(TrackLogPropietario.Twitter, message, track.Usuario, success)); | 58 track.LogAdd(new TrackLog(TrackLogPropietario.Twitter, message, track.Usuario, success)); |
52 } | 59 } |
53 catch(Exception ex) | 60 catch(Exception ex) |
54 { | 61 { |
55 track.LogAdd(new TrackLog(TrackLogPropietario.Twitter, track.Usuario, ex)); | 62 track.LogAdd(new TrackLog(TrackLogPropietario.Twitter, track.Usuario, ex)); |
56 } | 63 } |
57 } | 64 } |
58 } | 65 } |
59 } | 66 } |
60 | 67 |
61 private string BuildTwitt(Track track) | 68 private string[] BuildTwitt(Track track) |
62 { | 69 { |
63 ITwitterWriter writer; | 70 ITwitterWriter writer; |
64 if (!_writers.TryGetValue(track.Accion, out writer)) | 71 if (!_writers.TryGetValue(track.Accion, out writer)) |
65 return string.Empty; | 72 return new[] {string.Empty}; |
66 | 73 |
67 string body = writer.Write(track); | 74 var partes = writer.Write(track).Where(t => !string.IsNullOrWhiteSpace(t)).ToArray(); |
68 | 75 |
69 return body.Length > LIMITE_MENSAJE ? body.Substring(0, LIMITE_MENSAJE - 1) : body; | 76 var twitters = new List<string>(); |
77 | |
78 string twitter = string.Empty; | |
79 for (int index = 0; index < partes.Length; index++) | |
80 { | |
81 string parte = partes[index]; | |
82 | |
83 var nuevo = twitter + parte; | |
84 if (nuevo.Length < LIMITE_MENSAJE) | |
85 twitter = nuevo; | |
86 else | |
87 { | |
88 if (index<parte.Length-1) | |
89 twitter += "..."; | |
90 if (twitters.Any()) | |
91 twitter = "..." + twitter; | |
92 | |
93 twitters.Add(twitter); | |
94 twitter = parte.Trim(); | |
95 } | |
96 } | |
97 if (!string.IsNullOrWhiteSpace(twitter)) | |
98 { | |
99 if (twitters.Any()) | |
100 twitter = "..." + twitter; | |
101 twitters.Add(twitter); | |
102 } | |
103 | |
104 return twitters.ToArray(); | |
70 } | 105 } |
71 } | 106 } |
72 } | 107 } |