コード例 #1
0
 protected override List <SongInfo> GetPlaylist(string keyword)
 {
     try
     {
         NeteaseMusic.SongInfo[] songs;
         if (long.TryParse(keyword, out long id))
         {
             songs = MainConfig.Instance.LoginSession.LoginStatus ?
                     NeteaseMusicApi.GetPlayList(MainConfig.Instance.LoginSession, id) :
                     NeteaseMusicApi.GetPlayList(id);
         }
         else
         {
             string  json = NeteaseMusicApi.Search(keyword, SearchType.SongList, 1);
             JObject j    = JObject.Parse(json);
             if (j["code"].ToObject <int>() == 200)
             {
                 id = j["result"]["playlists"].Select(p => p["id"].ToObject <long>()).FirstOrDefault();
                 if (id > 0)
                 {
                     songs = MainConfig.Instance.LoginSession.LoginStatus ?
                             NeteaseMusicApi.GetPlayList(MainConfig.Instance.LoginSession, id) :
                             NeteaseMusicApi.GetPlayList(id);
                 }
                 else
                 {
                     Log("在获取歌单时收到了未知的服务器返回喵:" + json);
                     return(null);
                 }
             }
             else
             {
                 Log("在获取歌单时收到了未知的服务器返回喵:" + json);
                 return(null);
             }
         }
         NeteaseMusic.SongInfo[] cantPlaySongs = songs.Where(p => !p.CanPlay).ToArray();
         if (cantPlaySongs.Length > 0)
         {
             if (songs.Length == cantPlaySongs.Length)
             {
                 Log("该歌单内所有的单曲,网易云都没有版权,所以歌单添加失败了喵");
                 return(null);
             }
             Log($"以下列出的单曲,网易云暂时没有版权,所以它们被除外了喵~\n{string.Join("\n", cantPlaySongs.Select(p => $"{string.Join("; ", p.Artists.Select(q => q.Name))} - {p.Name}"))}");
         }
         return(songs.Where(p => p.CanPlay).Select(p => new SongInfo(this, PlatformType.NeteaseMusic, p.Id.ToString(), p.Name, p.Artists.Select(q => q.Name).ToArray(), Convert.ToString(p.Album.Id), null)).ToList());
     }
     catch (WebException e)
     {
         Log($"获取歌单失败了喵:{e.Message}\r\n这是由于网络原因导致获取失败, 如果多次出现, 请检查你的网络连接喵。");
     }
     catch (Exception e)
     {
         Log($"获取歌单失败了喵:{e.Message}");
     }
     return(null);
 }
コード例 #2
0
 private LyricInfo _GetLyric(long id, bool useCache = true)
 {
     if (!useCache || !LyricCache.ContainsKey(id))
     {
         LyricInfo lyric = NeteaseMusicApi.GetLyric(id);
         LyricCache[id] = lyric;
     }
     return(LyricCache[id]);
 }
コード例 #3
0
        private DownloadSongInfo _GetDownloadSongInfo(NeteaseSession session, long id, Quality quality, bool useCache = true)
        {
            DownloadSongInfo dsi;

            lock (DownloadSongInfoCache)
            {
                DownloadSongInfoCache.RemoveAll(p => p.ExpireTime < DateTime.Now);
                dsi = DownloadSongInfoCache.Find(p => p.Id == id && p.RequestQuality == quality);
            }
            if (!useCache || dsi == null)
            {
                IDictionary <long, DownloadSongInfo> dss = NeteaseMusicApi.GetSongsUrl(session, MainConfig.Instance.Quality, songIds: id);
                if (dss.TryGetValue(id, out dsi))
                {
                    DownloadSongInfoCache.Add(dsi);
                }
            }
            return(dsi);
        }
コード例 #4
0
 protected override SongInfo Search(string keyword)
 {
     try
     {
         NeteaseMusic.SongInfo[] songs = MainConfig.Instance.LoginSession.LoginStatus ?
                                         NeteaseMusicApi.SearchSongs(MainConfig.Instance.LoginSession, keyword, 1) :
                                         NeteaseMusicApi.SearchSongs(keyword, 1);
         NeteaseMusic.SongInfo song = songs.FirstOrDefault();
         if (song?.CanPlay == true)
         {
             LyricInfo lyric = null;
             try
             {
                 lyric = _GetLyric(song.Id);
             }
             catch (Exception e)
             {
                 Log($"获取歌词失败了喵:{e.Message}");
             }
             return(new SongInfo(this, PlatformType.NeteaseMusic, song.Id.ToString(), song.Name, song.Artists.Select(p => p.Name).ToArray(), Convert.ToString(song.Album.Id), lyric?.GetLyricText()));
         }
         else
         {
             Log($"{song.ArtistNames} - {song.Name} : 暂无版权喵");
         }
     }
     catch (WebException e)
     {
         Log($"搜索单曲失败了喵:{e.Message}\r\n这是由于网络原因导致搜索失败, 如果多次出现, 请检查你的网络连接喵。");
     }
     catch (Exception e)
     {
         Log($"搜索单曲失败了喵:{e.Message}");
     }
     return(null);
 }