public String baiduTranslate(String strToTranslate, String fromLanguage, String toLanguage) { String translatedStr = ""; String encodedStr = HttpUtility.UrlEncode(strToTranslate); String baiduTransBaseUrl = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=YEBdxswo4u7Oe21Y2LYcfz9I&q="; String baiduTransUrl = baiduTransBaseUrl; baiduTransUrl += encodedStr; baiduTransUrl += "&from=" + fromLanguage; // source language baiduTransUrl += "&to=" + toLanguage; // to language crifanLib cri = new crifanLib(); //使用crifanlib库 try { String transRetHtml = cri.getUrlRespHtml(baiduTransUrl); String[] str = transRetHtml.Split(new String[] { "\"dst\"" }, StringSplitOptions.RemoveEmptyEntries); cri.extractSingleStr(":\"(.+?)\"}]}", str[1], out translatedStr); translatedStr = System.Text.RegularExpressions.Regex.Unescape(translatedStr);//解码escape形式的Unicode,即将反斜杠u加上数字的字符 //获得fromLanguage语言 int index = transRetHtml.IndexOf("\",\"to\""); String fl = transRetHtml.Substring(9, index - 9); tts = "http://tts.baidu.com/text2audio?lan=" + fl; tts += "&ie=UTF-8&text=" + encodedStr; } catch { } return(translatedStr); }
public string googleTranslate(String strToTranslate, String fromLanguage, String toLanguage) { String translatedStr = ""; String encodedStr = HttpUtility.UrlEncode(strToTranslate); String googleTransBaseUrl = "http://translate.google.cn/translate_a/single?client=t"; String googleTransUrl = googleTransBaseUrl; googleTransUrl += "&sl=" + fromLanguage; // source language googleTransUrl += "&tl=" + toLanguage; // to language googleTransUrl += "&hl=zh-CN&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&q="; googleTransUrl += encodedStr; crifanLib cri = new crifanLib(); //使用crifanlib库 try { String transRetHtml = cri.getUrlRespHtml(googleTransUrl); int index1 = transRetHtml.IndexOf("\",,,"); String str1 = transRetHtml.Substring(0, index1); int index2 = str1.IndexOf(",[,,"); if (index2 > 0) { str1 = str1.Substring(3, index2 - 3); } else { str1 = str1.Substring(3, str1.Length - 3); } String[] str = str1.Split(new String[] { "],[" }, StringSplitOptions.RemoveEmptyEntries); String ss = ""; foreach (String s in str) { if (cri.extractSingleStr("\"(.+?)\",\".+?\"", s, out ss)) { translatedStr += ss; } } String fl = "en"; String str2 = transRetHtml.Substring(index1 - 7, 8); cri.extractSingleStr("\"(.+?)\"", str2, out fl); tts = "http://translate.google.cn/translate_tts?ie=UTF-8&tl=" + fl; tts += "&total=1&prev=input&q="; } catch { } return(translatedStr); }
// idOrUrl : 955407 or http://www.songtaste.com/song/955407 or http://www.songtaste.com/song/955407/ // songInfo: the ST song info: title, artist, realAddr, ... // errStr: error reason string public bool getSongInfo(string idOrUrl, out songInfo songInfo, out string errStr) { bool getInfoOk = false; errStr = "未知错误!"; songInfo = new songInfo(); string songId = ""; string songUrl = ""; if (!isValidIdOrUrl(idOrUrl, out songId, out songUrl)) { errStr = "无法识别的Songtaste的Id或Url:" + idOrUrl + " !"; return(getInfoOk); } else { songInfo.id = songId; songInfo.url = songUrl; } // here must clear previous cookies // otherwise access html with previous cookies will get fault html: //信息提示: 对不起,该用户不存在! 3 秒钟以后系统将自动跳转! crl.clearCurCookies(); string respHtml = ""; respHtml = crl.getUrlRespHtml(songInfo.url, stHtmlCharset); if (respHtml != "") { //note: some song is illegal: //http://songtaste.com/song/3029002/ //return invalid html content //following can not found valid info // 1. get song title and artist //<p class="mid_tit">╰.很多人都在找的一首伤感韩文丶最近很火﹀淑熙(啦啦啦)丶</p><p></p> string titleP = @"<p\s+?class=""mid_tit"">(?<title>.+?)</p>"; Match foundTitle = (new Regex(titleP)).Match(respHtml); if (foundTitle.Success) { songInfo.title = foundTitle.Groups["title"].Value; songInfo.title = crl.removeInvChrInPath(songInfo.title); songInfo.title = songInfo.title.Trim(); //<h1 class="h1singer">1956.烟蒂</h1> //<h1 class="h1singer">Rie fu</h1> //<h1 class="h1singer"></h1> string singerP = @"<h1\s+?class=""h1singer"">(?<singer>.*?)</h1>"; Match foundSinger = (new Regex(singerP)).Match(respHtml); if (foundSinger.Success) { songInfo.artist = foundSinger.Groups["singer"].Value; //special: http://www.songtaste.com/song/809103/, no singer if (songInfo.artist == "") { songInfo.artist = "Unknown Singer"; } songInfo.artist = crl.removeInvChrInPath(songInfo.artist); songInfo.artist = songInfo.artist.Trim(); // 2. get song real address //<a href="javascript:playmedia1('playicon','player', '5bf271ccad05f95186be764f725e9aaf07e0c7791a89123a9addb2a239179e64c91834c698a9c5d82f1ced3fe51ffc51', '355', '68', 'b3a7a4e64bcd8aabe4cabe0e55b57af5', 'http://m3.', '3015123',0);ListenLog(3015123, 0);"> //http://www.songtaste.com/song/2428041/ contain: //<a href="javascript:playmedia1('playicon','player', 'cachefile33.rayfile.com/12f1/zh-cn/download/d1e8d86a0a9880f697aee789f27383db/preview', '355', '68', 'b3a7a4e64bcd8aabe4cabe0e55b57af5', 'http://224.', '2428041',0);ListenLog(2428041, 0);"> //string songP = @"javascript:playmedia1\('playicon','player', '(?<str>\w+)', '\d+', '\d+', '\w+', '.+?', '(?<sid>\d+)',\d+\);"; string songP = @"javascript:playmedia1\('playicon','player', '(?<str>[^']+)', '\d+', '\d+', '(?<keyStr>\w+)', '(?<urlPref>.+?)', '(?<sid>\d+)',\d+\);"; Regex songRx = new Regex(songP); Match foundSong = songRx.Match(respHtml); if (foundSong.Success) { string str = foundSong.Groups["str"].Value; string urlPref = foundSong.Groups["urlPref"].Value; string keyStr = foundSong.Groups["keyStr"].Value; string sid = foundSong.Groups["sid"].Value; if (str.Contains("/")) { //cachefile33.rayfile.com/12f1/zh-cn/download/d1e8d86a0a9880f697aee789f27383db/preview //to get the suffix string suffix = ""; string mainJsUrl = "http://image.songtaste.com/inc/main.js"; string respHtmlMainJs = crl.getUrlRespHtml(mainJsUrl); // case "b3a7a4e64bcd8aabe4cabe0e55b57af5": // return ".mp3"; string suffixP = @"""" + keyStr + @""":.+?return\s+""(?<suffix>\.\w+)"";"; Regex suffixRx = new Regex(suffixP, RegexOptions.Singleline); Match foundSuffix = suffixRx.Match(respHtmlMainJs); if (foundSuffix.Success) { suffix = foundSuffix.Groups["suffix"].Value; songInfo.realAddr = urlPref + str + suffix; } } else { //5bf271ccad05f95186be764f725e9aaf07e0c7791a89123a9addb2a239179e64c91834c698a9c5d82f1ced3fe51ffc51 Dictionary <string, string> headerDict = new Dictionary <string, string>(); headerDict.Add("x-requested-with", "XMLHttpRequest"); // when click play // access http://songtaste.com/time.php, post data: //str=5bf271ccad05f95186be764f725e9aaf07e0c7791a89123a9addb2a239179e64c91834c698a9c5d82f1ced3fe51ffc51&sid=3015123&t=0 Dictionary <string, string> postDict = new Dictionary <string, string>(); postDict.Add("str", str); postDict.Add("sid", sid); postDict.Add("t", "0"); string getRealAddrUrl = "http://songtaste.com/time.php"; songInfo.realAddr = crl.getUrlRespHtml(getRealAddrUrl, headerDict, stHtmlCharset, postDict); } if (songInfo.realAddr != "") { int lastPoint = songInfo.realAddr.LastIndexOf('.'); int strLen = songInfo.realAddr.Length; songInfo.suffix = songInfo.realAddr.Substring(lastPoint); songInfo.storedName = songInfo.title + " - " + songInfo.artist + songInfo.suffix; getInfoOk = true; } else { errStr = "找不到歌曲的真实下载地址!"; } } else { errStr = "找不到歌曲的真实下载地址!"; } } else { errStr = "找不到歌曲的歌手信息!"; } } else { errStr = "找不到歌曲的标题信息!"; } } else { errStr = "无法获取网页信息!"; } return(getInfoOk); }