最近在学英语,很多单词不怎么会读.每次得去iciba.com和dreye.com上查,很是麻烦!
后来一想,能不能写个程序自动下载?测试一番,最终搞定!
下载的都是真人语音mp3,效果很棒!谷歌爱词霸是女声发音,译典通是男声发音,各有千秋.
核心的代码:
bool DownMp3FromDrEye(string word, string localPath)
{
HttpWebRequest webRequest = WebRequest.Create("http://www.dreye.com.cn/ews/dict.php") as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
string postString = "w=" + word;
postString = System.Web.HttpUtility.HtmlEncode(postString);
byte[] postData = Encoding.ASCII.GetBytes(postString);
webRequest.ContentLength = postData.Length;
Stream reqStream = webRequest.GetRequestStream();
reqStream.Write(postData, 0, postData.Length);
reqStream.Close();
Stream respStream = webRequest.GetResponse().GetResponseStream();
StreamReader reader = new StreamReader(respStream, true);
string html = reader.ReadToEnd();
respStream.Close();
//Debug.Write(html);
string prefix = "http{add}";
int i = html.IndexOf(prefix);
int k = i + prefix.Length;
string mp3Url = "http:";
while (html.Substring(k,1)!="\"")
{
mp3Url += html.Substring(k, 1);
k++;
}
Debug.WriteLine("Word:" + word + ", URL; " + mp3Url);
if (!mp3Url.EndsWith(".mp3"))
{
return false;
}
SetLog("Downloading from DrEye.com");
SetLog("Mp3 URL: " + mp3Url);
DownFile(mp3Url, Path.Combine(localPath, word + ".mp3"), true);
return true;
}
bool DownMp3FromIciba(string word, string localPath)
{
//_customerDetail = _customerDb.GetCustomerById(1);
string icabaWebUrl = "http://www.iciba.com/";
string wordWebUrl = icabaWebUrl + word + "/";
HttpWebRequest webRequest1 = WebRequest.Create(new Uri(wordWebUrl)) as HttpWebRequest;
HttpWebResponse webResponse1 = webRequest1.GetResponse() as HttpWebResponse;
Stream stream1 = webResponse1.GetResponseStream();
StreamReader reader1 = new StreamReader(stream1);
string html = reader1.ReadToEnd();
string mp3TextStarter = "echoAudio(\"";
int starterIndex = html.IndexOf(mp3TextStarter);
int realIndex = starterIndex + mp3TextStarter.Length;
string mp3Url = "";
while (html.Substring(realIndex, 1) != "\"")
{
mp3Url = mp3Url + html.Substring(realIndex, 1);
realIndex++;
}
Debug.WriteLine("Word:" + word + ", URL; " + mp3Url);
if (!mp3Url.EndsWith(".mp3"))
{
return false;
}
SetLog("Downloading from Iciba.com");
SetLog("Mp3 URL: " + mp3Url);
DownFile(mp3Url, Path.Combine(localPath, word + ".mp3"), true);
stream1.Close();
return true;
}
void DownFile(string fileUrl, string localPath, bool replace)
{
if (File.Exists(localPath) && !replace)
{
return;
}
try
{
HttpWebRequest webRequest2 = WebRequest.Create(new Uri(fileUrl)) as HttpWebRequest;
Stream stream2 = webRequest2.GetResponse().GetResponseStream();//TODO:Don't use GetRequestStream() directly.
StreamReader sr2 = new StreamReader(stream2);
FileStream localFile = new FileStream(localPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter writer = new StreamWriter(localFile);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int readcount = stream2.Read(buffer, 0, bufferSize);
while (readcount > 0)
{
localFile.Write(buffer, 0, readcount);
readcount = stream2.Read(buffer, 0, bufferSize);
}
localFile.Close();
stream2.Close();
}
catch (Exception)
{
throw;
}
}
{
HttpWebRequest webRequest = WebRequest.Create("http://www.dreye.com.cn/ews/dict.php") as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
string postString = "w=" + word;
postString = System.Web.HttpUtility.HtmlEncode(postString);
byte[] postData = Encoding.ASCII.GetBytes(postString);
webRequest.ContentLength = postData.Length;
Stream reqStream = webRequest.GetRequestStream();
reqStream.Write(postData, 0, postData.Length);
reqStream.Close();
Stream respStream = webRequest.GetResponse().GetResponseStream();
StreamReader reader = new StreamReader(respStream, true);
string html = reader.ReadToEnd();
respStream.Close();
//Debug.Write(html);
string prefix = "http{add}";
int i = html.IndexOf(prefix);
int k = i + prefix.Length;
string mp3Url = "http:";
while (html.Substring(k,1)!="\"")
{
mp3Url += html.Substring(k, 1);
k++;
}
Debug.WriteLine("Word:" + word + ", URL; " + mp3Url);
if (!mp3Url.EndsWith(".mp3"))
{
return false;
}
SetLog("Downloading from DrEye.com");
SetLog("Mp3 URL: " + mp3Url);
DownFile(mp3Url, Path.Combine(localPath, word + ".mp3"), true);
return true;
}
bool DownMp3FromIciba(string word, string localPath)
{
//_customerDetail = _customerDb.GetCustomerById(1);
string icabaWebUrl = "http://www.iciba.com/";
string wordWebUrl = icabaWebUrl + word + "/";
HttpWebRequest webRequest1 = WebRequest.Create(new Uri(wordWebUrl)) as HttpWebRequest;
HttpWebResponse webResponse1 = webRequest1.GetResponse() as HttpWebResponse;
Stream stream1 = webResponse1.GetResponseStream();
StreamReader reader1 = new StreamReader(stream1);
string html = reader1.ReadToEnd();
string mp3TextStarter = "echoAudio(\"";
int starterIndex = html.IndexOf(mp3TextStarter);
int realIndex = starterIndex + mp3TextStarter.Length;
string mp3Url = "";
while (html.Substring(realIndex, 1) != "\"")
{
mp3Url = mp3Url + html.Substring(realIndex, 1);
realIndex++;
}
Debug.WriteLine("Word:" + word + ", URL; " + mp3Url);
if (!mp3Url.EndsWith(".mp3"))
{
return false;
}
SetLog("Downloading from Iciba.com");
SetLog("Mp3 URL: " + mp3Url);
DownFile(mp3Url, Path.Combine(localPath, word + ".mp3"), true);
stream1.Close();
return true;
}
void DownFile(string fileUrl, string localPath, bool replace)
{
if (File.Exists(localPath) && !replace)
{
return;
}
try
{
HttpWebRequest webRequest2 = WebRequest.Create(new Uri(fileUrl)) as HttpWebRequest;
Stream stream2 = webRequest2.GetResponse().GetResponseStream();//TODO:Don't use GetRequestStream() directly.
StreamReader sr2 = new StreamReader(stream2);
FileStream localFile = new FileStream(localPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter writer = new StreamWriter(localFile);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int readcount = stream2.Read(buffer, 0, bufferSize);
while (readcount > 0)
{
localFile.Write(buffer, 0, readcount);
readcount = stream2.Read(buffer, 0, bufferSize);
}
localFile.Close();
stream2.Close();
}
catch (Exception)
{
throw;
}
}
界面示例:
软件下载: