Example #1
0
        public async Task <List <NicoNicoCommentList> > GetAllCommentAsync()
        {
            var normalList    = new NicoNicoCommentList("通常コメント");
            var uploaderList  = new NicoNicoCommentList("投稿者コメント");
            var communityList = new NicoNicoCommentList("コミュニティコメント");
            var channelList   = new NicoNicoCommentList("チャンネルコメント");
            var officialList  = new NicoNicoCommentList("公式コメント");

            try {
                var ret = new List <NicoNicoCommentList>();

                //通常コメントを取得
                //ret.Add(new NicoNicoCommentList() { ListName = "通常コメント", CommentList = await GetNormalCommentAsync() });

                //パケットIDとコメントタイプを紐付ける
                var dic = new Dictionary <long, CommentType>();

                var jsonarray = new List <object>();
                {
                    PacketId = PacketCount;
                    //rs
                    jsonarray.Add(new { ping = new { content = "rs:" + RequestId % IdLimit } });

                    //ps 通常コメント
                    jsonarray.Add(new { ping = new { content = "ps:" + PacketId % IdLimit } });

                    var normalThread = new {
                        thread      = Owner.ApiData.Thread.ThreadIds.Default,
                        version     = "20090904",
                        language    = 0,
                        user_id     = int.Parse(Owner.ApiData.Viewer.Id),
                        with_global = 1,
                        scores      = 1,
                        nicoru      = 0,
                        userkey     = Owner.ApiData.Context.UserKey
                    };

                    dic[PacketId] = CommentType.Normal;
                    jsonarray.Add(new { thread = normalThread });

                    //pf
                    jsonarray.Add(new { ping = new { content = "pf:" + PacketId++ % IdLimit } });
                    //------
                    //ps 通常コメント leaves
                    jsonarray.Add(new { ping = new { content = "ps:" + PacketId % IdLimit } });

                    var normalLeaves = new {
                        thread   = Owner.ApiData.Thread.ThreadIds.Default,
                        language = 0,
                        user_id  = int.Parse(Owner.ApiData.Viewer.Id),
                        content  = "0-" + ((Owner.ApiData.Video.Duration / 60) + 1) + ":100," + GetReadCommentCount(Owner.ApiData.Video.Duration).ToString(),
                        scores   = 1,
                        nicoru   = 0,
                        userkey  = Owner.ApiData.Context.UserKey
                    };

                    dic[PacketId] = CommentType.NormalLeaves;
                    jsonarray.Add(new { thread_leaves = normalLeaves });
                    //pf leaves
                    jsonarray.Add(new { ping = new { content = "pf:" + PacketId++ % IdLimit } });
                    //------

                    //投稿者コメント有りフラグ
                    if (Owner.ApiData.Thread.HasOwnerThread == "1")
                    {
                        //ps 投稿者コメント
                        jsonarray.Add(new { ping = new { content = "ps:" + PacketId % IdLimit } });

                        var forkThread = new {
                            thread      = Owner.ApiData.Thread.ThreadIds.Default,
                            version     = "20061206",
                            fork        = 1,
                            user_id     = int.Parse(Owner.ApiData.Viewer.Id),
                            res_from    = -1000,
                            with_global = 1,
                            scores      = 1,
                            nicoru      = 0,
                            userkey     = Owner.ApiData.Context.UserKey
                        };

                        dic[PacketId] = CommentType.Uploader;
                        jsonarray.Add(new { thread = forkThread });
                        //pf fork
                        jsonarray.Add(new { ping = new { content = "pf:" + PacketId++ % IdLimit } });
                    }

                    if (Owner.ApiData.IsChannelVideo)
                    {
                        //ps チャンネルコメント
                        jsonarray.Add(new { ping = new { content = "ps:" + PacketId % IdLimit } });

                        var query = new GetRequestQuery(ThreadKeyApiUrl);
                        query.AddQuery("thread", Owner.ApiData.Thread.ThreadIds.Community);

                        var threadkey = await App.ViewModelRoot.CurrentUser.Session.GetAsync(query.TargetUrl);

                        var threadKeyDic = HttpUtility.ParseQueryString(threadkey);

                        channelList.ThreadKey = threadKeyDic["threadkey"];
                        channelList.Force184  = threadKeyDic["force_184"];

                        var channelThread = new {
                            thread      = Owner.ApiData.Thread.ThreadIds.Community,
                            version     = "20090904",
                            language    = 0,
                            user_id     = int.Parse(Owner.ApiData.Viewer.Id),
                            force_184   = channelList.Force184,
                            with_global = 1,
                            scores      = 1,
                            nicoru      = 0,
                            threadkey   = channelList.ThreadKey
                        };

                        dic[PacketId] = CommentType.Channel;
                        jsonarray.Add(new { thread = channelThread });

                        //pf channel
                        jsonarray.Add(new { ping = new { content = "pf:" + PacketId++ % IdLimit } });

                        jsonarray.Add(new { ping = new { content = "ps:" + PacketId % IdLimit } });

                        var channelLeaves = new {
                            thread    = Owner.ApiData.Thread.ThreadIds.Community,
                            language  = 0,
                            user_id   = int.Parse(Owner.ApiData.Viewer.Id),
                            content   = "0-" + ((Owner.ApiData.Video.Duration / 60) + 1) + ":100," + GetReadCommentCount(Owner.ApiData.Video.Duration).ToString(),
                            scores    = 1,
                            nicoru    = 0,
                            force_184 = threadKeyDic["force_184"],
                            threadkey = threadKeyDic["threadkey"]
                        };

                        dic[PacketId] = CommentType.ChannelLeaves;
                        jsonarray.Add(new { thread_leaves = channelLeaves });


                        //pf channel
                        jsonarray.Add(new { ping = new { content = "pf:" + PacketId++ % IdLimit } });
                    }


                    //rf
                    jsonarray.Add(new { ping = new { content = "rf:" + RequestId++ % IdLimit } });

                    PacketCount += jsonarray.Count;
                }


                var request = new HttpRequestMessage(HttpMethod.Post, Owner.ApiData.Thread.ServerUrl)
                {
                    Content = new StringContent(DynamicJson.Serialize(jsonarray), Encoding.UTF8, "text/plain")
                };
                var a = await App.ViewModelRoot.CurrentUser.Session.GetAsync(request);

                //結果
                {
                    var currentType = CommentType.Normal;
                    foreach (var entry in DynamicJson.Parse(a))
                    {
                        //コメントタイプを確認
                        if (entry.ping())
                        {
                            var content = entry.ping.content as string;
                            if (content.Contains("ps"))
                            {
                                currentType = dic[long.Parse(content.Split(':')[1])];
                            }
                            continue;
                        }
                        if (entry.thread())
                        {
                            var thread = entry.thread;


                            switch (currentType)
                            {
                            case CommentType.NormalLeaves: {
                                normalList.ThreadId = thread.thread;
                                normalList.Ticket   = thread.ticket() ? thread.ticket : "";
                                normalList.LastRes  = thread.last_res() ? (int)thread.last_res : 0;
                                break;
                            }

                            case CommentType.Uploader: {
                                break;
                            }

                            case CommentType.Channel: {
                                break;
                            }

                            case CommentType.ChannelLeaves: {
                                channelList.ThreadId = thread.thread;
                                channelList.Ticket   = thread.ticket() ? thread.ticket : "";
                                channelList.LastRes  = thread.last_res() ? (int)thread.last_res : 0;
                                break;
                            }
                            }
                            continue;
                        }

                        if (entry.chat())
                        {
                            var chat = entry.chat;

                            switch (currentType)
                            {
                            case CommentType.NormalLeaves: {
                                if (chat.deleted())
                                {
                                    continue;
                                }

                                var item = new NicoNicoCommentEntry()
                                {
                                    Number    = (int)chat.no,
                                    Vpos      = (int)chat.vpos,
                                    PostedAt  = (long)chat.date,
                                    Anonymity = chat.anonymity(),
                                    UserId    = chat.user_id,
                                    Mail      = chat.mail() ? chat.mail : "",
                                    Content   = chat.content,
                                    Score     = chat.score() ? (int)chat.score : 0
                                };
                                item.DisassembleMail();
                                normalList.Add(item);
                                break;
                            }

                            case CommentType.Uploader: {
                                if (chat.deleted())
                                {
                                    continue;
                                }

                                var item = new NicoNicoCommentEntry()
                                {
                                    Number   = (int)chat.no,
                                    Vpos     = (int)chat.vpos,
                                    PostedAt = (long)chat.date,
                                    Content  = chat.content,
                                    Mail     = chat.mail() ? chat.mail : ""
                                };
                                item.Mail       = new string(item.Mail.Select(n => (ConvMap.ContainsKey(n) ? ConvMap[n] : n)).ToArray());
                                item.IsUploader = true;

                                item.DisassembleMail();
                                uploaderList.Add(item);
                                break;
                            }

                            case CommentType.Channel: {
                                break;
                            }

                            case CommentType.ChannelLeaves: {
                                if (chat.deleted())
                                {
                                    continue;
                                }

                                var item = new NicoNicoCommentEntry()
                                {
                                    Number    = (int)chat.no,
                                    Vpos      = (int)chat.vpos,
                                    PostedAt  = (long)chat.date,
                                    Anonymity = chat.anonymity(),
                                    UserId    = chat.user_id,
                                    Mail      = chat.mail() ? chat.mail : "",
                                    Content   = chat.content,
                                    Score     = chat.score() ? (int)chat.score : 0
                                };

                                item.DisassembleMail();
                                channelList.Add(item);

                                break;
                            }
                            }
                        }
                    }

                    if (!normalList.IsEmpty())
                    {
                        normalList.Sort();
                        ret.Add(normalList);
                    }
                    if (!communityList.IsEmpty())
                    {
                        communityList.Sort();
                        ret.Add(communityList);
                    }
                    if (!channelList.IsEmpty())
                    {
                        channelList.Sort();
                        ret.Add(channelList);
                    }
                    if (!officialList.IsEmpty())
                    {
                        officialList.Sort();
                        ret.Add(officialList);
                    }
                    if (!uploaderList.IsEmpty())
                    {
                        uploaderList.Sort();
                        ret.Add(uploaderList);
                    }
                }
                return(ret);
            } catch (RequestFailed) {
                Owner.Status = "コメントの取得に失敗しました";
                return(null);
            }
        }
