msjumbu / ilyrics

Automatically exported from code.google.com/p/ilyrics
0 stars 0 forks source link

Doesn't follow redirects #1

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Great App!

I'm using it atm and I've recognised some problems:

- It does not follow redirects in the wiki (Example:
http://lyricwiki.org/index.php?title=Kaiser_Chiefs:Everyday_I_Love_You_Less_And_
Less)
I think this shouldn't be very hard to fix.

If you could fix this little problem, the app would be perfect!

Original issue reported on code.google.com by simonhut...@gmail.com on 30 Jul 2007 at 2:58

GoogleCodeExporter commented 9 years ago
I've fixed as follow (LyricsUpdater.cs)

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using iTunesLib;
using System.Net;

namespace iTuneslyrics
{
    class LyricsUpdater
    {
        private IITTrackCollection m_selectedTracks;
        private org.lyricwiki.LyricWiki m_lyricsWiki;
        private frmResult m_form;
        private Boolean m_overwrite = false;

        public LyricsUpdater(IITTrackCollection selectedTracks, 
org.lyricwiki.LyricWiki lyricsWiki, Boolean overwrite, frmResult form)
        {
            this.m_selectedTracks = selectedTracks;
            this.m_lyricsWiki = lyricsWiki;
            this.m_overwrite = overwrite;
            this.m_form = form;
        }

        public void UpdateLyrics()
        {
            for (int i = 1; i <= m_selectedTracks.Count; i++)
            {
                IITFileOrCDTrack currentTrack = (IITFileOrCDTrack)m_selectedTracks
[i];

                String artist = currentTrack.Artist;
                String song = currentTrack.Name;

                if (!string.IsNullOrEmpty(currentTrack.Location) && 
                    !string.IsNullOrEmpty(artist) && !string.IsNullOrEmpty(song))
                {
                    String[] row = { song, artist, "Processing..." };
                    int index = (int)m_form.Invoke(m_form.m_DelegateAddRow, new 
Object[] { row });

                    if (currentTrack.Lyrics != null && !m_overwrite)
                    {
                        m_form.Invoke(m_form.m_DelegateUpdateRow, new Object[] { 
index, "skip" });
                        continue;
                    }

                    try
                    {
                        if (m_lyricsWiki.checkSongExists(artist, song) == true)
                        {
                            org.lyricwiki.LyricsResult result = m_lyricsWiki.getSong
(artist, song);
                            if (m_overwrite || currentTrack.Lyrics == null)
                            {
                                Regex regexUrl = new Regex(@"http://(?<Domain>[\w@]
[\w.:@]+)\/?[\w\.?=%:&=\-@/$,]*");
                                Match m = regexUrl.Match(result.lyrics);
                                if (m.Success)
                                {
                                    string url = m.Captures[0].Value;
                                    String htmlCode = new WebClient().DownloadString
(url);
                                    Regex regexLyrics = new Regex(@"\<(?<start>div)
\s*class='lyricbox'\s*>(.*?)</\k<start>>", RegexOptions.Singleline);
                                    Match l = regexLyrics.Match(htmlCode);
                                    if (l.Success)
                                    {
                                        Encoding iso8859 = Encoding.GetEncoding("ISO-
8859-1");
                                        string sResult = Regex.Replace(l.Groups
[1].Value, "<br\\s*/>", "\n");
                                        sResult = Regex.Replace(sResult, "<(.|\n)+?
>", string.Empty);
                                        currentTrack.Lyrics = Encoding.UTF8.GetString
(iso8859.GetBytes(sResult));
                                    }
                                }
                                else
                                {
                                    Encoding iso8859 = Encoding.GetEncoding("ISO-
8859-1");
                                    currentTrack.Lyrics = Encoding.UTF8.GetString
(iso8859.GetBytes(result.lyrics));
                                }
                            }
                            m_form.Invoke(m_form.m_DelegateUpdateRow, new Object[] { 
index, "true" });
                        }
                        else
                        {
                            m_form.Invoke(m_form.m_DelegateUpdateRow, new Object[] { 
index, "false" });
                        }
                    }
                    catch (Exception e)
                    {
                        //throw;
                        MessageBox.Show(e.Message);
                        m_form.Invoke(m_form.m_DelegateUpdateRow, new Object[] { 
index, "false" });
                    }
                }
            }
            MessageBox.Show("Completed");
        }

    }
}

Original comment by danilo.c...@gmail.com on 5 Aug 2009 at 2:42

Attachments: