annotate Agendas/trunk/src/Agendas.Google/GUrlShortenerAdapter.cs @ 190:e6abe8e1a794

Mejoras menores en shorten url
author nelopauselli
date Tue, 09 Aug 2011 09:45:07 -0300
parents fe47f11f5f20
children 806fd94727ce
rev   line source
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
1 using System.IO;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
2 using System.Net;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
3 using AltNetHispano.Agendas.Configurations;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
4
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
5 namespace AltNetHispano.Agendas.Google
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
6 {
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
7 public class GUrlShortenerAdapter
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
8 {
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
9 private readonly ShortenConfigurationElement _config;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
10
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
11 public GUrlShortenerAdapter()
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
12 {
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
13 _config = AgendasConfigurationManager.Publicadores.Google.Shorten;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
14 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
15
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
16 public string GetShortUrl(string url)
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
17 {
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
18 WebRequest request = WebRequest.Create(_config.UrlAPI);
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
19 request.Method = WebRequestMethods.Http.Post;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
20 request.ContentType = "application/json";
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
21
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
22 using (var writer = new StreamWriter(request.GetRequestStream()))
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
23 {
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
24 var json = string.Format("{{\"longUrl\": \"{0}\"}}", url);
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
25 writer.Write(json);
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
26 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
27
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
28 var response = request.GetResponse() as HttpWebResponse;
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
29
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
30 if (response != null && response.StatusCode == HttpStatusCode.OK)
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
31 {
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
32 var stream = response.GetResponseStream();
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
33
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
34 if (stream != null)
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
35 {
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
36 using (var reader = new StreamReader(stream))
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
37 {
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
38 const string prefix = "\"id\": \"";
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
39
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
40 var json = reader.ReadToEnd();
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
41
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
42 var start = json.IndexOf(prefix) + prefix.Length;
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
43 if (start > prefix.Length)
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
44 {
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
45 var length = json.IndexOf("\",", start) - start;
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
46
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
47 if (length > 0)
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
48 return json.Substring(start, length);
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
49 }
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
50 }
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
51 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
52 }
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
53
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
54 return string.Empty;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
55 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
56 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
57 }