Skip to content

Instantly share code, notes, and snippets.

@JeffreyZhao
Created December 25, 2009 03:11
Show Gist options
  • Save JeffreyZhao/263486 to your computer and use it in GitHub Desktop.
Save JeffreyZhao/263486 to your computer and use it in GitHub Desktop.
static void Main()
{
var languages = GetLanguages();
var targetLanguages = languages.Select(
p => String.Format(
"{0}: {1}",
p.Value,
Translate("圣诞快乐", "zh-CN", p.Key)));
File.WriteAllLines("output.txt", targetLanguages.ToArray());
}
private static Dictionary<string, string> GetLanguages()
{
var url =
"http://translate.googleapis.com" +
"/translate_a/l?client=te&hl=zh-CN&cb=_callbacks_._0g3mb650r";
var webClient = new WebClient();
webClient.Proxy = new WebProxy("127.0.0.1", 8888);
var script = webClient.DownloadString(url);
var json = Regex.Match(script, @"'tl':({.+})}\)").Groups[1].Value;
var serializer = new JavaScriptSerializer();
return serializer.Deserialize<Dictionary<string, string>>(json);
}
private static string Translate(string source, string sl, string tl)
{
var url =
"https://translate.googleapis.com" +
"/translate_a/t?client=te&format=html&v=1.0";
var data = String.Format(
"q={0}&sl={1}&tl={2}&tc=1",
HttpUtility.UrlEncode(source), sl, tl);
var webClient = new WebClient();
webClient.Encoding = Encoding.UTF8;
webClient.Headers.Add(
HttpRequestHeader.UserAgent,
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0;)");
var json = webClient.UploadString(url, data);
var serializer = new JavaScriptSerializer();
return serializer.Deserialize<string>(json);
}
#light
module Parallel
open System
open System.Text.RegularExpressions
open System.Net
open System.Web
open System.Web.Script.Serialization
open System.Collections.Generic
open System.Text
type private LangMap = Dictionary<string, string>
type WebClient with
member c.GetStringAsync(url) =
async {
c.DownloadStringAsync(new Uri(url))
let! args = c.DownloadStringCompleted |> Async.AwaitEvent
return args.Result
}
member c.PostStringAsync(url, data) =
async {
c.UploadStringAsync(new Uri(url), data)
let! args = c.UploadStringCompleted |> Async.AwaitEvent
return args.Result
}
let private getLanguages() =
async {
let url =
"http://translate.googleapis.com" +
"/translate_a/l?client=te&hl=zh-CN&cb=_callbacks_._0g3mb650r"
let webClient = new WebClient()
// let script = webClient.DownloadString(url)
let! script = webClient.GetStringAsync(url)
let json = Regex.Match(script, @"'tl':({.+})}\)").Groups.Item(1).Value
let serializer = new JavaScriptSerializer()
// serializer.Deserialize<LangMap>(json)
return serializer.Deserialize<LangMap>(json)
}
let private translateText (text:string) sl tl =
async {
let url =
"https://translate.googleapis.com" +
"/translate_a/t?client=te&format=html&v=1.0"
let encoded = text |> HttpUtility.UrlEncode
let data = sprintf "q=%s&sl=%s&tl=%s&tc=1" encoded sl tl
let webClient = new WebClient()
webClient.Encoding <- Encoding.UTF8
let userAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0;)"
webClient.Headers.Add(HttpRequestHeader.UserAgent, userAgent)
let! json = webClient.PostStringAsync(url, data) // changed
let serializer = new JavaScriptSerializer()
return serializer.Deserialize<string>(json) // changed
}
let translate text sl =
async {
let! languages = getLanguages()
let! translated =
languages
|> Seq.map (fun p -> translateText text sl p.Key)
|> Async.Parallel
let names = languages |> Seq.map (fun p -> p.Value)
return Seq.zip names translated |> Seq.toArray
}
let translateSeq text sl =
let rec translateSeq' targets acc =
async {
match targets with
| [] -> return acc |> List.rev
| t :: ts ->
let! result = translateText text sl t
return! translateSeq' ts (t :: acc)
}
async {
let! languages = getLanguages()
let targets = languages |> Seq.map (fun p -> p.Key) |> Seq.toList
let! translated = translateSeq' targets List.empty
let names = languages |> Seq.map (fun p -> p.Value)
return Seq.zip names translated |> Seq.toArray
}
#light
module Sync
open System
open System.Text.RegularExpressions
open System.Net
open System.Web
open System.Web.Script.Serialization
open System.Collections.Generic
open System.Text
type private LangMap = Dictionary<string, string>
let private getLanguages() =
let url =
"http://translate.googleapis.com" +
"/translate_a/l?client=te&hl=zh-CN&cb=_callbacks_._0g3mb650r"
let webClient = new WebClient()
let script = webClient.DownloadString(url)
let json = Regex.Match(script, @"'tl':({.+})}\)").Groups.Item(1).Value
let serializer = new JavaScriptSerializer()
serializer.Deserialize<LangMap>(json)
let private translateText (text:string) sl tl =
let url =
"https://translate.googleapis.com" +
"/translate_a/t?client=te&format=html&v=1.0"
let encoded = text |> HttpUtility.UrlEncode
let data = sprintf "q=%s&sl=%s&tl=%s&tc=1" encoded sl tl
let webClient = new WebClient()
webClient.Encoding <- Encoding.UTF8
let userAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0;)"
webClient.Headers.Add(HttpRequestHeader.UserAgent, userAgent)
let json = webClient.UploadString(url, data)
let serializer = new JavaScriptSerializer()
serializer.Deserialize<string>(json)
let translate text sl =
let languages = getLanguages()
let translated =
languages
|> Seq.map (fun p -> translateText text sl p.Key)
let names = languages |> Seq.map (fun p -> p.Value)
Seq.zip names translated |> Seq.toArray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment