OSDN Git Service

HttpConnectionクラスにある通信設定の初期化などの処理をNetworkingクラスに移動
[opentween/open-tween.git] / OpenTween / Bing.cs
1 // OpenTween - Client of Twitter
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      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
9 // 
10 // This file is part of OpenTween.
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;
28 using System.Collections.Generic;
29 using System.Net.Http;
30 using System.Net.Http.Headers;
31 using System.Text;
32 using System.Threading.Tasks;
33 using System.Web;
34 using System.Xml.Linq;
35 using OpenTween.Connection;
36
37 namespace OpenTween
38 {
39     public class Bing
40     {
41         private static readonly List<string> LanguageTable = new List<string>
42         {
43             "af",
44             "sq",
45             "ar-sa",
46             "ar-iq",
47             "ar-eg",
48             "ar-ly",
49             "ar-dz",
50             "ar-ma",
51             "ar-tn",
52             "ar-om",
53             "ar-ye",
54             "ar-sy",
55             "ar-jo",
56             "ar-lb",
57             "ar-kw",
58             "ar-ae",
59             "ar-bh",
60             "ar-qa",
61             "eu",
62             "bg",
63             "be",
64             "ca",
65             "zh-tw",
66             "zh-cn",
67             "zh-hk",
68             "zh-sg",
69             "hr",
70             "cs",
71             "da",
72             "nl",
73             "nl-be",
74             "en",
75             "en-us",
76             "en-gb",
77             "en-au",
78             "en-ca",
79             "en-nz",
80             "en-ie",
81             "en-za",
82             "en-jm",
83             "en",
84             "en-bz",
85             "en-tt",
86             "et",
87             "fo",
88             "fa",
89             "fi",
90             "fr",
91             "fr-be",
92             "fr-ca",
93             "fr-ch",
94             "fr-lu",
95             "gd",
96             "ga",
97             "de",
98             "de-ch",
99             "de-at",
100             "de-lu",
101             "de-li",
102             "el",
103             "he",
104             "hi",
105             "hu",
106             "is",
107             "id",
108             "it",
109             "it-ch",
110             "ja",
111             "ko",
112             "ko",
113             "lv",
114             "lt",
115             "mk",
116             "ms",
117             "mt",
118             "no",
119             "no",
120             "pl",
121             "pt-br",
122             "pt",
123             "rm",
124             "ro",
125             "ro-mo",
126             "ru",
127             "ru-mo",
128             "sz",
129             "sr",
130             "sr",
131             "sk",
132             "sl",
133             "sb",
134             "es",
135             "es-mx",
136             "es-gt",
137             "es-cr",
138             "es-pa",
139             "es-do",
140             "es-ve",
141             "es-co",
142             "es-pe",
143             "es-ar",
144             "es-ec",
145             "es-cl",
146             "es-uy",
147             "es-py",
148             "es-bo",
149             "es-sv",
150             "es-hn",
151             "es-ni",
152             "es-pr",
153             "sx",
154             "sv",
155             "sv-fi",
156             "th",
157             "ts",
158             "tn",
159             "tr",
160             "uk",
161             "ur",
162             "ve",
163             "vi",
164             "xh",
165             "ji",
166             "zu",
167         };
168
169         private static readonly string TranslateUri =
170             "https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/v1/Translate";
171
172         protected HttpClient http
173         {
174             get { return this.localHttpClient ?? Networking.Http; }
175         }
176         private readonly HttpClient localHttpClient;
177
178         public Bing()
179             : this(null)
180         {
181         }
182
183         public Bing(HttpClient http)
184         {
185             this.localHttpClient = http;
186         }
187
188         /// <summary>
189         /// Microsoft Translator API を使用した翻訳を非同期に行います
190         /// </summary>
191         /// <exception cref="HttpRequestException"/>
192         public async Task<string> TranslateAsync(string text, string langFrom, string langTo)
193         {
194             var param = new Dictionary<string, string>
195             {
196                 {"Text", "'" + text + "'"},
197                 {"To", "'" + langTo + "'"},
198                 {"$format", "Raw"},
199             };
200
201             if (langFrom != null)
202                 param["From"] = "'" + langFrom + "'";
203
204             var uri = new Uri(TranslateUri + "?" + MyCommon.BuildQueryString(param));
205             var request = new HttpRequestMessage(HttpMethod.Get, uri);
206             request.Headers.Authorization = CreateBasicAuthHeaderValue(ApplicationSettings.AzureMarketplaceKey, ApplicationSettings.AzureMarketplaceKey);
207
208             using (var response = await this.http.SendAsync(request).ConfigureAwait(false))
209             {
210                 response.EnsureSuccessStatusCode();
211
212                 var xmlStr = await response.Content.ReadAsStringAsync()
213                     .ConfigureAwait(false);
214
215                 var xdoc = XDocument.Parse(xmlStr);
216
217                 return xdoc.Root.Value;
218             }
219         }
220
221         public static string GetLanguageEnumFromIndex(int index)
222         {
223             return LanguageTable[index];
224         }
225
226         public static int GetIndexFromLanguageEnum(string lang)
227         {
228             return LanguageTable.IndexOf(lang);
229         }
230
231         internal static AuthenticationHeaderValue CreateBasicAuthHeaderValue(string user, string pass)
232         {
233             var paramBytes = Encoding.UTF8.GetBytes(user + ":" + pass);
234             return new AuthenticationHeaderValue("Basic", Convert.ToBase64String(paramBytes));
235         }
236     }
237 }