示例#1
0
        public bool sendDanmaku(string message, int fsize = 25, int color = 16777215, int bubble = 0)
        {
            if (lastsend_dmk != null)
            {
                while ((DateTime.Now - lastsend_dmk).TotalSeconds < 3)
                {
                    ;
                }
            }
            lastsend_dmk = DateTime.Now;
            Dictionary <string, string> kvs = new Dictionary <string, string>();
            CookieCollection            ck  = sess.CookieContext;
            JObject job = new JObject();

            kvs.Add("color", color.ToString());
            kvs.Add("fontsize", fsize.ToString());
            kvs.Add("mode", "1");
            kvs.Add("msg", message);
            kvs.Add("rnd", TimestampHandler.GetTimeStamp(DateTime.Now).ToString());
            kvs.Add("roomid", roomid.ToString());
            kvs.Add("bubble", bubble.ToString());
            kvs.Add("csrf_token", ck["bili_jct"].Value);
            kvs.Add("csrf", ck["bili_jct"].Value);
            string response = sess._post_with_cookies("https://api.live.bilibili.com/msg/send", kvs);

            if (response == "")
            {
                return(false);
            }

            JObject raw_json = (JObject)JsonConvert.DeserializeObject(response);

            return(raw_json.Value <int>("code") == 0);
        }
        public List <Dyncard> fetchDynamics()
        {
            List <Dyncard> dynss   = new List <Dyncard>();
            string         jstring = sess.getBiliUserDynamicJson(uid);

            try
            {
                JObject jb = (JObject)JsonConvert.DeserializeObject(jstring);
                if (jb.Value <int>("code") != 0)
                {
                    throw new ApiRemoteException(jb);
                }
                if (jb == null || jb["data"] == null || jb["data"]["cards"] == null)
                {
                    return(new List <Dyncard>());
                }
                ExceptionCollection excpts = new ExceptionCollection();
                foreach (JToken jobj in jb["data"]["cards"])
                {
                    try
                    {
                        JObject j    = (JObject)jobj;
                        JObject card = (JObject)JsonConvert.DeserializeObject(j["card"].ToString());
                        Dyncard dyn  = new Dyncard
                        {
                            dynid    = j["desc"].Value <long>("dynamic_id"),
                            sendtime = TimestampHandler.GetDateTime(j["desc"].Value <long>("timestamp")),
                            type     = j["desc"].Value <int>("type"),
                            view     = j["desc"].Value <int>("view"),
                            repost   = j["desc"].Value <int>("repost"),
                            like     = j["desc"].Value <int>("like"),
                            rid      = j["desc"].Value <long>("rid")
                        };
                        if (j["desc"]["user_profile"]["info"]["uname"] != null)//如果给了更多信息,就直接用上
                        {
                            dyn.sender = new BiliUser(j["desc"]["user_profile"]["info"].Value <int>("uid"),
                                                      j["desc"]["user_profile"]["info"].Value <string>("uname"),
                                                      "未知",
                                                      j["desc"]["user_profile"].Value <string>("sign"),
                                                      false,
                                                      0,
                                                      j["desc"]["user_profile"]["info"].Value <string>("face"),
                                                      j["desc"]["user_profile"]["level_info"].Value <int>("current_level"),
                                                      0,
                                                      new BiliUser.OfficialInfo(),
                                                      sess);
                        }
                        else//如果没有信息就从缓存抓取
                        if (BiliUser.userlist.ContainsKey(j["desc"]["user_profile"]["info"].Value <int>("uid")))
                        {
                            dyn.sender = BiliUser.getUser(j["desc"]["user_profile"]["info"].Value <int>("uid"), sess);
                            //使用用户数据缓存来提高速度
                            //因为监听的是同一个账号,所以缓存命中率超高
                        }
                        else//如果缓存未命中,就拿获得的UID抓取剩余信息
                        {
                            dyn.sender = BiliUser.getUser(j["desc"]["user_profile"]["info"].Value <int>("uid"), sess);
                        }

                        if (j["desc"]["orig_type"] != null)
                        {
                            dyn.origintype = j["desc"].Value <int>("orig_type");
                        }
                        if (card["origin"] != null)
                        {
                            dyn.card_origin = (JObject)JsonConvert.DeserializeObject(card["origin"].ToString());
                        }
                        switch (dyn.type)
                        {
                        case 1:    //普通动态
                        case 4:    //?出现在转发和普通动态
                            dyn.dynamic = card["item"].Value <string>("content");
                            if (dyn.dynamic.Length > 23)
                            {
                                dyn.short_dynamic = dyn.dynamic.Substring(0, 20) + "...";
                            }
                            else
                            {
                                dyn.short_dynamic = dyn.dynamic;
                            }

                            break;

                        case 2:    //图片
                            dyn.dynamic = card["item"].Value <string>("description");
                            if (dyn.dynamic.Length > 23)
                            {
                                dyn.short_dynamic = dyn.dynamic.Substring(0, 20) + "...";
                            }
                            else
                            {
                                dyn.short_dynamic = dyn.dynamic;
                            }

                            break;

                        case 256:    //音频
                            break;

                        case 8:    //视频
                            dyn.vinfo = new Videoinfo
                            {
                                bvid        = j["desc"].Value <string>("bvid"),
                                title       = card.Value <string>("title"),
                                discription = card.Value <string>("desc")
                            };
                            if (dyn.vinfo.discription.Length > 23)
                            {
                                dyn.vinfo.short_discription = dyn.vinfo.discription.Substring(0, 20) + "...";
                            }
                            else
                            {
                                dyn.vinfo.short_discription = dyn.vinfo.discription;
                            }

                            dyn.vinfo.av = j["desc"].Value <int>("rid");
                            dyn.dynamic  = card.Value <string>("dynamic");
                            break;

                        default:
                            break;
                        }
                        dynss.Add(dyn);
                    }
                    catch (Exception err)
                    {
                        excpts.Add(err);
                    }
                }
                if (excpts.Count > 0)
                {
                    throw excpts;
                }
                //while (dynss.Count > 5)
                //{
                //    dynss.RemoveAt(5);
                //}
                return(dynss);
            }
            catch (ExceptionCollection)
            {
                throw;
            }
            catch (Exception err)
            {
                throw new UnexpectedResultException(jstring, err);
            }
        }