예제 #1
0
        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;
                        }
                        return song.ByteSize > 0;
                    }
                }
            }
            catch
            {
                return false;
            }
        }
예제 #2
0
        public async Task<List<WebSong>> SearchMp3Skull(string title, string artist, string album = null,
            bool checkAllLinks = false)
        {
            using (var client = new HttpClient())
            {
                var url = string.Format(Mp3SkullSearchUrl, CreateQuery(title, artist, album), Mp3SkullFckh);
                using (var resp = await client.GetAsync(url).ConfigureAwait(false))
                {
                    if (!resp.IsSuccessStatusCode)
                    {
                        return null;
                    }

                    var html = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);

                    var doc = new HtmlDocument();
                    doc.LoadHtml(html);

                    if (html.Contains("You have made too many request"))
                    {
                        return null;
                    }

                    if (html.Contains("Your search session has expired"))
                    {
                        var fckhNode =
                            doc.DocumentNode.Descendants("input")
                                .FirstOrDefault(
                                    p => p.Attributes.Contains("name") && p.Attributes["name"].Value == "fckh");
                        if (fckhNode == null)
                        {
                            return null;
                        }

                        Mp3SkullFckh = fckhNode.Attributes["value"].Value;

                        return await SearchMp3Skull(title, artist, album);
                    }

                    // Get the div node
                    var songNodes = doc.DocumentNode.Descendants("div").Where(p => p.Id == "song_html");

                    var songs = new List<WebSong>();

                    foreach (var songNode in songNodes)
                    {
                        var song = new WebSong {Provider = Mp3Provider.Mp3Skull};

                        var songUrlNode = songNode.Descendants("a").FirstOrDefault(p => p.InnerText == "Download");

                        if (songUrlNode == null)
                        {
                            continue;
                        }

                        song.AudioUrl = songUrlNode.Attributes["href"].Value;
                        var songInfo =
                            songNode.Descendants("div")
                                .FirstOrDefault(p => p.Attributes["class"].Value == "left")
                                .InnerText.Replace("<!-- info mp3 here -->", string.Empty)
                                .Trim();


                        var bitRateIndex = songInfo.IndexOf("kbps", StringComparison.Ordinal);
                        if (bitRateIndex > -1)
                        {
                            var bitrateTxt = songInfo.Substring(0, bitRateIndex);
                            int bitrate;
                            if (int.TryParse(bitrateTxt, out bitrate))
                            {
                                song.BitRate = bitrate;
                            }
                        }

                        #region Duration

                        if (bitRateIndex > -1)
                        {
                            songInfo = songInfo.Remove(0, bitRateIndex + 4);
                        }

                        var durationIndex = songInfo.IndexOf(":", StringComparison.Ordinal);
                        if (durationIndex > -1)
                        {
                            var durationText = songInfo.Substring(0, durationIndex + 3);
                            var seconds = int.Parse(durationText.Substring(durationText.Length - 2, 2));
                            var minutes = int.Parse(durationText.Remove(durationText.Length - 3));

                            song.Duration = new TimeSpan(0, 0, minutes, seconds);
                        }

                        #endregion

                        #region Size

                        if (durationIndex > -1)
                        {
                            songInfo = songInfo.Remove(0, durationIndex + 3);
                        }

                        var sizeIndex = songInfo.IndexOf("mb", StringComparison.Ordinal);
                        if (sizeIndex > -1)
                        {
                            var sizeText = songInfo.Substring(0, sizeIndex);
                            long size;
                            if (long.TryParse(sizeText, out size))
                            {
                                song.ByteSize = (long) (size*(1024*1024.0));
                            }
                        }

                        #endregion

                        var songTitle = songNode.Descendants("b").FirstOrDefault().InnerText;
                        songTitle = songTitle.Substring(0, songTitle.Length - 4).Trim();

                        song.Name = songTitle;
                        
                        songs.Add(song);
                    }

                    return songs.Any() ? await IdentifyMatches(songs, title, artist, checkAllLinks) : null;
                }
            }
        }
