Beispiel #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");
            }
        }
Beispiel #2
0
        private void SaveLyricsToFile()
        {
            var dialog = new SaveFileDialog
            {
                Filter   = "Text file (*.txt)|*.txt|Lyrics file (*.lrc)|*.lrc|Subtitles file (*.srt)|*.srt",
                FileName = $"{ArtistTitle}.txt"
            };

            if (_filePath != null)
            {
                string directory = Path.GetDirectoryName(_filePath);
                if (Directory.Exists(directory))
                {
                    dialog.InitialDirectory = directory;
                    dialog.FileName         = Path.GetFileNameWithoutExtension(_filePath) + ".txt";
                }
            }

            if (dialog.ShowDialog() != true)
            {
                return;
            }

            _lyricsFilePath = dialog.FileName;
            File.WriteAllText(_lyricsFilePath, Lyrics.Text);
            Lyrics.ToolTip = Path.GetFileName(_lyricsFilePath);
            _source        = LyricsSource.File;
            Trace.WriteLine($"Lyrics have been saved to {_lyricsFilePath}");
        }
Beispiel #3
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);
        }
Beispiel #4
0
        private bool GetLyricsFromFile()
        {
            if (_filePath is null || !new Uri(_filePath).IsFile)
            {
                return(false);
            }

            string directory   = Path.GetDirectoryName(_filePath);
            string filePattern = Path.GetFileNameWithoutExtension(_filePath) + ".*";

            _lyricsFilePath = Directory.EnumerateFiles(directory, filePattern)
                              .FirstOrDefault(x => x.ToLower().EndsWith(".txt") || x.ToLower().EndsWith(".lrc") || x.ToLower().EndsWith(".srt"));

            if (_lyricsFilePath == null)
            {
                return(false);
            }

            Lyrics.Text    = File.ReadAllText(_lyricsFilePath);
            _source        = LyricsSource.File;
            Lyrics.ToolTip = Path.GetFileName(_lyricsFilePath);
            Trace.WriteLine($"Lyrics received from {_lyricsFilePath}");
            return(true);
        }
Beispiel #5
0
 private void ClearLyrics()
 {
     Lyrics.Text    = "";
     Lyrics.ToolTip = "";
     _source        = LyricsSource.None;
 }