Exemplo n.º 1
0
        /// <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.FromHours(6);
                    if (localChannelInfo != null && DateTime.Now - localChannelInfoTime < distance)
                    {
                        return localChannelInfo;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(DateTime.Now + " 获取本地频道列表失败:" + ex.Message);
            }

            //尝试获取服务器频道列表
            int tryCount = 0;
            while (true)
            {
                //获取服务器频道列表多次失败后采用本地频道列表
                if (tryCount == 5 && localChannelInfo != null) return localChannelInfo;

                ++tryCount;

                Debug.WriteLine(DateTime.Now + " 获取频道列表……");
                var file = new ConnectionBase().Get("http://doubanfmcloud-channelinfo.stor.sinaapp.com/channelinfo");
                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;
                }
            }
        }
Exemplo n.º 2
0
        /// <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;
                }
            }
        }