Ejemplo n.º 1
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            if (obj is Dyncard)
            {
                Dyncard b = (Dyncard)obj;
                return(dynid == b.dynid);
            }
            return(base.Equals(obj));
        }
Ejemplo n.º 2
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);
            }
        }