public void Download(WebSong webSong, string downloadPath) { using (var webClient = new WebClient()) { webClient.DownloadFileAsync(new Uri(webSong.AudioUrl), Path.Combine(downloadPath, webSong.Artist + " - " + webSong.Name)); } }
public async Task<List<WebSong>> SearchPleer(string title, string artist, string album = null, bool checkAllLinks = false) { var url = string.Format("http://pleer.com/search?page=1&q={0}&sort_mode=0&sort_by=0&quality=best&onlydata=true", CreateQuery(title, artist, album)); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest"); using (var resp = await client.GetAsync(url)) { if (!resp.IsSuccessStatusCode) return null; var json = await resp.Content.ReadAsStringAsync(); var o = JToken.Parse(json); if (!o.Value<bool>("success")) return null; var html = o.Value<string>("html"); var doc = new HtmlDocument(); doc.LoadHtml(html); var songNodes = doc.DocumentNode.Descendants("li"); var songs = new List<WebSong>(); foreach (var songNode in songNodes) { var song = new WebSong { Id = songNode.Attributes["file_id"].Value, Name = songNode.Attributes["song"].Value, Artist = songNode.Attributes["singer"].Value }; int bitRate; if (int.TryParse(songNode.Attributes["rate"].Value.Replace(" Kb/s", ""), out bitRate)) { song.BitRate = bitRate; } int seconds; if (int.TryParse(songNode.Attributes["duration"].Value, out seconds)) { song.Duration = TimeSpan.FromSeconds(seconds); } var linkId = songNode.Attributes["link"].Value; song.AudioUrl = await GetPleerLinkAsync(client, linkId); if (string.IsNullOrEmpty(song.AudioUrl)) continue; songs.Add(song); } return await IdentifyMatches(songs, title, artist, checkAllLinks); } } }
private async Task<bool> IsUrlOnlineAsync(WebSong song) { try { using (var client = new HttpClient()) { client.Timeout = TimeSpan.FromSeconds(10); using (var resp = await client.SendAsync( new HttpRequestMessage(HttpMethod.Head, new Uri(song.AudioUrl)), HttpCompletionOption.ResponseHeadersRead)) { if (!resp.IsSuccessStatusCode) return false; if (!resp.Content.Headers.ContentType.MediaType.Contains("audio") && !resp.Content.Headers.ContentType.MediaType.Contains("octet-stream")) { return false; } var size = resp.Content.Headers.ContentLength; if (size != null) { song.ByteSize = (long)size; song.Size = Utilities.BytesToString((long)size); } return song.ByteSize > 0; } } } catch { return false; } }