/// <summary>
        /// 下载6分钟内的弹幕,返回弹幕列表
        /// </summary>
        /// <param name="avid">稿件avID</param>
        /// <param name="cid">视频CID</param>
        /// <param name="segmentIndex">分包,每6分钟一包</param>
        /// <returns></returns>
        public List <BiliDanmaku> GetDanmakuProto(long avid, long cid, int segmentIndex)
        {
            string url     = $"https://api.bilibili.com/x/v2/dm/web/seg.so?type=1&oid={cid}&pid={avid}&segment_index={segmentIndex}";
            string referer = "https://www.bilibili.com";

            FileDownloadUtil fileDownload = new FileDownloadUtil();

            fileDownload.Init(url, referer, Path.GetTempPath() + "downkyi/danmaku", $"{cid}-{segmentIndex}.proto", "DanmakuProtobuf");
            fileDownload.Download();

            var danmakuList = new List <BiliDanmaku>();

            DmSegMobileReply danmakus;

            try
            {
                using (var input = File.OpenRead(Path.GetTempPath() + $"downkyi/danmaku/{cid}-{segmentIndex}.proto"))
                {
                    danmakus = DmSegMobileReply.Parser.ParseFrom(input);
                    if (danmakus == null || danmakus.Elems == null)
                    {
                        return(danmakuList);
                    }

                    foreach (var dm in danmakus.Elems)
                    {
                        var danmaku = new BiliDanmaku
                        {
                            Id       = dm.Id,
                            Progress = dm.Progress,
                            Mode     = dm.Mode,
                            Fontsize = dm.Fontsize,
                            Color    = dm.Color,
                            MidHash  = dm.MidHash,
                            Content  = dm.Content,
                            Ctime    = dm.Ctime,
                            Weight   = dm.Weight,
                            //Action = dm.Action,
                            Pool = dm.Pool
                        };
                        danmakuList.Add(danmaku);
                    }
                }
            }
            catch (Exception e)
            {
#if DEBUG
                Console.WriteLine("发生异常: {0}", e);
#endif
                return(null);
            }

            return(danmakuList);
        }
Beispiel #2
0
        /// <summary>
        /// 下载6分钟内的弹幕,返回弹幕列表
        /// </summary>
        /// <param name="avid">稿件avID</param>
        /// <param name="cid">视频CID</param>
        /// <param name="segmentIndex">分包,每6分钟一包</param>
        /// <returns></returns>
        public static List <BiliDanmaku> GetDanmakuProto(long avid, long cid, int segmentIndex)
        {
            string url = $"https://api.bilibili.com/x/v2/dm/web/seg.so?type=1&oid={cid}&pid={avid}&segment_index={segmentIndex}";
            //string referer = "https://www.bilibili.com";

            string directory = Path.Combine(Storage.StorageManager.GetDanmaku(), $"{cid}");
            string filePath  = Path.Combine(directory, $"{segmentIndex}.proto");

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            try
            {
                System.Net.WebClient mywebclient = new System.Net.WebClient();
                mywebclient.DownloadFile(url, filePath);
            }
            catch (Exception e)
            {
                Utils.Debugging.Console.PrintLine("GetDanmakuProto()发生异常: {0}", e);
                //Logging.LogManager.Error(e);
            }

            var danmakuList = new List <BiliDanmaku>();

            try
            {
                using (var input = File.OpenRead(filePath))
                {
                    DmSegMobileReply danmakus = DmSegMobileReply.Parser.ParseFrom(input);
                    if (danmakus == null || danmakus.Elems == null)
                    {
                        return(danmakuList);
                    }

                    foreach (var dm in danmakus.Elems)
                    {
                        var danmaku = new BiliDanmaku
                        {
                            Id       = dm.Id,
                            Progress = dm.Progress,
                            Mode     = dm.Mode,
                            Fontsize = dm.Fontsize,
                            Color    = dm.Color,
                            MidHash  = dm.MidHash,
                            Content  = dm.Content,
                            Ctime    = dm.Ctime,
                            Weight   = dm.Weight,
                            //Action = dm.Action,
                            Pool = dm.Pool
                        };
                        danmakuList.Add(danmaku);
                    }
                }
            }
            catch (Exception e)
            {
                Utils.Debugging.Console.PrintLine("GetDanmakuProto()发生异常: {0}", e);
                //Logging.LogManager.Error(e);
                return(null);
            }

            return(danmakuList);
        }