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); } }
public async Task <List <NicoNicoCommentList> > RefreshCommentAsync(List <NicoNicoCommentList> targetList) { var jsonarray = new List <object>(); PacketId = PacketCount; //rs jsonarray.Add(new { ping = new { content = "rs:" + RequestId % IdLimit } }); foreach (var target in targetList) { if (target.ListName == "通常コメント") { //ps jsonarray.Add(new { ping = new { content = "ps:" + PacketId % IdLimit } }); var normalThread = new { thread = Owner.ApiData.Thread.ThreadIds.Default, version = "20061206", language = 0, user_id = int.Parse(Owner.ApiData.Viewer.Id), res_from = target.LastRes + 1, with_global = 1, scores = 1, nicoru = 0, userkey = Owner.ApiData.Context.UserKey }; jsonarray.Add(new { thread = normalThread }); target.PacketId = PacketId; //pf jsonarray.Add(new { ping = new { content = "pf:" + PacketId++ % IdLimit } }); } else if (target.ListName == "チャンネルコメント") { //ps jsonarray.Add(new { ping = new { content = "ps:" + PacketId % IdLimit } }); var channelThread = new { thread = Owner.ApiData.Thread.ThreadIds.Community, version = "20061206", language = 0, user_id = int.Parse(Owner.ApiData.Viewer.Id), res_from = target.LastRes + 1, force_184 = target.Force184, with_global = 1, scores = 0, nicoru = 0, threadkey = target.ThreadKey }; jsonarray.Add(new { thread = channelThread }); target.PacketId = PacketId; //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") }; var a = await App.ViewModelRoot.CurrentUser.Session.GetAsync(request); long currentPacketId = 0; foreach (var entry in DynamicJson.Parse(a)) { //コメントタイプを確認 if (entry.ping()) { var content = entry.ping.content as string; if (content.Contains("ps")) { currentPacketId = long.Parse(content.Split(':')[1]); } continue; } foreach (var target in targetList) { if (target.PacketId == currentPacketId) { if (entry.thread()) { var thread = entry.thread; target.ThreadId = thread.thread; target.Ticket = thread.ticket() ? thread.ticket : ""; target.LastRes = thread.last_res() ? (int)thread.last_res : 0; continue; } if (entry.chat()) { var chat = entry.chat; if (target.ListName == "通常コメント") { 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(); target.Add(item); break; } if (target.ListName == "投稿者コメント") { 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(); target.Add(item); } if (target.ListName == "チャンネルコメント") { 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(); target.Add(item); } } } } } return(targetList); }