/// <summary> /// 根据情况从本地或服务器获取频道列表。 /// </summary> /// <returns>频道列表</returns> private static ChannelInfo GetChannelInfo() { var localPath = Path.Combine(ConnectionBase.DataFolder, "channelinfo"); ChannelInfo localChannelInfo = null; //尝试获取本地频道列表 try { if (File.Exists(localPath)) { var localChannelInfoTime = File.GetLastWriteTime(localPath); var content = File.ReadAllText(localPath); var channelInfo = new ChannelInfo(Json.JsonHelper.FromJson <Json.ChannelInfo>(content)); if (channelInfo.IsEffective) { localChannelInfo = channelInfo; } //满足条件时采用本地频道列表 var distance = TimeSpan.FromSeconds(int.Parse(ConfigurationManager.AppSettings["Player.ChannelInfoExpireSeconds"])); if (localChannelInfo != null && DateTime.Now - localChannelInfoTime < distance) { return(localChannelInfo); } } } catch (Exception ex) { Debug.WriteLine(DateTime.Now + " 获取本地频道列表失败:" + ex.Message); } //尝试获取服务器频道列表 int tryCount = 0; int tryCountMax = int.Parse(ConfigurationManager.AppSettings["Player.ChannelInfoRetryCount"]); while (true) { //获取服务器频道列表多次失败后采用本地频道列表 if (tryCount == tryCountMax && localChannelInfo != null) { return(localChannelInfo); } ++tryCount; Debug.WriteLine(DateTime.Now + " 获取频道列表……"); var file = new ConnectionBase { UseGzip = true }.Get( string.Format(ConfigurationManager.AppSettings["Player.ChannelInfoUrlFormat"], typeof(Player).Assembly.GetName().Version)); Debug.WriteLine(DateTime.Now + " 获取频道列表完成"); if (string.IsNullOrEmpty(file)) { TakeABreak(); continue; } var channelInfo = new ChannelInfo(Json.JsonHelper.FromJson <Json.ChannelInfo>(file)); if (!channelInfo.IsEffective) { Debug.WriteLine(DateTime.Now + " 获取频道列表失败"); TakeABreak(); } else { Debug.WriteLine(DateTime.Now + " 获取频道列表成功"); try { File.WriteAllText(localPath, file); } catch (Exception ex) { Debug.WriteLine(DateTime.Now + " 写入本地频道列表失败:" + ex.Message); } return(channelInfo); } } }
/// <summary> /// 保存Cookies /// </summary> /// <returns>成功与否</returns> static bool SaveCookies() { return(ConnectionBase.SaveCookies()); }
/// <summary> /// 获取播放列表 /// </summary> /// <param name="playerState">播放器状态</param> /// <param name="operationType">操作类型</param> /// <returns> /// 播放列表 /// </returns> internal static PlayList GetPlayList(Player.PlayerState playerState, string operationType) { //构造链接 Parameters parameters = new Parameters(); parameters["app_name"] = "radio_desktop_win"; parameters["version"] = "100"; parameters["user_id"] = playerState.CurrentUser.UserID; parameters["token"] = playerState.CurrentUser.Token; parameters["expire"] = playerState.CurrentUser.Expire; parameters["from"] = "mainsite"; parameters["context"] = playerState.CurrentChannel.Context; parameters["sid"] = playerState.CurrentSong != null ? playerState.CurrentSong.SongId : null; parameters["channel"] = playerState.CurrentChannel.Id; parameters["type"] = operationType; random.NextBytes(bytes); parameters["r"] = (BitConverter.ToUInt64(bytes, 0) % 0xFFFFFFFFFF).ToString("x10"); if (playerState.CurrentUser.IsPro) { string kbps = null; switch (playerState.CurrentUser.ProRate) { case ProRate.Kbps64: kbps = "64"; break; case ProRate.Kbps128: kbps = "128"; break; case ProRate.Kbps192: kbps = "192"; break; default: break; } parameters["kbps"] = kbps; } string url = ConnectionBase.ConstructUrlWithParameters("http://www.douban.com/j/app/radio/people", parameters); //获取列表 string json = new ConnectionBase().Get(url, @"application/json, text/javascript, */*; q=0.01", @"http://douban.fm"); var jsonPlayList = Json.JsonHelper.FromJson <Json.PlayList>(json); if (jsonPlayList != null && jsonPlayList.r) { RaiseGetPlayListFailedEvent(json); } PlayList pl = new PlayList(jsonPlayList); //将小图更换为大图 foreach (var s in pl) { s.Picture = s.Picture.Replace("/mpic/", "/lpic/").Replace("//otho.", "//img3."); } //去广告 pl.RemoveAll(s => s.IsAd); return(pl); }