none
这段代码应该怎么改,我下载不到歌词啊 RRS feed

  • 问题

  • 歌词搜索

    默认接口

    http://api.ttlyrics.com/v6/service?sh?Artist={singerName}&Title={songName}&external=true
    
    • {songName}:歌曲名称
    • {singerName}:歌手名称

    歌词下载

    请注意:在搜索时,服务器会返回用于保存会话的 connect.sid Cookie,在下载歌词时,需附加此 Cookie,以便服务器识别保存的搜索数据。因而每次搜索结果只在会话有效期内可以使用,过期后需重新搜索。

    默认接口

    http://api.ttlyrics.com/v6/service?dl?Id={id}
    
    • {id}:在搜索中获取的歌词 ID(请与 sid 加以区分,后者为搜索源上的 ID)

    定义两个类:LyricsHelper(辅助获取歌词Id)LrcInfo(歌词下载相应信息)

    LyricsHelper

    复制代码
    
            public class LyricsHelper
            {
                //歌词Id获取地址
                private static readonly string SearchPath = "http://ttlrcct2.qianqian.com/dll/lyricsvr.dll?sh?Artist={0}&Title={1}&Flags=0";
    
                //根据artist和title获取歌词信息
                public static LrcInfo[] GetLrcList(string artist, string title, string filepath)
                {
                    string artistHex = GetHexString(artist, Encoding.Unicode);
                    string titleHex = GetHexString(title, Encoding.Unicode);
    
                    string resultUrl = string.Format(SearchPath, artistHex, titleHex);
                    
                    XmlDocument doc = new XmlDocument();
                    try
                    {
                        doc.Load(resultUrl);
    
                        XmlNodeList nodelist = doc.SelectNodes("/result/lrc");
                        List<LrcInfo> lrclist = new List<LrcInfo>();
                        foreach (XmlNode node in nodelist)
                        {
                            XmlElement element = (XmlElement)node;
                            string artistItem = element.GetAttribute("artist");
                            string titleItem = element.GetAttribute("title");
                            string idItem = element.GetAttribute("id");
                            lrclist.Add(new LrcInfo(idItem, titleItem, artistItem, filepath));
                        }
                        return lrclist.ToArray();
                    }
                    catch (XmlException)
                    {
                        return null;
                    }
                }
                
                //把字符串转换为十六进制
                public static string GetHexString(string str, Encoding encoding)
                {
                    StringBuilder sb = new StringBuilder();
                    byte[] bytes = encoding.GetBytes(str);
                    foreach (byte b in bytes)
                    {
                        sb.Append(b.ToString("X").PadLeft(2, '0'));
                    }
                    return sb.ToString();
                }
            }
            
            public class LrcInfo
            {
                //歌词下载地址
                private static readonly string DownloadPath = "http://ttlrcct2.qianqian.com/dll/lyricsvr.dll?dl?Id={0}&Code={1}";
    
                public string FilePath { get; set; }
                public string Id { get; set; }
                public string Artist { get; set; }
                public string Title { get; set; }
                public string LrcUri { get; set; }
    
                public LrcInfo(string id, string title, string artist, string filepath)
                {
                    this.FilePath = filepath;
                    this.Id = id.Trim();
                    this.Title = title;
                    this.Artist = artist;
                    //算出歌词的下载地址
                    this.LrcUri = string.Format(DownloadPath, Id, CreateQianQianCode());
                }
                public bool DownloadLrc()
                {
                    string file = FilePath;
                    string directory = Path.GetDirectoryName(file) + "\\Lrc\\";
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }
                    string filepath = directory + Path.GetFileNameWithoutExtension(file) + ".lrc";
                    WebRequest request = WebRequest.Create(LrcUri);
    
                    StringBuilder sb = new StringBuilder();
                    try
                    {
                        using (StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8))
                        {
                            using (StreamWriter sw = new StreamWriter(filepath, false, Encoding.UTF8))
                            {
                                sw.Write(sr.ReadToEnd());
                            }
                        }
                        return true;
                    }
                    catch (WebException)
                    {
    
                    }
                    return false;
                }
    
    
                private string CreateQianQianCode()
                {
                    int lrcId = Convert.ToInt32(Id);
                    string qqHexStr = LyricsHelper.GetHexString(Artist + Title, Encoding.UTF8);
                    int length = qqHexStr.Length / 2;
                    int[] song = new int[length];
                    for (int i = 0; i < length; i++)
                    {
                        song[i] = int.Parse(qqHexStr.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
                    }
                    int t1 = 0, t2 = 0, t3 = 0;
                    t1 = (lrcId & 0x0000FF00) >> 8;
                    if ((lrcId & 0x00FF0000) == 0)
                    {
                        t3 = 0x000000FF & ~t1;
                    }
                    else
                    {
                        t3 = 0x000000FF & ((lrcId & 0x00FF0000) >> 16);
                    }
    
                    t3 = t3 | ((0x000000FF & lrcId) << 8);
                    t3 = t3 << 8;
                    t3 = t3 | (0x000000FF & t1);
                    t3 = t3 << 8;
                    if ((lrcId & 0xFF000000) == 0)
                    {
                        t3 = t3 | (0x000000FF & (~lrcId));
                    }
                    else
                    {
                        t3 = t3 | (0x000000FF & (lrcId >> 24));
                    }
    
                    int j = length - 1;
                    while (j >= 0)
                    {
                        int c = song[j];
                        if (c >= 0x80) c = c - 0x100;
    
                        t1 = (int)((c + t2) & 0x00000000FFFFFFFF);
                        t2 = (int)((t2 << (j % 2 + 4)) & 0x00000000FFFFFFFF);
                        t2 = (int)((t1 + t2) & 0x00000000FFFFFFFF);
                        j -= 1;
                    }
                    j = 0;
                    t1 = 0;
                    while (j <= length - 1)
                    {
                        int c = song[j];
                        if (c >= 128) c = c - 256;
                        int t4 = (int)((c + t1) & 0x00000000FFFFFFFF);
                        t1 = (int)((t1 << (j % 2 + 3)) & 0x00000000FFFFFFFF);
                        t1 = (int)((t1 + t4) & 0x00000000FFFFFFFF);
                        j += 1;
                    }
    
                    int t5 = (int)Conv(t2 ^ t3);
                    t5 = (int)Conv(t5 + (t1 | lrcId));
                    t5 = (int)Conv(t5 * (t1 | t3));
                    t5 = (int)Conv(t5 * (t2 ^ lrcId));
    
                    long t6 = (long)t5;
                    if (t6 > 2147483648)
                        t5 = (int)(t6 - 4294967296);
                    return t5.ToString();
                }
                private long Conv(int i)
                {
                    long r = i % 4294967296;
                    if (i >= 0 && r > 2147483648)
                        r = r - 4294967296;
    
                    if (i < 0 && r < 2147483648)
                        r = r + 4294967296;
                    return r;
                }
    
            }

    LrcInfo

       LrcInfo[] infos = LyricsHelper.GetLrcList("周杰伦", "不能说的秘密", @"C:\不能说的秘密.mp3");
            if (infos[0].DownloadLrc())
            {
                MessageBox.Show("下载成功");
            }

    2018年1月26日 2:55

全部回复

  • 可能是服务器的原因,您将下载的URL复制到浏览器中,看能否弹出下载对话框并保存文件。


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年1月29日 0:43
  • 请问这是什么类型的项目,我需要将您的帖子移到更适合讨论这个问题的论坛去。

    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月29日 7:37