Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 343 lines (308 sloc) 10.338 kb
d667e27d »
2012-03-03 プロジェクト名などをリネーム
1 // OpenTween - Client of Twitter
4394983a »
2011-12-31 Add missing copyright notices
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 // (c) 2008-2011 Moz (@syo68k)
4 // (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 // (c) 2011 Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8 // All rights reserved.
9 //
d667e27d »
2012-03-03 プロジェクト名などをリネーム
10 // This file is part of OpenTween.
4394983a »
2011-12-31 Add missing copyright notices
11 //
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 //
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 using System;
91288dbc »
2011-12-11 Port Tween/Google.vb to C#
28 using System.Collections.Generic;
29 using System.Linq;
30 using System.Text;
31 using System.Runtime.Serialization;
32 using System.Net;
33 using System.Runtime.Serialization.Json;
34 using System.Web;
35
d667e27d »
2012-03-03 プロジェクト名などをリネーム
36 namespace OpenTween
91288dbc »
2011-12-11 Port Tween/Google.vb to C#
37 {
38 public class Google
39 {
40 #region "Translation"
41 // http://code.google.com/intl/ja/apis/ajaxlanguage/documentation/#fonje
42 // デベロッパー ガイド - Google AJAX Language API - Google Code
43
44 private const string TranslateEndPoint = "http://ajax.googleapis.com/ajax/services/language/translate";
45 private const string LanguageDetectEndPoint = "https://ajax.googleapis.com/ajax/services/language/detect";
46
47 #region "言語テーブル定義"
48 private static List<string> LanguageTable = new List<string>
49 {
50 "af",
51 "sq",
52 "am",
53 "ar",
54 "hy",
55 "az",
56 "eu",
57 "be",
58 "bn",
59 "bh",
60 "br",
61 "bg",
62 "my",
63 "ca",
64 "chr",
65 "zh",
66 "zh-CN",
67 "zh-TW",
68 "co",
69 "hr",
70 "cs",
71 "da",
72 "dv",
73 "nl",
74 "en",
75 "eo",
76 "et",
77 "fo",
78 "tl",
79 "fi",
80 "fr",
81 "fy",
82 "gl",
83 "ka",
84 "de",
85 "el",
86 "gu",
87 "ht",
88 "iw",
89 "hi",
90 "hu",
91 "is",
92 "id",
93 "iu",
94 "ga",
95 "it",
96 "ja",
97 "jw",
98 "kn",
99 "kk",
100 "km",
101 "ko",
102 "ku",
103 "ky",
104 "lo",
105 "la",
106 "lv",
107 "lt",
108 "lb",
109 "mk",
110 "ms",
111 "ml",
112 "mt",
113 "mi",
114 "mr",
115 "mn",
116 "ne",
117 "no",
118 "oc",
119 "or",
120 "ps",
121 "fa",
122 "pl",
123 "pt",
124 "pt-PT",
125 "pa",
126 "qu",
127 "ro",
128 "ru",
129 "sa",
130 "gd",
131 "sr",
132 "sd",
133 "si",
134 "sk",
135 "sl",
136 "es",
137 "su",
138 "sw",
139 "sv",
140 "syr",
141 "tg",
142 "ta",
143 "tt",
144 "te",
145 "th",
146 "bo",
147 "to",
148 "tr",
149 "uk",
150 "ur",
151 "uz",
152 "ug",
153 "vi",
154 "cy",
155 "yi",
156 "yo",
157 };
158 #endregion
159
160 [DataContract]
161 public class TranslateResponseData
162 {
163 [DataMember(Name = "translatedText")] public string TranslatedText;
164 }
165
166
167 [DataContract]
168 private class TranslateResponse
169 {
bc64ba02 »
2012-03-11 ビルド時に警告が出る箇所の修正
170 [DataMember(Name = "responseData")] public TranslateResponseData ResponseData = null;
171 [DataMember(Name = "responseDetails")] public string ResponseDetails = null;
172 [DataMember(Name = "responseStatus")] public HttpStatusCode ResponseStatus = 0;
91288dbc »
2011-12-11 Port Tween/Google.vb to C#
173 }
174
175
176 [DataContract]
177 public class LanguageDetectResponseData
178 {
179 [DataMember(Name = "language")] public string Language;
180 [DataMember(Name = "isReliable")] public bool IsReliable;
181 [DataMember(Name = "confidence")] public double Confidence;
182 }
183
184 [DataContract]
185 private class LanguageDetectResponse
186 {
bc64ba02 »
2012-03-11 ビルド時に警告が出る箇所の修正
187 [DataMember(Name = "responseData")] public LanguageDetectResponseData ResponseData = null;
188 [DataMember(Name = "responseDetails")] public string ResponseDetails = null;
189 [DataMember(Name = "responseStatus")] public HttpStatusCode ResponseStatus = 0;
91288dbc »
2011-12-11 Port Tween/Google.vb to C#
190 }
191
192 public bool Translate(string srclng, string dstlng, string source, ref string destination, ref string ErrMsg)
193 {
194 var http = new HttpVarious();
195 var apiurl = TranslateEndPoint;
196 var headers = new Dictionary<string, string>();
197 headers.Add("v", "1.0");
198
199 ErrMsg = "";
200 if (string.IsNullOrEmpty(srclng) || string.IsNullOrEmpty(dstlng))
201 {
202 return false;
203 }
204 headers.Add("User-Agent", MyCommon.GetUserAgentString());
205 headers.Add("langpair", srclng + "|" + dstlng);
206
207 headers.Add("q", source);
208
209 var content = "";
210 if (http.GetData(apiurl, headers, out content))
211 {
212 var serializer = new DataContractJsonSerializer(typeof(TranslateResponse));
213 TranslateResponse res;
214
215 try
216 {
217 res = MyCommon.CreateDataFromJson<TranslateResponse>(content);
218 }
219 catch(Exception)
220 {
221 ErrMsg = "Err:Invalid JSON";
222 return false;
223 }
224
225 if (res.ResponseData == null)
226 {
227 ErrMsg = "Err:" + res.ResponseDetails;
228 return false;
229 }
230 var _body = res.ResponseData.TranslatedText;
231 var buf = HttpUtility.UrlDecode(_body);
232
233 destination = string.Copy(buf);
234 return true;
235 }
236 return false;
237 }
238
239 public string LanguageDetect(string source)
240 {
241 var http = new HttpVarious();
242 var apiurl = LanguageDetectEndPoint;
243 var headers = new Dictionary<string, string>();
244 headers.Add("User-Agent", MyCommon.GetUserAgentString());
245 headers.Add("v", "1.0");
246 headers.Add("q", source);
247 var content = "";
248 if (http.GetData(apiurl, headers, out content))
249 {
250 var serializer = new DataContractJsonSerializer(typeof(LanguageDetectResponse));
251 try
252 {
253 var res = MyCommon.CreateDataFromJson<LanguageDetectResponse>(content);
254 return res.ResponseData.Language;
255 }
256 catch(Exception)
257 {
258 return "";
259 }
260 }
261 return "";
262 }
263
264 public string GetLanguageEnumFromIndex(int index)
265 {
266 return LanguageTable[index];
267 }
268
269 public int GetIndexFromLanguageEnum(string lang)
270 {
271 return LanguageTable.IndexOf(lang);
272 }
273 #endregion
274
275 #region "UrlShortener"
276 // http://code.google.com/intl/ja/apis/urlshortener/v1/getting_started.html
277 // Google URL Shortener API
278
279 [DataContract]
280 private class UrlShortenerParameter
281 {
bc64ba02 »
2012-03-11 ビルド時に警告が出る箇所の修正
282 [DataMember(Name = "longUrl")] public string LongUrl = null;
91288dbc »
2011-12-11 Port Tween/Google.vb to C#
283 }
284
285 [DataContract]
286 private class UrlShortenerResponse
287 {
288
289 }
290
291 public string Shorten(string source)
292 {
293 var http = new HttpVarious();
294 var apiurl = "https://www.googleapis.com/urlshortener/v1/url";
295 var headers = new Dictionary<string, string>();
296 headers.Add("User-Agent", MyCommon.GetUserAgentString());
297 headers.Add("Content-Type", "application/json");
298
299 http.PostData(apiurl, headers);
300 return "";
301 }
302 #endregion
303
304 #region "GoogleMaps"
305 public string CreateGoogleStaticMapsUri(GlobalLocation locate)
306 {
307 return CreateGoogleStaticMapsUri(locate.Latitude, locate.Longitude);
308 }
309
310 public string CreateGoogleStaticMapsUri(double lat, double lng)
311 {
43331142 »
2011-12-26 Fix build errors in Google.cs
312 int width = AppendSettingDialog.Instance.FoursquarePreviewWidth;
313 int height = AppendSettingDialog.Instance.FoursquarePreviewHeight;
314 int zoom = AppendSettingDialog.Instance.FoursquarePreviewZoom;
315 string location = lat.ToString() + "," + lng.ToString();
316
317 return "http://maps.google.com/maps/api/staticmap?center=" + location + "&size=" + width + "x" + height + "&zoom=" + zoom + "&markers=" + location + "&sensor=false";
91288dbc »
2011-12-11 Port Tween/Google.vb to C#
318 }
319
320 public string CreateGoogleMapsUri(GlobalLocation locate)
321 {
322 return CreateGoogleMapsUri(locate.Latitude, locate.Longitude);
323 }
324
325 public string CreateGoogleMapsUri(double lat, double lng)
326 {
43331142 »
2011-12-26 Fix build errors in Google.cs
327 int zoom = AppendSettingDialog.Instance.FoursquarePreviewZoom;
328 string location = lat.ToString() + "," + lng.ToString();
329
330 return "http://maps.google.com/maps?ll=" + location + "&z=" + zoom + "&q=" + location;
91288dbc »
2011-12-11 Port Tween/Google.vb to C#
331 }
332
333 public class GlobalLocation
334 {
335 public double Latitude { get; set; }
336 public double Longitude { get; set; }
337 public string LocateInfo { get; set; }
338 }
339
340 #endregion
341 }
342 }
Something went wrong with that request. Please try again.