コード例 #1
0
ファイル: Program.cs プロジェクト: mattshall/lyricsfetcher
        static void TestSources()
        {
            ITunesLibrary lib = new ITunesLibrary();

            lib.MaxSongsToFetch = 150;
            lib.LoadSongs();
            lib.WaitLoad();

            List <Song> songs = lib.Songs.FindAll(
                delegate(Song s) {
                LyricsStatus status = s.LyricsStatus;
                return(status == LyricsStatus.Untried ||
                       status == LyricsStatus.Failed ||
                       status == LyricsStatus.Success);
            }
                );

            songs = songs.GetRange(0, 10);

            //TestOneSource(songs, new LyricsSourceLyricWiki());
            //TestOneSource(songs, new LyricsSourceLyricWikiHtml());
            TestOneSource(songs, new LyricsSourceLyricsPlugin());
            TestOneSource(songs, new LyricsSourceLyrdb());
            TestOneSource(songs, new LyricsSourceLyricsFly());
        }
コード例 #2
0
        private void VerifyGetLyricsResponse(LyricsResponse response, LyricsStatus expectedStatus, string expectedLyrics, Mock <ILyricsApiQuery> lyricsApiQueryMock)
        {
            response.Should().NotBeNull();
            response.Status.Should().Equals(expectedStatus);
            response.Lyrics.Should().Equals(expectedLyrics);

            lyricsApiQueryMock.Verify(x => x.FetchLyrics(It.IsAny <Artist>(), It.IsAny <Song>()), Times.Once);
        }
コード例 #3
0
 private LyricsResponse GetMockFetchLyricsResponse(LyricsStatus status, string lyrics)
 {
     return(new LyricsResponse
     {
         Lyrics = lyrics,
         Status = status
     });
 }
コード例 #4
0
ファイル: Program.cs プロジェクト: mattshall/lyricsfetcher
        static void TestSecondTry()
        {
            // Is it actually worthwhile doing the second or subsequent attempt?
            ITunesLibrary lib = new ITunesLibrary();

            //lib.MaxSongsToFetch = 1000;
            lib.LoadSongs();
            lib.WaitLoad();

            List <Song> songs = lib.Songs.FindAll(
                delegate(Song s) {
                LyricsStatus status = s.LyricsStatus;
                return(status == LyricsStatus.Untried ||
                       status == LyricsStatus.Failed ||
                       status == LyricsStatus.Success);
            }
                );

            ILyricsSource source2 = new LyricsSourceLyricsPlugin();
            ILyricsSource source1 = new LyricsSourceLyrdb();
            ILyricsSource source3 = new LyricsSourceLyricsFly();

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();
            Stopwatch sw3 = new Stopwatch();

            int failures = 0;
            int success1 = 0;
            int success2 = 0;
            int success3 = 0;

            foreach (Song song in songs)
            {
                sw1.Start();
                string lyrics = source1.GetLyrics(song);
                sw1.Stop();

                if (lyrics == string.Empty)
                {
                    sw2.Start();
                    lyrics = source2.GetLyrics(song);
                    sw2.Stop();

                    if (lyrics == string.Empty)
                    {
                        sw3.Start();
                        lyrics = source3.GetLyrics(song);
                        sw3.Stop();

                        if (lyrics == string.Empty)
                        {
                            failures++;
                        }
                        else
                        {
                            success3++;
                        }
                    }
                    else
                    {
                        success2++;
                    }
                }
                else
                {
                    success1++;
                }
            }

            Console.WriteLine("1st try: successes: {0} ({1}%), time: {2} ({3} each)",
                              success1, (success1 * 100 / songs.Count), sw1.Elapsed, sw1.ElapsedMilliseconds / songs.Count);
            Console.WriteLine("2st try: successes: {0} ({1}%), time: {2} ({3} each)",
                              success2, (success2 * 100 / songs.Count), sw2.Elapsed, sw2.ElapsedMilliseconds / (songs.Count - success1));
            Console.WriteLine("3st try: successes: {0} ({1}%), time: {2} ({3} each)",
                              success3, (success3 * 100 / songs.Count), sw3.Elapsed, sw3.ElapsedMilliseconds / (songs.Count - success1 - success2));
            Console.WriteLine("failures: {0} ({1}%)",
                              failures, (failures * 100 / songs.Count));
        }
