예제 #1
0
        private void SearchLyricsInGoogle(string searchTerm)
        {
            string encoded = HttpUtility.UrlEncode(searchTerm);
            string url     = $"https://www.google.com/search?q={encoded}";

            Trace.WriteLine($"Searching lyrics by term: {encoded}");

            using var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36");
            httpClient.DefaultRequestHeaders.AcceptLanguage.ParseAdd("ru,en");
            var html = httpClient.GetStringAsync(url).Result;

            var doc = (IHTMLDocument2) new HTMLDocument();

            doc.write(html);
            var lyrics = ParseLyrics((HTMLDocument)doc);

            if (string.IsNullOrEmpty(lyrics))
            {
                Dispatcher.BeginInvoke(new Action(() => Lyrics.ToolTip = "Not Found"));
                Trace.WriteLine("Lyrics not found");
            }
            else
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    Lyrics.Text    = lyrics;
                    _source        = LyricsSource.Google;
                    Lyrics.ToolTip = _source.ToString();
                }));
                Trace.WriteLine("Lyrics received from Google");
            }
        }
예제 #2
0
        private bool GetLyricsFromTag()
        {
            if (string.IsNullOrEmpty(_lyrics))
            {
                return(false);
            }

            Lyrics.Text    = _lyrics;
            _source        = LyricsSource.Tag;
            Lyrics.ToolTip = _source.ToString();
            Trace.WriteLine("Lyrics received from lyrics tag");
            return(true);
        }