예제 #1
0
        private static void ResponseDreamInfo(HttpListenerContext context, DreamInfo dream)
        {
            var obj = new JObject {
                { "status", 200 }, { "error", 0 }
            };
            var inner = new JObject();

            obj.Add("data", inner);
            foreach (var stat in dream.Status)
            {
                inner.Add(stat.Key, stat.Value);
            }
            JArray steps = new JArray();

            inner.Add("steps", steps);
            foreach (var dreamStep in dream.Steps)
            {
                JObject stepContent = new JObject
                {
                    { "id", dreamStep.Status["sid"] },
                    { "content", dreamStep.Status["content"] },
                    { "isremoved", dreamStep.IsRemoved },
                    { "lastupdated", dreamStep.Status.ContainsKey("lastupdated") ? dreamStep.Status["lastupdated"] : "0" }
                };
                steps.Add(stepContent);
            }
            context.ResponseString(obj.ToString());
        }
예제 #2
0
        public void DisplayStepInfo(HttpListenerContext context, string subUrl)
        {
            string inUrl = context.Request.RawUrl.Replace(subUrl, "");

            string[] splited = inUrl.Split('?')[0].Split('/');
            if (splited.Length == 0 || splited.Length == 1)
            {
                ResponseUserInfo(context);
                return;
            }
            DreamInfo dream = data.Dreams.FirstOrDefault(d => d.Status["id"] == splited[1]);

            if (dream == null)
            {
                FailReturn(context, "Dream" + splited[1] + "Not Found");
                return;
            }
            else
            {
                if (splited.Length == 2)
                {
                    ResponseDreamInfo(context, dream);
                    return;
                }
            }
            StepInfo step = dream.Steps.FirstOrDefault(d => d.Status["sid"] == splited[2]);

            if (step == null)
            {
                FailReturn(context, "Step" + splited[2] + "Not Found");
                return;
            }
            else
            {
                if (splited.Length == 3)
                {
                    ResponseStepInfo(context, step);
                    return;
                }
            }
            CommentInfo cmt = step.Comments.FirstOrDefault(d => d.Status["id"] == splited[3]);

            if (cmt == null)
            {
                FailReturn(context, "Comment" + splited[3] + "Not Found");
                return;
            }
            else
            {
                if (splited.Length == 4)
                {
                    ResponseCommentInfo(context, cmt);
                    return;
                }
            }
            FailReturn(context, "???");
        }
예제 #3
0
        private void GetDreamList(int targetUser, NianData data)
        {
            int page = 1;

            if (data.Dreams == null)
            {
                data.Dreams = new List <DreamInfo>();
            }
            for (page = 1;
                 page <= Math.Ceiling(
                     int.Parse(data.ListItems["dream"]) / 20.0f);
                 page++)
            {
                var result = api.GetUserDreams(targetUser.ToString(), page);
                foreach (var dreamStatus in (JArray)result["dreams"])
                {
                    var dreamInfo = (JObject)dreamStatus;
                    var id        = dreamInfo["id"].Value <string>();
                    var title     = dreamInfo["title"].Value <string>();
                    var di        = data.Dreams.FirstOrDefault(
                        info => info.Status["id"] == id);
                    if (di == null)
                    {
                        di = new DreamInfo();
                        data.Dreams.Add(di);
                        DiffDetected?.Invoke(
                            "http://nian.so/#!/dream/" + id,
                            uName + "新增了梦想" + title,
                            uName + "新增了梦想" + title,
                            "Nian." + targetUser + ".Dream." + id + ".Info");
                        di.Status = new Dictionary <string, string>();
                        di.Steps  = new List <StepInfo>();
                    }
                    foreach (var val in dreamInfo)
                    {
                        di.Status[val.Key] = val.Value.Value <string>();
                    }
                }
            }
            foreach (var dataDream in data.Dreams)
            {
                GetDreamExtendInfo(dataDream, data, currentPeroid % privatePeroid != 0);
            }
        }