コード例 #5
0
        private string DownloadLrc(string folderPath, string filenamePatern, Music music, int ModeIIndex, int DelayMsc, out LyricsStatus status, out string filePath, string fileEncoding = "UTF-8", string revisedsContentOriLyricsForUserReviseFunc = null, string revisedsContentTransLyricsForUserReviseFunc = null)
        {
            ExtendedLyrics l = new ExtendedLyrics(music.ID);

            l.FetchOnlineLyrics(revisedsContentOriLyricsForUserReviseFunc, revisedsContentTransLyricsForUserReviseFunc);
            string lyricText = l.GetCustomLyric(ModeIIndex, DelayMsc);

            filePath = "";
            if (lyricText != "")
            {
                LyricsFileWriter writer = new LyricsFileWriter(folderPath, filenamePatern, music, fileEncoding);
                writer.WriteFile(lyricText);
                filePath = writer.GetFilePath();
                status   = l.Status;
            }
            else
            {
                status = l.Status;
            }
            return(l.ErrorLog);
        }
コード例 #6
0
        private void GETbutton_Click(object sender, EventArgs e)
        {
            string filenamePattern = FilenamePatterncomboBox.Text;

            GETbutton.Enabled               = false;
            StatusInfolabel.Text            = "StatusInfo";
            StatusPDFinishedCountlabel.Text = "0";
            StatusPDTotalCountlabel.Text    = "0";
            Stopwatch sw = new Stopwatch();

            sw.Start();
            int id         = Convert.ToInt32(IDtextBox.Text);
            int modelIndex = Convert.ToInt32(LyricsStylenumericUpDown.Value);
            int delayMsec  = Convert.ToInt32(DelayMsecnumericUpDown.Value);

            if (MusicradioButton.Checked)
            {
                Music        m      = new Music(id);
                string       Log    = "";
                LyricsStatus status = LyricsStatus.UNSURED;
                if (ReviseRawcheckBox.Checked == false)
                {
                    Log = DownloadLrc(".\\", filenamePattern, m, modelIndex, delayMsec, out status, out string filePath);//正常的自动化操作
                }
                else
                {
                    HttpRequest hr = new HttpRequest();
                    string      sContentOriLyrics = hr.GetContent("https://music.163.com/api/song/media?id=" + id);
                    Clipboard.SetText(sContentOriLyrics, TextDataFormat.UnicodeText);
                    if (MessageBox.Show("OriLyrics raw text has been set is in the clipboard. Please follow these guide below." + Environment.NewLine + Environment.NewLine + "==========" + Environment.NewLine + @"1. Use Notepad++ to read(such as ""paste"") text from the clipboard." + Environment.NewLine + @"2. Try to revise the text. The most mistake we face is wrong typo. (You can search "".lrc guidance"" to learn more.)" + Environment.NewLine + @"3. After revising, please set revised text to the clipboard(such as ""copy"")" + Environment.NewLine + @"4. Click ""Yes"" button. Software will use the revised text instead of the raw text." + Environment.NewLine + "==========" + Environment.NewLine + Environment.NewLine + @"**If you don't know why you're seeing this messagebox now or you don't know what to do, just click ""No"" button. Don't worry, nothing wrong will happen.**" + Environment.NewLine + @"You can click the blue label ""Need Help?"" in the LrcDownloader Form to learn more about this function.", "OriLyrics raw text - ReviseRaw(Func)_Step 1", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    {
                        sContentOriLyrics = Clipboard.GetText(TextDataFormat.UnicodeText);
                    }

                    string sContentTransLyrics = hr.GetContent("https://music.163.com/api/song/lyric?os=pc&id=" + id + "&tv=-1");
                    Clipboard.SetText(sContentTransLyrics, TextDataFormat.UnicodeText);
                    if (MessageBox.Show("TransLyrics raw text has been set is in the clipboard. Please follow these guide below." + Environment.NewLine + Environment.NewLine + "==========" + Environment.NewLine + @"1. Use Notepad++ to read(such as ""paste"") text from the clipboard." + Environment.NewLine + @"2. Try to revise the text. The most mistake we face is wrong typo. (You can search "".lrc guidance"" to learn more.)" + Environment.NewLine + @"3. After revising, please set revised text to the clipboard(such as ""copy"")" + Environment.NewLine + @"4. Click ""Yes"" button. Software will use the revised text instead of the raw text." + Environment.NewLine + "==========" + Environment.NewLine + Environment.NewLine + @"**If you don't know why you're seeing this messagebox now or you don't know what to do, just click ""No"" button. Don't worry, nothing wrong will happen.**" + Environment.NewLine + @"You can click the blue label ""Need Help?"" in the LrcDownloader Form to learn more about this function.", "TransLyrics raw text - ReviseRaw(Func)_Step 2", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    {
                        sContentTransLyrics = Clipboard.GetText(TextDataFormat.UnicodeText);
                    }

                    Log = DownloadLrc(".\\", filenamePattern, m, modelIndex, delayMsec, out status, out string filePath, revisedsContentOriLyricsForUserReviseFunc: sContentOriLyrics, revisedsContentTransLyricsForUserReviseFunc: sContentTransLyrics);
                }
                sw.Stop();
                if (Log == "")
                {
                    StatusInfolabel.Text = "Done Status:" + status + "\r\nUsed Time:" + Math.Round(sw.Elapsed.TotalSeconds, 3) + "sec";
                }
                else
                {
                    StatusInfolabel.Text = Log + " Status:" + status + "\r\nUsed Time:" + Math.Round(sw.Elapsed.TotalSeconds, 3) + "sec";
                }
                GETbutton.Enabled = true;
                IDtextBox.Clear();
            }
            else if (PlaylistradioButton.Checked)
            {
                cancelToken = new CancellationTokenSource();
                ParallelOptions parOpts = new ParallelOptions()
                {
                    CancellationToken      = cancelToken.Token,
                    MaxDegreeOfParallelism = System.Environment.ProcessorCount//上面三行针对TPL并行类库
                };
                Task.Factory.StartNew(() =>
                {
                    Playlist pl             = new Playlist(id);
                    LogFileWriter logWriter = new LogFileWriter(@".\\" + FormatFileName.CleanInvalidFileName(pl.Name) + @"\");
                    List <long> idList      = pl.SongidInPlaylist;
                    logWriter.AppendLyricsDownloadTaskDetail();
                    logWriter.AppendLyricsDownloadTaskDetail(pl.SongidInPlaylist.Count);
                    this.Invoke((Action) delegate
                    {
                        StatusPDTotalCountlabel.Text = idList.Count.ToString(); //写Status
                        Cancelbutton.Enabled         = true;                    //下面使用TPL类库,可以取消
                    });
                    try
                    {
                        Parallel.For(0, idList.Count, parOpts, i =>

                        {
                            parOpts.CancellationToken.ThrowIfCancellationRequested();
                            string ErrorLog     = "";
                            LyricsStatus status = LyricsStatus.UNSURED;
                            string filePath     = "";
                            Music m             = new Music(idList[i], i + 1);
                            try
                            {
                                ErrorLog = DownloadLrc(@".\\" + FormatFileName.CleanInvalidFileName(pl.Name) + @"\", filenamePattern, m, modelIndex, delayMsec, out status, out filePath);
                            }
                            catch (Exception ex)
                            {
                                ErrorLog += "<" + ex.Message + ">";
                            }
                            finally
                            {
                                if (System.IO.File.Exists(filePath))
                                {
                                    logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "√" + ErrorLog);
                                }
                                else
                                {
                                    logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "×" + ErrorLog);
                                }
                                this.Invoke((Action) delegate
                                {
                                    StatusPDFinishedCountlabel.Text = (Convert.ToInt32(StatusPDFinishedCountlabel.Text) + 1).ToString();
                                });
                                m = null;
                            }
                        });
                    }
                    catch (OperationCanceledException)
                    {
                        //
                    }
                    finally
                    {
                        this.Invoke((Action) delegate
                        {
                            Cancelbutton.Enabled = false;
                            GETbutton.Enabled    = true;
                            IDtextBox.Clear();
                        });
                    }

                    sw.Stop();
                    logWriter.AppendHeadInformation("Playlist", id, pl.Name, idList.Count, cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished");
                    logWriter.AppendBottomInformation(Math.Round(sw.Elapsed.TotalSeconds, 3));
                    logWriter.WriteFIle();
                    this.Invoke((Action) delegate
                    {
                        StatusInfolabel.Text = (cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished") + "\r\nRead Log.txt to learn more.";
                    });
                    cancelToken.Dispose();
                    pl = null;
                });
            }
            else if (AlbumradioButton.Checked)
            {
                cancelToken = new CancellationTokenSource();
                ParallelOptions parOpts = new ParallelOptions()
                {
                    CancellationToken      = cancelToken.Token,
                    MaxDegreeOfParallelism = System.Environment.ProcessorCount//上面三行针对TPL并行类库
                };
                Task.Factory.StartNew(() =>
                {
                    Album a = new Album(id);
                    LogFileWriter logWriter = new LogFileWriter(@".\\" + FormatFileName.CleanInvalidFileName(a.Name) + @"\");
                    List <long> idList      = a.SongidInAlbum;
                    logWriter.AppendLyricsDownloadTaskDetail();
                    logWriter.AppendLyricsDownloadTaskDetail(a.SongidInAlbum.Count);
                    this.Invoke((Action) delegate
                    {
                        StatusPDTotalCountlabel.Text = idList.Count.ToString(); //写Status
                        Cancelbutton.Enabled         = true;                    //下面使用TPL类库,可以取消
                    });
                    try
                    {
                        Parallel.For(0, idList.Count, parOpts, i =>
                        {
                            parOpts.CancellationToken.ThrowIfCancellationRequested();
                            Music m             = new Music(idList[i], i + 1);
                            string ErrorLog     = "";
                            LyricsStatus status = LyricsStatus.UNSURED;
                            string filePath     = "";
                            try
                            {
                                ErrorLog = DownloadLrc(@".\\" + FormatFileName.CleanInvalidFileName(a.Name) + @"\", filenamePattern, m, modelIndex, delayMsec, out status, out filePath);
                            }
                            catch (Exception ex)
                            {
                                ErrorLog += "<" + ex.Message + ">";
                            }
                            finally
                            {
                                if (System.IO.File.Exists(filePath))
                                {
                                    logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "√" + ErrorLog);
                                }
                                else
                                {
                                    logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "×" + ErrorLog);
                                }
                                this.Invoke((Action) delegate
                                {
                                    StatusPDFinishedCountlabel.Text = (Convert.ToInt32(StatusPDFinishedCountlabel.Text) + 1).ToString();
                                });
                                m = null;
                            }
                        });
                    }
                    catch (OperationCanceledException)
                    {
                        //
                    }
                    finally
                    {
                        this.Invoke((Action) delegate
                        {
                            Cancelbutton.Enabled = false;
                            GETbutton.Enabled    = true;
                            IDtextBox.Clear();
                        });
                    }
                    sw.Stop();
                    logWriter.AppendHeadInformation("Album", id, a.Name, idList.Count, cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished");
                    logWriter.AppendBottomInformation(Math.Round(sw.Elapsed.TotalSeconds, 3));
                    logWriter.WriteFIle();
                    this.Invoke((Action) delegate
                    {
                        StatusInfolabel.Text = (cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished") + "\r\nRead Log.txt to learn more.";
                    });
                    cancelToken.Dispose();
                    a = null;
                });
            }
        }
コード例 #7
0
        private void GETbutton_Click(object sender, EventArgs e)
        {
            string filenamePattern = FilenamePatterncomboBox.Text;

            GETbutton.Enabled               = false;
            StatusInfolabel.Text            = "StatusInfo";
            StatusPDFinishedCountlabel.Text = "0";
            StatusPDTotalCountlabel.Text    = "0";
            Stopwatch sw = new Stopwatch();

            sw.Start();
            int id         = Convert.ToInt32(IDtextBox.Text);
            int modelIndex = Convert.ToInt32(LyricsStylenumericUpDown.Value);
            int delayMsec  = Convert.ToInt32(DelayMsecnumericUpDown.Value);

            if (MusicradioButton.Checked)
            {
                Music  m   = new Music(id);
                string Log = DownloadLrc(".\\", filenamePattern, m, modelIndex, delayMsec, out LyricsStatus status, out string filePath);
                sw.Stop();
                if (Log == "")
                {
                    StatusInfolabel.Text = "Done Status:" + status + "\r\nUsed Time:" + Math.Round(sw.Elapsed.TotalSeconds, 3) + "sec";
                }
                else
                {
                    StatusInfolabel.Text = Log + " Status:" + status + "\r\nUsed Time:" + Math.Round(sw.Elapsed.TotalSeconds, 3) + "sec";
                }
                GETbutton.Enabled = true;
                IDtextBox.Clear();
            }
            else if (PlaylistradioButton.Checked)
            {
                cancelToken = new CancellationTokenSource();
                ParallelOptions parOpts = new ParallelOptions()
                {
                    CancellationToken      = cancelToken.Token,
                    MaxDegreeOfParallelism = System.Environment.ProcessorCount//上面三行针对TPL并行类库
                };
                Task.Factory.StartNew(() =>
                {
                    Playlist pl             = new Playlist(id);
                    LogFileWriter logWriter = new LogFileWriter(@".\\" + pl.Name + @"\");
                    List <long> idList      = pl.SongidInPlaylist;
                    logWriter.AppendLyricsDownloadTaskDetail();
                    logWriter.AppendLyricsDownloadTaskDetail(pl.SongidInPlaylist.Count);
                    this.Invoke((Action) delegate
                    {
                        StatusPDTotalCountlabel.Text = idList.Count.ToString(); //写Status
                        Cancelbutton.Enabled         = true;                    //下面使用TPL类库,可以取消
                    });
                    try
                    {
                        Parallel.For(0, idList.Count, parOpts, i =>

                        {
                            parOpts.CancellationToken.ThrowIfCancellationRequested();
                            string ErrorLog     = "";
                            LyricsStatus status = LyricsStatus.UNSURED;
                            string filePath     = "";
                            Music m             = new Music(idList[i], i + 1);
                            try
                            {
                                ErrorLog = DownloadLrc(".\\" + pl.Name + @"\", filenamePattern, m, modelIndex, delayMsec, out status, out filePath);
                            }
                            catch (Exception ex)
                            {
                                ErrorLog += "<" + ex.Message + ">";
                            }
                            finally
                            {
                                if (System.IO.File.Exists(filePath))
                                {
                                    logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "√" + ErrorLog);
                                }
                                else
                                {
                                    logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "×" + ErrorLog);
                                }
                                this.Invoke((Action) delegate
                                {
                                    StatusPDFinishedCountlabel.Text = (Convert.ToInt32(StatusPDFinishedCountlabel.Text) + 1).ToString();
                                });
                                m = null;
                            }
                        });
                    }
                    catch (OperationCanceledException)
                    {
                        //
                    }
                    finally
                    {
                        this.Invoke((Action) delegate
                        {
                            Cancelbutton.Enabled = false;
                            GETbutton.Enabled    = true;
                            IDtextBox.Clear();
                        });
                    }

                    sw.Stop();
                    logWriter.AppendHeadInformation("Playlist", id, pl.Name, idList.Count, cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished");
                    logWriter.AppendBottomInformation(Math.Round(sw.Elapsed.TotalSeconds, 3));
                    logWriter.WriteFIle();
                    this.Invoke((Action) delegate
                    {
                        StatusInfolabel.Text = (cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished") + "\r\nRead Log.txt to learn more.";
                    });
                    cancelToken.Dispose();
                    pl = null;
                });
            }
            else if (AlbumradioButton.Checked)
            {
                cancelToken = new CancellationTokenSource();
                ParallelOptions parOpts = new ParallelOptions()
                {
                    CancellationToken      = cancelToken.Token,
                    MaxDegreeOfParallelism = System.Environment.ProcessorCount//上面三行针对TPL并行类库
                };
                Task.Factory.StartNew(() =>
                {
                    Album a = new Album(id);
                    LogFileWriter logWriter = new LogFileWriter(@".\\" + a.Name + @"\");
                    List <long> idList      = a.SongidInAlbum;
                    logWriter.AppendLyricsDownloadTaskDetail();
                    logWriter.AppendLyricsDownloadTaskDetail(a.SongidInAlbum.Count);
                    this.Invoke((Action) delegate
                    {
                        StatusPDTotalCountlabel.Text = idList.Count.ToString(); //写Status
                        Cancelbutton.Enabled         = true;                    //下面使用TPL类库,可以取消
                    });
                    try
                    {
                        Parallel.For(0, idList.Count, parOpts, i =>
                        {
                            parOpts.CancellationToken.ThrowIfCancellationRequested();
                            Music m             = new Music(idList[i], i + 1);
                            string ErrorLog     = "";
                            LyricsStatus status = LyricsStatus.UNSURED;
                            string filePath     = "";
                            try
                            {
                                ErrorLog = DownloadLrc(".\\" + a.Name + @"\", filenamePattern, m, modelIndex, delayMsec, out status, out filePath);
                            }
                            catch (Exception ex)
                            {
                                ErrorLog += "<" + ex.Message + ">";
                            }
                            finally
                            {
                                if (System.IO.File.Exists(filePath))
                                {
                                    logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "√" + ErrorLog);
                                }
                                else
                                {
                                    logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "×" + ErrorLog);
                                }
                                this.Invoke((Action) delegate
                                {
                                    StatusPDFinishedCountlabel.Text = (Convert.ToInt32(StatusPDFinishedCountlabel.Text) + 1).ToString();
                                });
                                m = null;
                            }
                        });
                    }
                    catch (OperationCanceledException)
                    {
                        //
                    }
                    finally
                    {
                        this.Invoke((Action) delegate
                        {
                            Cancelbutton.Enabled = false;
                            GETbutton.Enabled    = true;
                            IDtextBox.Clear();
                        });
                    }
                    sw.Stop();
                    logWriter.AppendHeadInformation("Album", id, a.Name, idList.Count, cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished");
                    logWriter.AppendBottomInformation(Math.Round(sw.Elapsed.TotalSeconds, 3));
                    logWriter.WriteFIle();
                    this.Invoke((Action) delegate
                    {
                        StatusInfolabel.Text = (cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished") + "\r\nRead Log.txt to learn more.";
                    });
                    cancelToken.Dispose();
                    a = null;
                });
            }
        }