public LyricEventArgs(ILyricsSource s, string lyrics, bool found, iTunesLib.IITFileOrCDTrack t) { this.Source = s; this.Lyrics = lyrics; this.Track = t; this.LyricsFound = found; }
public string GetLyrics(iTunesLib.IITFileOrCDTrack s) { if (string.IsNullOrEmpty(s.Artist) || string.IsNullOrEmpty(s.Name)) { return(""); } string ret = String.Empty; string url = string.Format("http://www.absolutelyrics.com/lyrics/view/{0}/{1}", s.Artist.Replace(" ", "_"), s.Name.Replace(" ", "_")); try { WebClient wc = new WebClient(); string html = wc.DownloadString(url); Match match = Regex.Match(html, pattern); if (match.Success) { ret = Flatten(match.Value); ret = Unbreak(ret) .Replace("<i>", "") .Replace("</i>", ""); } } catch (Exception ex) { ret = ""; } return(ret); }
/// <summary> /// Retrieve the lyrics for the given song /// </summary> /// <param name="song">The song whose lyrics are to be fetched</param> /// <returns>The lyrics or an empty string if the lyrics could not be found</returns> public string GetLyrics(iTunesLib.IITFileOrCDTrack song) { if (string.IsNullOrEmpty(song.Artist) || string.IsNullOrEmpty(song.Name)) { return(""); } // clean the title; we don't need quotes string title = Regex.Replace(song.Name, "['\"]", ""); string lyrics = String.Empty; using (WebClient client = new WebClient()) { try { string uri = Uri.EscapeUriString(String.Format( QueryFormat, song.Artist.Replace(" ", String.Empty).ToLower(), song.Name.Replace(" ", String.Empty).ToLower())); string result = client.DownloadString(uri); if (!String.IsNullOrEmpty(result)) { Match match = Regex.Match(result, pattern); if (match.Success) { lyrics = Flatten(match.Value); lyrics = Unbreak(lyrics) .Replace("<i>", "") .Replace("</i>", ""); lyrics = Encode(lyrics); } } } catch (Exception ex) { lyrics = String.Empty; } } return(lyrics); }
public void Start() { foreach (iTunesLib.IITFileOrCDTrack t2 in this.t.Tracks) { iTunesLib.IITFileOrCDTrack t = t2; if (t == null) { onSkip(); continue; } if (t.Location == null) { onSkip(); continue; } if (t.Podcast) { onSkip(); continue; } if (t.Location == "") { onSkip(); continue; } if (t.Location.ToLower().EndsWith(".m4") || t.Location.ToLower().EndsWith(".m4v")) { onSkip(); continue; } // skip ones that already have lyrics if (!string.IsNullOrEmpty(t.Lyrics)) { onSkip(); continue; } string result = string.Empty; ILyricsSource rSource = null; Thread thrd = new Thread(new ThreadStart(delegate { threads++; onNewThread(); foreach (ILyricsSource source in this.Sources) { if (source == null) { continue; } string s = source.GetLyrics(t); //System.Windows.Forms.MessageBox.Show(source.Name + " :: " + s); if (OnStatusChanged != null) { OnStatusChanged(null, new LyricEventArgs(source, s, !string.IsNullOrEmpty(s), t)); } if (string.IsNullOrEmpty(s) == false) { result = s; rSource = source; break; } } if (string.IsNullOrEmpty(result) == false) { t.Lyrics = result; try { //t.Info.Save(); //MainWindow.Instance.StatusMessage = String.Format("Saved lyrics from {0}!", rSource.Name); } catch (Exception ex) { //MainWindow.Instance.StatusMessage = String.Format("Error saving lyrics: {0}", ex.Message); } } if (OnTrackFinished != null) { OnTrackFinished(null, null); } threads--; onRemoveThread(); })); while (threads >= 500) { ; } thrd.Start(); } }
public iTunesClass(iTunesLib.IITTrack track) { try { //型を変換 iTunesLib.IITFileOrCDTrack track2 = (iTunesLib.IITFileOrCDTrack)track; SongAlbum = track2.Album; SongAlbumArtist = track2.AlbumArtist; SongArtist = track2.Artist; SongComment = track2.Comment; SongComposer = track2.Composer; SongDiscCount = track2.DiscCount; SongDiscNumber = track2.DiscNumber; SongGenre = track2.Genre; SongLastPlayed = track2.PlayedDate; SongPlayedTimes = track2.PlayedCount; SongRating = track2.Rating; SongTitle = track2.Name; SongTrackNumber = track2.TrackNumber; SongYear = track2.Year; TrackDatabaseID = track2.TrackDatabaseID; Group = track2.Grouping; if (SongAlbumArtist == "" | SongAlbumArtist == null) { SongAlbumArtist = SongArtist; } try { track2.Artwork[1].SaveArtworkToFile(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\tmp\" + TrackDatabaseID.ToString() + ".png"); } catch (Exception ex) { } Marshal.FinalReleaseComObject(track2); } catch (InvalidCastException castex) { //キャスト出来ない曲(ホームシェアリング等) SongAlbum = track.Album; SongAlbumArtist = ""; SongArtist = track.Artist; SongComment = track.Comment; SongComposer = track.Composer; SongDiscCount = track.DiscCount; SongDiscNumber = track.DiscNumber; SongGenre = track.Genre; SongLastPlayed = track.PlayedDate; SongPlayedTimes = track.PlayedCount; SongRating = track.Rating; SongTitle = track.Name; SongTrackNumber = track.TrackNumber; SongYear = track.Year; TrackDatabaseID = track.TrackDatabaseID; Group = track.Grouping; if (SongAlbumArtist == "" || SongAlbumArtist == null) { SongAlbumArtist = SongArtist; } try { track.Artwork[1].SaveArtworkToFile(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\tmp\" + TrackDatabaseID.ToString() + ".png"); } catch (Exception ex) { } } }
/// <summary> /// Retrieve the lyrics for the given song /// </summary> /// <param name="song">The song whose lyrics are to be fetched</param> /// <returns>The lyrics or an empty string if the lyrics could not be found</returns> public string GetLyrics(iTunesLib.IITFileOrCDTrack song) { if (string.IsNullOrEmpty(song.Artist) || string.IsNullOrEmpty(song.Name)) { return(""); } // clean the title; we don't need quotes string title = song.Name.Replace("\"", ""); string lyrics = String.Empty; using (WebClient client = new WebClient()) { try { string category = song.Artist.Substring(0, 1); if (char.IsNumber(category, 0)) { category = "0-9"; } string artist = song.Artist.ToLower().Replace(" ", "-"); title = title.ToLower().Replace(" ", "-").Replace("'", "-"); string uri = Uri.EscapeUriString( String.Format(QueryFormat, category, song.Artist, title)); string result = client.DownloadString(uri); if (!String.IsNullOrEmpty(result)) { Match match = Regex.Match(result, pattern); if (match.Success) { lyrics = match.Value; match = Regex.Match(lyrics, advertisingPattern); if (match.Success) { var builder = new StringBuilder(lyrics); lyrics = builder.Remove(match.Index, match.Length).ToString(); } lyrics = Unbreak(lyrics) .Replace("<i/>", String.Empty) .Replace("<i>", String.Empty) .Replace("<u/>", String.Empty) .Replace("<u>", String.Empty); lyrics = Encode(lyrics); } } } catch (Exception ex) { lyrics = String.Empty; } } return(lyrics); }
/// <summary> /// Fetch the lyrics for the given song /// </summary> /// <param name="s">The song whose lyrics are to be fetched</param> /// <returns>The lyrics or an empty string if the lyrics could not be found</returns> public string GetLyrics(iTunesLib.IITFileOrCDTrack s) { if (string.IsNullOrEmpty(s.Artist) || string.IsNullOrEmpty(s.Name)) { return(""); } // Lyrdb can't handle single or double quotes in the title (as of 12/1/2008) // So we just remove them string title = s.Name.Replace("'", ""); title = title.Replace("\"", ""); WebClient client = new WebClient(); string result = String.Empty; try { // If we have both the title and the artist, we can look for a perfect match if (!String.IsNullOrEmpty(s.Artist)) { string queryUrl = String.Format("http://webservices.lyrdb.com/lookup.php?q={0}|{1}&for=match&agent={2}/{3}", s.Artist, title, PROGRAM_NAME, PROGRAM_VERSION); result = client.DownloadString(queryUrl); } // If we only have the title or the perfect match failed, do a full text search using // whatever information we have //if (result == String.Empty) //{ // string queryUrl = String.Format("http://webservices.lyrdb.com/lookup.php?q={0}&for=trackname&agent={2}/{3}", // title, s.Artist, PROGRAM_NAME, PROGRAM_VERSION); // result = client.DownloadString(queryUrl); //} } catch (Exception ex) { // Many things can do wrong here, but there's nothing we can do it fix them System.Diagnostics.Debug.WriteLine("Lyrdb GetLyrics failed:"); System.Diagnostics.Debug.WriteLine(ex); return(String.Empty); } // Still didn't work? Give up. if (result == String.Empty || result.StartsWith("error:")) { return(String.Empty); } foreach (string x in result.Split('\n')) { string id = x.Split('\\')[0]; try { string lyrics = client.DownloadString("http://webservices.lyrdb.com/getlyr.php?q=" + id); if (lyrics != String.Empty && !lyrics.StartsWith("error:")) { Encoding latin1 = Encoding.GetEncoding("ISO-8859-1"); string str = Encoding.UTF8.GetString(latin1.GetBytes(lyrics)); str = str.Replace("\r", String.Empty); return(str.Replace("\n", System.Environment.NewLine)); } } catch (Exception) { } } return(String.Empty); }