Example #2
0
        public async Task <int> PostAsync(NicoNicoCommentList target, int vpos, string mail, string text)
        {
            try {
                var query = new GetRequestQuery(GetPostKeyApiUrl);
                query.AddQuery("thread", target.ThreadId);
                query.AddQuery("block_no", Math.Floor((decimal)(target.LastRes + 1) / 100).ToString());
                query.AddQuery("device", "1");
                query.AddQuery("version", "1");
                query.AddQuery("version_sub", "6");
                var postkey = await App.ViewModelRoot.CurrentUser.Session.GetAsync(query.TargetUrl);


                PacketId = PacketCount;

                var jsonarray = new List <object>();

                //rs
                jsonarray.Add(new { ping = new { content = "rs:" + RequestId % IdLimit } });
                //ps
                jsonarray.Add(new { ping = new { content = "ps:" + PacketId % IdLimit } });

                //コメント投稿
                var chat = new {
                    thread  = target.ThreadId,
                    vpos    = vpos,
                    mail    = mail,
                    ticket  = target.Ticket,
                    user_id = int.Parse(Owner.ApiData.Viewer.Id),
                    premium = Owner.ApiData.Viewer.IsPremium ? 1 : 0,
                    postkey = postkey,
                    content = text
                };

                jsonarray.Add(new { chat = chat });

                //pf
                jsonarray.Add(new { ping = new { content = "pf:" + PacketId++ % IdLimit } });
                //rf
                jsonarray.Add(new { ping = new { content = "rf:" + RequestId++ % IdLimit } });


                var request = new HttpRequestMessage(HttpMethod.Post, Owner.ApiData.Thread.ServerUrl)
                {
                    Content = new StringContent(DynamicJson.Serialize(jsonarray), Encoding.UTF8, "text/plain")
                };

                PacketCount += jsonarray.Count;

                var a = await App.ViewModelRoot.CurrentUser.Session.GetAsync(request);

                //成功したか確認
                foreach (var entry in DynamicJson.Parse(a))
                {
                    if (entry.chat_result())
                    {
                        if (entry.chat_result.status == 0D)
                        {
                            Owner.Status = "コメントを投稿しました";
                            return((int)entry.chat_result.no);
                        }
                        else
                        {
                            Owner.Status = "コメントの投稿に失敗しました";
                            return(-1);
                        }
                    }
                }
                return(-1);
            } catch (RequestFailed) {
                Owner.Status = "コメントの投稿に失敗しました";
                return(-1);
            }
        }