Example #1
0
        internal void GetOnlineLyric()
        {
            try
            {
                HasOriLyrics   = false;
                HasTransLyrics = false;
                Lyrics      tempOriLyric   = new Lyrics();
                Lyrics      tempTransLyric = new Lyrics();
                string      sLRC           = "";
                string      sContent;
                HttpRequest hr = new HttpRequest();
                sContent = hr.GetContent("http://music.163.com/api/song/media?id=" + ID);
                if (sContent.Substring(0, 4).Equals("ERR!"))
                {
                    ErrorLog = ErrorLog + "<RETURN ERR!>";
                    return;
                }

                //反序列化JSON数据
                JObject o = (JObject)JsonConvert.DeserializeObject(sContent);
                if (Regex.IsMatch(o.Root.ToString(), @"""lyric""") == false)
                {
                    ErrorLog = ErrorLog + "<CAN NOT FIND LYRIC LABEL>";
                    return;
                }
                sLRC = o["lyric"].ToString();
                tempOriLyric.ArrangeLyrics(sLRC);
                HasOriLyrics = true;
                MixedLyrics.ArrangeLyrics(sLRC);
                //===========翻译
                sContent = hr.GetContent("http://music.163.com/api/song/lyric?os=pc&id=" + ID + "&tv=-1");
                if (sContent.Substring(0, 4).Equals("ERR!"))
                {
                    ErrorLog = ErrorLog + "<RETURN ERR!>";
                    return;
                }
                //反序列化JSON数据
                o    = (JObject)JsonConvert.DeserializeObject(sContent);
                sLRC = o["tlyric"].ToString();
                o    = (JObject)JsonConvert.DeserializeObject(sLRC);
                sLRC = o["lyric"].ToString();
                tempTransLyric.ArrangeLyrics(sLRC);
                if (tempOriLyric.Count >= tempTransLyric.Count && tempTransLyric.Count != 0) //翻译可能比外文歌词少,下面会对时间轴来判断配对
                {
                    int j = 0;                                                               //j为外文歌词的index
                    for (int i = 0; i < tempTransLyric.Count && j < tempOriLyric.Count; j++)
                    {
                        if (tempOriLyric[j].Timeline != tempTransLyric[i].Timeline)
                        {
                            continue;
                        }
                        if (tempTransLyric[i].OriLyrics != null && tempTransLyric[i].OriLyrics != "")
                        {
                            MixedLyrics[j].SetTransLyrics("#", tempTransLyric[i].OriLyrics);//Mix是以外文歌词的j来充填,当没有trans的时候留空
                        }
                        i++;
                    }
                    HasTransLyrics = true;
                }
                MixedLyrics.Sort();
                tempOriLyric   = null;
                tempTransLyric = null;
            }
            catch (System.ArgumentNullException)
            {
                ErrorLog = ErrorLog + "<ArgumentNullException ERROR!>";
            }
            catch (System.NullReferenceException)
            {
                ErrorLog = ErrorLog + "<NullReferenceException ERROR!>";
            }
        }