예제 #4
0
        /// <summary>
        /// 对应数据项的增量抓取函数表
        /// </summary>

        private void GetDreamExtendInfo(DreamInfo dream, NianData data, bool Incremantal = true)
        {
            int lastupdated = 0;
            {
                string tmp = "0";
                dream.Status.TryGetValue("lastupdated", out tmp);
                lastupdated = int.Parse(tmp);
            }
            int maxPage = 1;
            //Origin Target
            int  unresolvedDiff = 0;
            bool isAllResolved  = false;
            int  pos            = dream.Steps.FindLastIndex(info => info.IsRemoved == false);

            bool            isFoundHead   = false;//是否找到最近出现的元素
            List <StepInfo> pendingInsert = new List <StepInfo>();
            int             page;

            for (page = 1; page <= maxPage; page++)
            {
                if (dream.Status.ContainsKey("private"))
                {
                    if (dream.Status["private"] == "2")
                    {
                        continue;                                 //被删除的记本
                    }
                    if (dream.Status["private"] == "1" && currentPeroid % privatePeroid != 0)
                    {
                        return;
                    }
                    //私密记本一天只能拉取1次
                }
                var result = api.GetDreamUpdate(dream.Status["id"], page);
                //Get Extend Dream Info

                var id    = dream.Status["id"];
                var title = dream.Status["title"];
                if (page == 1)
                {
                    var dreamInfo = (JObject)result["dream"];
                    foreach (var values in dreamInfo)
                    {
                        string valItem = "";
                        if (values.Value is JArray)
                        {
                            continue;
                        }
                        string targetValue = values.Value.Value <string>();
                        if (dream.Status.ContainsKey(values.Key))
                        {
                            valItem = dream.Status[values.Key];
                        }
                        if (values.Key == "step")
                        {
                            unresolvedDiff =
                                int.Parse(targetValue) - int.Parse("0" + valItem);
                        }
                        if (valItem != targetValue)
                        {
                            DiffDetected?.Invoke(
                                "http://nian.so/#!/dream/" + id,
                                uName + "修改了梦想" + title + "的信息",
                                "属性" + values.Key + "从" + valItem + "变为" + targetValue,
                                "Nian." + TargetUID + ".Dream." + id + ".Info." + values.Key);
                            dream.Status[values.Key] = targetValue;
                        }
                    }
                    maxPage = (int)Math.Ceiling(int.Parse(dream.Status["step"]) / 20.0f);
                }
                if (unresolvedDiff == 0 && Incremantal)//需要翻到下一页才能完全保证,因为有同时删除和增加的情况
                {
                    if (isAllResolved)
                    {
                        break;
                    }
                    else
                    {
                        isAllResolved = true;
                    }
                }
                else
                {
                    isAllResolved = false;
                }
                var stepsArray = (JArray)result["steps"];
                foreach (var stepInfo in stepsArray)
                {
                    StepInfo si;
                    int      unresolvedComment = 0;
                    var      currentId         = stepInfo["sid"].Value <string>();
                    int      index             = dream.Steps.FindIndex(item => item.Status["sid"] == currentId);
                    StepInfo refsi             = dream.Steps.ElementAtOrDefault(index);
                    string   sid = refsi?.Status["sid"];
                    if (index == -1)
                    {
                        if (isFoundHead)
                        {
                            throw new Exception("莫名的顺序,怀疑不对");
                        }
                        si = new StepInfo
                        {
                            Status   = new Dictionary <string, string>(),
                            Images   = new List <string>(),
                            Comments = new List <CommentInfo>()
                        };
                        pendingInsert.Add(si);
                        foreach (var val in (JObject)stepInfo)
                        {
                            if (val.Key == "images")
                            {
                                foreach (var imgPath in (JArray)val.Value)
                                {
                                    si.Images.Add(imgPath["path"].Value <string>());
                                }
                            }
                            else
                            {
                                si.Status[val.Key] = val.Value.Value <string>();
                            }
                        }
                        DiffDetected?.Invoke(
                            "http://nian.so/m/step/" + currentId,
                            uName + "在记本" + title + "发布了一条足迹",
                            "足迹内容:" + si.Status["content"],
                            "Nian." + TargetUID + ".Dream." + id + ".Step." + currentId);
                        unresolvedComment = ((JObject)stepInfo)["comments"].Value <int>();
                        unresolvedDiff--;
                        si.Status["lastupdated"] = DateTime.Now.ToUnixTime().ToString();
                        lastupdated = (int)DateTime.Now.ToUnixTime();
                    }
                    else
                    {
                        isFoundHead = true;
                        if (refsi.IsRemoved == true)
                        {
                            refsi.IsRemoved = false; //修复之前因为转为private被误标记为已删除的内容
                            pos             = index;
                        }
                        if (pos - index < 0)
                        {
                            throw new Exception("???WTF???");
                        }
                        for (int j = index + 1; j <= pos; j++)
                        {
                            if (!dream.Steps[j].IsRemoved)
                            {
                                dream.Steps[j].IsRemoved = true;
                                unresolvedDiff++;
                                DiffDetected?.Invoke(
                                    "http://nian.so/m/dream/" + dream.Status["id"],
                                    uName + "删除了一条在" + title + "的足迹",
                                    "足迹内容:" + dream.Steps[j].Status["content"],
                                    "Nian." + TargetUID + ".Dream." + id + ".Step." + dream.Steps[j].Status["sid"]);
                                dream.Steps[j].Status["lastupdated"] = DateTime.Now.ToUnixTime().ToString();
                                lastupdated = (int)DateTime.Now.ToUnixTime();
                            }
                        }
                        pos = index - 1;
                        unresolvedComment =
                            int.Parse(refsi.Status["comments"]) -
                            ((JObject)stepInfo)["comments"].Value <int>();
                        refsi.Status["comments"] = ((JObject)stepInfo)["comments"].Value <string>();
                        si = refsi;
                    }

                    //需要检测的只有comment
                    if (unresolvedComment != 0)
                    {
                        ResolveComment(si, unresolvedComment);
                        si.Status["lastupdated"] = DateTime.Now.ToUnixTime().ToString();
                        lastupdated = (int)DateTime.Now.ToUnixTime();
                    }
                }
                dream.Status["lastupdated"] = lastupdated.ToString();
            }
            page -= 1;                                         //for跳出的那一轮需要减去
            if ((page == maxPage || maxPage == 0) && pos >= 0) //最后一页时如果pos不等于0,说明有step被删除了
            {
                for (int j = pos; j >= 0; j--)
                {
                    if (!dream.Steps[j].IsRemoved)
                    {
                        dream.Steps[j].IsRemoved = true;
                        unresolvedDiff++;
                        DiffDetected?.Invoke(
                            "http://nian.so/m/dream/" + dream.Status["id"],
                            uName + "删除了一条在" + dream.Status["title"] + "的足迹",
                            "足迹内容:" + dream.Steps[j].Status["content"],
                            "Nian." + TargetUID + ".Dream." + dream.Status["id"] + ".Step." + dream.Steps[j].Status["sid"]);
                    }
                }
            }
            if (unresolvedDiff != 0)
            {
                Console.WriteLine("Still Have UnresolvedDiff!!!!" + " Offset:" + unresolvedDiff);
            }
            for (int j = pendingInsert.Count - 1; j >= 0; j--)
            {
                dream.Steps.Add(pendingInsert[j]);
            }
        }