Merge commit 'upstream/0.2.7'
[debian/pino.git] / src / url_short.vala
1 /* url_short.vala
2  *
3  * Copyright (C) 2009-2010  troorl
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author:
19  *      troorl <troorl@gmail.com>
20  */
21
22 using Soup;
23 using Gee;
24 using RestAPI;
25
26 public class Position : Object {
27         public int start;
28         public int end;
29         
30         public Position(int _start, int _end) {
31                 start = _start;
32                 end = _end;
33         }
34 }
35
36 public class UrlShort : Object {
37         
38         Prefs prefs;
39         RestAPIRe api;
40         
41         private Regex urls;
42         
43         public UrlShort(Prefs _prefs, RestAPIRe _api) {
44                 prefs = _prefs;
45                 api = _api;
46                 
47                 urls = new Regex("((http|https|ftp)://([\\S]+)\\.([\\S]+))");
48         }
49         
50         public string shortit(owned string text) {
51                 ArrayList<string> urls = find_urls(text);
52                 if(urls.size == 0)
53                         return text;
54                 
55                 foreach(string url in urls) {
56                         warning(url);
57                         try {
58                                 string new_url = "";
59                                 
60                                 switch(prefs.urlShorten) {
61                                         case "goo.gl":
62                                                 new_url = shorter_google(url);
63                                                 break;
64                                         
65                                         case "is.gd":
66                                                 new_url = shorter_isgd(url);
67                                                 break;
68                                         
69                                         case "ur1.ca":
70                                                 new_url = shorter_ur1(url);
71                                                 break;
72                                 }
73                                 
74                                 warning(new_url);
75                                 
76                                 if(new_url.length > 0)
77                                         text = text.replace(url, new_url);
78                                 
79                         } catch(RestError e) {
80                                 warning(e.message);
81                         }
82                 }
83                 
84                 return text;
85         }
86         
87         public ArrayList<string> find_urls(owned string text, bool len = true) {
88                 ArrayList<string> lst = new ArrayList<string>();
89                 int pos = 0;
90                 
91                 while(true) {
92                         MatchInfo match_info;
93                         bool bingo = urls.match_all_full(text, -1, pos, GLib.RegexMatchFlags.NEWLINE_ANY, out match_info);
94                         
95                         if(bingo) {
96                                 foreach(string s in match_info.fetch_all()) {
97                                         if(len) {
98                                                 if(s.length > 18)
99                                                         lst.add(s);
100                                         } else {
101                                                 lst.add(s);
102                                         }
103                                         
104                                         match_info.fetch_pos(0, null, out pos);
105                                         break;
106                                 }
107                         } else break;
108                 }
109                 
110                 return lst;
111         }
112         
113         /* shorten with http://goo.gl */
114         private string shorter_google(string url) throws RestError {
115                 var map = new HashTable<string, string>(str_hash, str_equal);
116                 map.insert("url", url);
117                 
118                 string req_url = "http://ggl-shortener.appspot.com/";
119                 
120                 string data = api.make_request(req_url, "GET", map);
121                 
122                 return data.split("\"")[3];
123         }
124         
125         /* shorten with http://is.gd */
126         private string shorter_isgd(string url) throws RestError {
127                 var map = new HashTable<string, string>(str_hash, str_equal);
128                 map.insert("longurl", url);
129                 
130                 string req_url = "http://is.gd/api.php";
131                 
132                 return api.make_request(req_url, "GET", map);
133         }
134         
135         /* shorten with http://ur1.ca via Behrooz Shabani <everplays@gmail.com> */
136         private string shorter_ur1(string url) throws RestError {
137                 var map = new HashTable<string, string>(str_hash, str_equal);
138                 map.insert("longurl", url);
139                 map.insert("submit", "Make it an ur1!");
140
141                 string response = api.make_request("http://ur1.ca/", "POST", map);
142                 MatchInfo ur1_match;
143                 var ur1_regex = new Regex("Your ur1 is: <a href=\"([^\"]+)\">");
144                 if(ur1_regex.match(response, 0, out ur1_match))
145                         return (string) ur1_match.fetch(1);
146                 else
147                         return url;
148         }
149 }