annotate Agendas/trunk/src/Agendas.Google/GUrlShortenerAdapter.cs @ 189:fe47f11f5f20

Adapter para url shorten de google
author nelopauselli
date Tue, 09 Aug 2011 09:38:28 -0300
parents
children e6abe8e1a794
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";
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
21
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
22 using(var writer = new StreamWriter(request.GetRequestStream()))
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
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
28 var response = request.GetResponse();
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
29
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
30 using(var reader = new StreamReader(response.GetResponseStream()))
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
31 {
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
32 const string prefix = "\"id\": \"";
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
33
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
34 var json = reader.ReadToEnd();
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
35
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
36 var start = json.IndexOf(prefix) + prefix.Length;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
37 if (start>prefix.Length)
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
38 {
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
39 var length = json.IndexOf("\",", start) - start;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
40
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
41 if (length>0)
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
42 return json.Substring(start, length);
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
43 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
44 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
45 return string.Empty;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
46 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
47 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
48 }