A good way to automatically download mp3 English words from Google and the translation app

Source: Internet
Author: User

I have been learning English recently, and many words are not very easy to read. I have to go to iciba.com and dreye.com every time. It is very troublesome!

Later I thought, could I write a program to automatically download it? Test it and get it done!

All the audio mp3 files downloaded are real-person voices. The results are amazing! Google love word overlord is female pronunciation, and the translation is male pronunciation, each of which has its own merits.

 

Core code:

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 downmp3fromicba (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 buffer size = 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;
}

}

 

Interface Example:

Software Download:

WordMp3Downloader.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.