예제 #3
0
        public async Task<List<WebSong>> SearchMp3Truck(string title, string artist, string album = null,
            bool checkAllLinks = false)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Referrer = new Uri("https://mp3truck.net/");
                var data = new Dictionary<string, string>
                {
                    {"sort", "relevance"},
                    {"p", "1"},
                    {"q", CreateQuery(title, artist, album)}
                };

                using (var content = new FormUrlEncodedContent(data))
                {
                    using (var resp = await client.PostAsync(Mp3TruckSearchUrl, content).ConfigureAwait(false))
                    {
                        if (!resp.IsSuccessStatusCode)
                        {
                            return null;
                        }

                        var html = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);

                        var doc = new HtmlDocument();
                        doc.LoadHtml(html);

                        // Get the div node with the class='actl'
                        var songNodes =
                            doc.DocumentNode.Descendants("div")
                                .Where(
                                    p => p.Attributes.Contains("class") && p.Attributes["class"].Value.Contains("actl"));

                        var songs = new List<WebSong>();

                        foreach (var songNode in songNodes)
                        {
                            var song = new WebSong {Provider = Mp3Provider.Mp3Truck};

                            if (songNode.Attributes.Contains("data-id"))
                            {
                                song.Id = songNode.Attributes["data-id"].Value;
                            }

                            if (songNode.Attributes.Contains("data-bitrate"))
                            {
                                song.BitRate = int.Parse(songNode.Attributes["data-bitrate"].Value);
                            }

                            if (songNode.Attributes.Contains("data-filesize"))
                            {
                                song.ByteSize = (int) double.Parse(songNode.Attributes["data-filesize"].Value);
                            }

                            if (songNode.Attributes.Contains("data-duration"))
                            {
                                var duration = songNode.Attributes["data-duration"].Value;

                                if (duration.Contains(":"))
                                {
                                    var seconds = int.Parse(duration.Substring(duration.Length - 2, 2));
                                    var minutes = int.Parse(duration.Remove(duration.Length - 3));
                                    song.Duration = new TimeSpan(0, 0, minutes, seconds);
                                }
                                else
                                {
                                    song.Duration = new TimeSpan(0, 0, 0, int.Parse(duration));
                                }
                            }

                            var songTitle =
                                songNode.Descendants("div")
                                    .FirstOrDefault(
                                        p => p.Attributes.Contains("id") && p.Attributes["id"].Value == "title")
                                    .InnerText;
                            songTitle = WebUtility.HtmlDecode(songTitle.Substring(0, songTitle.Length - 4)).Trim();

                            // artist - title
                            var dashIndex = songTitle.IndexOf('-');
                            if (dashIndex != -1)
                            {
                                var titlePart = songTitle.Substring(dashIndex, songTitle.Length - dashIndex);
                                song.Artist = songTitle.Replace(titlePart, string.Empty).Trim();

                                songTitle = titlePart.Remove(0, 1).Trim();
                            }

                            song.Name = songTitle;

                            var linkNode =
                                songNode.Descendants("a")
                                    .FirstOrDefault(
                                        p =>
                                            p.Attributes.Contains("class")
                                            && p.Attributes["class"].Value.Contains("mp3download"));
                            if (linkNode == null)
                            {
                                continue;
                            }

                            song.AudioUrl = linkNode.Attributes["href"].Value.Replace("/idl.php?u=", string.Empty);

                            songs.Add(song);
                        }

                        return songs.Any() ? await IdentifyMatches(songs, title, artist, checkAllLinks) : null;
                    }
                }
            }
        }
예제 #4
0
        public async Task<List<WebSong>> SearchPleer(string title, string artist, string album = null,
            bool checkAllLinks = false)
        {
            var url = string.Format(
                PleerSearchUrl,
                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();

                        song.Id = songNode.Attributes["file_id"].Value;
                        song.Name = songNode.Attributes["song"].Value;
                        song.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);
                }
            }
        }
예제 #5
0
        public async Task<List<WebSong>> SearchSongily(string title, string artist, string album = null, int page = 1,
            bool checkAllLinks = false)
        {
            var url = string.Format(SongilySearchUrl, WebUtility.UrlEncode(CreateQuery(title, artist, album, false).ToCleanQuery()), page);
            using (var client = new HttpClient())
            {
                using (var resp = await client.GetAsync(url).ConfigureAwait(false))
                {
                    if (!resp.IsSuccessStatusCode)
                    {
                        return null;
                    }

                    var html = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);

                    var doc = new HtmlDocument();
                    doc.LoadHtml(html);

                    // Get the div node with the class='actl'
                    var songNodes =
                        doc.DocumentNode.Descendants("li")
                            .Where(
                                p => p.Attributes.Contains("class") && p.Attributes["class"].Value.Contains("list-group-item"));

                    var songs = new List<WebSong>();

                    foreach (var songNode in songNodes)
                    {
                        var song = new WebSong {Provider = Mp3Provider.Mp3Truck};

                        var detailNode = songNode.Descendants("small").FirstOrDefault();
                        if (detailNode != null)
                        {
                            var duration = detailNode.InnerText;

                            var durIndex = duration.IndexOf(":");
                            if (durIndex > 0)
                            {
                                var seconds = int.Parse(duration.Substring(durIndex + 1, 2));
                                var minutes = int.Parse(duration.Substring(0, durIndex));;
                                song.Duration = new TimeSpan(0, 0, minutes, seconds);
                            }
                        }

                        var songTitle =
                            songNode.Descendants("span")
                                .FirstOrDefault();

                        if (songTitle == null) continue;

                        song.Name = WebUtility.HtmlDecode(songTitle.InnerText).Trim();

                        var linkNode =
                            songNode.Descendants("a")
                                .FirstOrDefault(
                                    p =>
                                        p.Attributes.Contains("title")
                                        && p.Attributes["title"].Value.Contains("Download"));
                        if (linkNode == null)
                        {
                            continue;
                        }

                        song.AudioUrl = "http://songily.com/" + linkNode.Attributes["href"].Value;

                        songs.Add(song);
                    }

                    return songs.Any() ? await IdentifyMatches(songs, title, artist, checkAllLinks) : null;
                }
            }
        }