public Download(String songId, String songType, String rootPath, FinishCallBack func) { inf = new SongInformation(); inf.songId = songId; inf.songType = songType; this.rootPath = rootPath; this.func = func; }
public void StartDownload(Object o) { inf = CatchCommand.GetSongInformation(inf.songId, inf.songType); inf = CatchCommand.AddDownloadPath(inf, rootPath); int t = CatchCommand.DownloadSong(inf, new AsyncCompletedEventHandler(DownloadFinish)); if (t == 0) func(true); else if (t == -1) func(false); }
public bool CheckExist() { if (inf.fileName == "") inf = CatchCommand.GetSongInformation(inf.songId, inf.songType); if (inf.fileName == "") return false; if (inf.file == "") inf = CatchCommand.AddDownloadPath(inf, rootPath); if (inf.file == "") return false; if (File.Exists(inf.path + "\\" + inf.file)) return true; return false; }
public static int DownloadSong(SongInformation inf, AsyncCompletedEventHandler handler) { if (inf == null || inf.fileName == "" || inf.path == "" || inf.file == "") return -1; if (!Directory.Exists(inf.path)) { Directory.CreateDirectory(inf.path); } if (File.Exists(inf.path + "\\" + inf.file)) return 0; WebClient webClient = new WebClient(); webClient.DownloadFileAsync(new Uri(inf.fileName), inf.path + "\\" + inf.file); webClient.DownloadFileCompleted += handler; return 1; }
public static SongInformation AddDownloadPath(SongInformation inf, String rootPath) { if (inf.songName == "" || inf.authorName == "") return inf; inf.path = rootPath + "\\" + MakeDirPath(inf.authorName); inf.file = MakeFilePath(inf.authorName + " - " + inf.songName + inf.fileName.Substring(inf.fileName.LastIndexOf("."))); return inf; }
public static SongInformation GetSongInformation(String songId, String songType) { /*String url = "http://service.5sing.kugou.com/song/getPermission?songId="+songId+"&songType="+songType; Encoding myEncoding = Encoding.GetEncoding("utf-8"); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.Timeout = 5000; req.CookieContainer = cookie; try { HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream()); String str = reader.ReadToEnd(); reader.Close(); //写入class SongInformation result = new SongInformation(); JObject jo = JObject.Parse(str); result.songId = songId; result.songType = songType; if (jo["success"].ToString() == "false") return result; result.songName = jo["data"]["songName"].ToString(); result.authorName = jo["data"]["authorName"].ToString(); result.fileName = jo["data"]["fileName"].ToString(); return result; } catch (Exception e) { return null; }*/ String url = "http://5sing.kugou.com/m/detail/" + songType + "-" + songId + "-1.html"; Encoding myEncoding = Encoding.GetEncoding("utf-8"); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.Timeout = 5000; try { HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream()); String str = reader.ReadToEnd(); reader.Close(); //写入class SongInformation result = new SongInformation(); result.songId = songId; result.songType = songType; String res = Regex.Match(str, @"http://.+preload").ToString(); res = res.Substring(0, res.Length - 9); result.fileName = res; String s = Regex.Match(str, @"<title>.+ - 手机原创音乐基地</title>").ToString(); s = s.Substring(0, s.IndexOf(" - 手机原创音乐基地</title>")); s = s.Substring(0, s.LastIndexOf(" - ")); s = s.Substring(7); result.songName = s.Substring(0, s.LastIndexOf(" - ")); result.authorName = s.Substring(s.LastIndexOf(" - ") + 3); return result; } catch (Exception e) { SongInformation result = new SongInformation(); result.songId = songId; result.songType = songType; return result; } }