Ejemplo n.º 1
0
        /// <summary>
        /// 从专辑信息中获取歌曲的中文名
        /// </summary>
        /// <param name="sName"></param>
        /// <param name="pAlbum"></param>
        /// <returns></returns>
        public static string convertSongTitle(string sName, CloudMusicAlbum pAlbum)
        {
            if (pAlbum == null || pAlbum.songs.Count <= 0)
            {
                return(sName);
            }

            int iIndex  = -1;
            int iWeight = 0;

            for (int i = 0; i < pAlbum.songs.Count; i++)
            {
                int iTmp = calcWeight(sName, pAlbum.songs[i].name);
                if (iTmp > iWeight)
                {
                    iIndex  = i;
                    iWeight = iTmp;
                }
            }
            if (iIndex < 0)
            {
                return(sName);
            }
            return(pAlbum.songs[iIndex].name);
        }
Ejemplo n.º 2
0
        public void DownloadTrack()
        {
            string Errlabel = "";

            //GetStream
            Progress.StatusMsg = "GetStream...";
            StreamUrl TidalStream = TidalTool.getStreamUrl(TidalTrack.ID.ToString(), Quality, out Errlabel);

            if (Errlabel.IsNotBlank())
            {
                goto ERR_RETURN;
            }

            //Get path
            FilePath = TidalTool.getTrackPath(OutputDir, TidalAlbum, TidalTrack, TidalStream.Url, AddHyphen, TidalPlaylist, artistBeforeTitle: ArtistBeforeTitle);

            //Check if song is downloaded already
            string CheckName = OnlyM4a ? FilePath.Replace(".mp4", ".m4a") : FilePath;

            if (CheckExist && System.IO.File.Exists(CheckName))
            {
                Progress.Update(100, 100);
                Progress.SetStatus(ProgressHelper.STATUS.COMPLETE);
                return;
            }

            //Get contributors
            ObservableCollection <Contributor> pContributors = TidalTool.getTrackContributors(TidalTrack.ID.ToString(), out Errlabel);

            //To chinese
            if (ToChinese)
            {
                CloudMusicAlbum cloalbum = Chinese.matchAlbum(TidalAlbum.Title, TidalAlbum.Artist.Name);
                string          chnname  = Chinese.convertSongTitle(TidalTrack.Title, cloalbum);
                if (chnname != TidalTrack.Title)
                {
                    FilePath         = TidalTool.getTrackPath(OutputDir, TidalPlaylist != null ? null : TidalAlbum, TidalTrack, TidalStream.Url, AddHyphen, TidalPlaylist, chnname, artistBeforeTitle: ArtistBeforeTitle);
                    TidalTrack.Title = chnname;
                }
            }

            //Download
            Progress.StatusMsg = "Start...";
            for (int i = 0; i < 100 && Progress.GetStatus() != ProgressHelper.STATUS.CANCLE; i++)
            {
                if ((bool)DownloadFileHepler.Start(TidalStream.Url, FilePath, Timeout: 5 * 1000, UpdateFunc: UpdateDownloadNotify, ErrFunc: ErrDownloadNotify, Proxy:TidalTool.PROXY))
                {
                    //Decrypt
                    if (!TidalTool.DecryptTrackFile(TidalStream, FilePath))
                    {
                        Errlabel = "Decrypt failed!";
                        goto ERR_RETURN;
                    }

                    if (OnlyM4a)
                    {
                        string sNewName;
                        if (!TidalTool.ConvertMp4ToM4a(FilePath, out sNewName))
                        {
                            Errlabel = "Convert mp4 to m4a failed!";
                            goto ERR_RETURN;
                        }
                        else
                        {
                            FilePath = sNewName;
                        }
                    }

                    //SetMetaData
                    if (TidalAlbum == null && TidalTrack.Album != null)
                    {
                        string sErrcode = null;
                        TidalAlbum = TidalTool.getAlbum(TidalTrack.Album.ID.ToString(), out sErrcode);
                    }
                    string sLabel = TidalTool.SetMetaData(FilePath, TidalAlbum, TidalTrack, TidalTool.getAlbumCoverPath(OutputDir, TidalAlbum), pContributors);
                    if (sLabel.IsNotBlank())
                    {
                        Errlabel = "Set metadata failed!";
                        goto ERR_RETURN;
                    }

                    Progress.SetStatus(ProgressHelper.STATUS.COMPLETE);
                    return;
                }
            }
            Errlabel = "Download failed!";

ERR_RETURN:
            if (Progress.GetStatus() == ProgressHelper.STATUS.CANCLE)
            {
                return;
            }

            ErrlabelHeight = 15;
            Progress.SetStatus(ProgressHelper.STATUS.ERROR);
            Progress.Errmsg = Errlabel;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 根据专辑和歌手获取匹配的网易云专辑
        /// </summary>
        /// <param name="sAlbumName">专辑名</param>
        /// <param name="sArtistName">歌手</param>
        /// <returns></returns>
        public static CloudMusicAlbum matchAlbum(string sAlbumName, string sArtistName)
        {
            //使用豆瓣接口搜索专辑,支持英文、中文、繁体搜索
            string serr;
            string stxt = (string)HttpHelper.GetOrPost("https://api.douban.com/v2/music/search?q=" + sAlbumName, out serr);
            List <DoubanMusicRecord> pDoubans = JsonHelper.ConverStringToObject <List <DoubanMusicRecord> >(stxt, "musics");

            //使用网易云接口搜索歌手
            string stxt2 = (string)HttpHelper.GetOrPost(string.Format("http://music.163.com/api/search/pc?s={0}&type=100&limit=10&offset=0", sArtistName), out serr);
            List <CloudMusicArtist> pClounds = JsonHelper.ConverStringToObject <List <CloudMusicArtist> >(stxt2, "result", "artists");

            //匹配
            int iIndex1 = -1;
            int iIndex2 = -1;

            for (int i = 0; pClounds != null && i < pClounds.Count && iIndex1 == -1; i++)
            {
                string skey = pClounds[i].name;
                for (int j = 0; pDoubans != null && j < pDoubans.Count && iIndex1 == -1; j++)
                {
                    for (int k = 0; pDoubans[j].author != null && k < pDoubans[j].author.Count; k++)
                    {
                        string stmp = converSimpleChinese(pDoubans[j].author[k].name);
                        if (skey == stmp || skey.Contains(stmp) || stmp.Contains(skey) || stmp == sArtistName)
                        {
                            iIndex1 = i;
                            iIndex2 = j;
                            break;
                        }
                    }
                }
            }

            if (iIndex1 < 0)
            {
                return(null);
            }

            string sname = converSimpleChinese(pDoubans[iIndex2].title);
            string stxt3 = (string)HttpHelper.GetOrPost(string.Format("http://music.163.com/api/search/pc?s={0}&type=10&limit=30&offset=0", sname), out serr);
            List <CloudMusicAlbum> pCloundAlbums = JsonHelper.ConverStringToObject <List <CloudMusicAlbum> >(stxt3, "result", "albums");

            //匹配
            int iIndex3 = -1;

            for (int i = 0; pCloundAlbums != null && i < pCloundAlbums.Count; i++)
            {
                if (pCloundAlbums[i].artist.name == pClounds[iIndex1].name &&
                    pCloundAlbums[i].name == sname)
                {
                    iIndex3 = i;
                    break;
                }
            }
            if (iIndex3 < 0)
            {
                return(null);
            }

            string          stxt4  = (string)HttpHelper.GetOrPost(string.Format("http://music.163.com/api/album/{0}?ext=true&id={1}&offset=0&total=true&limit=10", pCloundAlbums[iIndex3].id.ToString(), pCloundAlbums[0].id.ToString()), out serr);
            CloudMusicAlbum pAlbum = JsonHelper.ConverStringToObject <CloudMusicAlbum>(stxt4, "album");

            return(pAlbum);
        }