public async Task GetComment([FromServices] Func <string, IVideoDetailService> func, string Id) { HttpResponse response = Response; response.Headers.Add("Content-Type", "text/event-stream"); response.StatusCode = 200; IEnumerable <VideoDetailModel> lstVideoDetail = null; string body = String.Empty; if (String.IsNullOrEmpty(Id)) { response.BadRequest("'id' is not valid."); } else { _videoDetailService = func(Id); try { while (_isLiving && Response.HttpContext.RequestAborted.IsCancellationRequested == false) { lstVideoDetail = await _videoDetailService.FindAsync(); if (lstVideoDetail != null && lstVideoDetail.Count() > 0) { lstVideoDetail = lstVideoDetail.OrderBy(i => i.FbName); } body = JsonConvert.SerializeObject(lstVideoDetail); await response.WriteAsync("data:" + JsonConvert.SerializeObject(new ResponseModel(200, body)) + "\r\r"); await response.Body.FlushAsync(); Thread.Sleep(1000); var objPage = _pageService.FindAsync(() => Builders <PageModel> .Filter.Eq("Videos.VideoId", Id.Trim())).Result.FirstOrDefault(); if (objPage == null) { _isLiving = false; } else { foreach (var video in objPage.Videos) { if (video.VideoId.Equals(Id) && video.Status.Equals("LIVE") == false) { _isLiving = false; break; } } } } } catch (Exception ex) { response.InternalServer(ex.Message); response.Body.Close(); } response.Body.Close(); } }
public async Task <IActionResult> ExportExcelData([FromServices] Func <string, IVideoDetailService> func, string Id) { if (String.IsNullOrEmpty(Id)) { return(BadRequest()); } try { _videoDetailService = func(Id); var lstComment = await _videoDetailService.FindAsync(); if (lstComment != null && lstComment.Count() > 0) { lstComment = lstComment.OrderBy(i => i.FbName); } using (var workbook = new XLWorkbook()) { var worksheet = workbook.Worksheets.Add("Bình luận"); worksheet.Columns().AdjustToContents(); var currentRow = 1; worksheet.Cell(currentRow, 1).Value = "Facebook ID"; worksheet.Cell(currentRow, 2).Value = "Tên Facebook"; worksheet.Cell(currentRow, 3).Value = "Số điện thoại"; worksheet.Cell(currentRow, 4).Value = "Bình luận"; worksheet.Cell(currentRow, 5).Value = "Trạng thái"; worksheet.Cell(currentRow, 6).Value = "Ngày tạo"; worksheet.Range(worksheet.Cell(currentRow, 1), worksheet.Cell(currentRow, 6)).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center); worksheet.Range(worksheet.Cell(currentRow, 1), worksheet.Cell(currentRow, 6)).Style.Font.SetBold(true); worksheet.Range(worksheet.Cell(currentRow, 1), worksheet.Cell(currentRow, 6)).Style.Fill.SetBackgroundColor(XLColor.AshGrey); worksheet.Range(worksheet.Cell(currentRow, 1), worksheet.Cell(currentRow, 6)).SetAutoFilter(true); worksheet.Columns("A", "F").AdjustToContents(); for (int i = 0; i < lstComment.Count(); i++) { var comment = lstComment.ElementAt(i); currentRow = currentRow + 1; //Facebook Id worksheet.Cell(currentRow, 1).SetValue <string>(comment.FbId.ToString()); worksheet.Range(worksheet.Cell(currentRow, 1), worksheet.Cell(currentRow + comment.Comments.Count - 1, 1)).Merge(); worksheet.Cell(currentRow, 1).Style.Alignment.SetVertical(XLAlignmentVerticalValues.Center); //Tên Facebook worksheet.Cell(currentRow, 2).Value = comment.FbName; worksheet.Range(worksheet.Cell(currentRow, 2), worksheet.Cell(currentRow + comment.Comments.Count - 1, 2)).Merge(); worksheet.Cell(currentRow, 2).Style.Alignment.SetVertical(XLAlignmentVerticalValues.Center); if (i % 2 == 0) { worksheet.Range(worksheet.Cell(currentRow, 1), worksheet.Cell(currentRow + comment.Comments.Count - 1, 6)).Style.Fill.SetBackgroundColor(XLColor.LightGray); } worksheet.Range(worksheet.Cell(currentRow, 1), worksheet.Cell(currentRow + comment.Comments.Count - 1, 6)).Style.Border.TopBorder = XLBorderStyleValues.Thin; worksheet.Range(worksheet.Cell(currentRow, 1), worksheet.Cell(currentRow + comment.Comments.Count - 1, 6)).Style.Border.RightBorder = XLBorderStyleValues.Thin; worksheet.Range(worksheet.Cell(currentRow, 1), worksheet.Cell(currentRow + comment.Comments.Count - 1, 6)).Style.Border.BottomBorder = XLBorderStyleValues.Thin; worksheet.Range(worksheet.Cell(currentRow, 1), worksheet.Cell(currentRow + comment.Comments.Count - 1, 6)).Style.Border.LeftBorder = XLBorderStyleValues.Thin; worksheet.Range(worksheet.Cell(currentRow, 1), worksheet.Cell(currentRow + comment.Comments.Count - 1, 6)).Style.Border.InsideBorder = XLBorderStyleValues.Thin; //Số điện thoại for (int j = 0; j < comment.Phones.Count; j++) { worksheet.Cell(currentRow + j, 3).SetValue <string>(comment.Phones[j]); } ; for (int j = 0; j < comment.Comments.Count; j++) { //Bình luận worksheet.Cell(currentRow, 4).SetValue <string>(comment.Comments[j].Message); if (comment.Comments[j].IsDeleteWhenDetectedBadWordResult) { //Trạng thái worksheet.Cell(currentRow, 5).Value = "Đã xóa"; } else if (comment.Comments[j].IsHiddenResult) { //Trạng thái worksheet.Cell(currentRow, 5).Value = "Đã ẩn"; } else if (comment.Comments[j].IsSendWhenDetectedPhoneResult || comment.Comments[j].IsSendWhenDetectedGoodWordResult) { //Trạng thái worksheet.Cell(currentRow, 5).Value = "Đã gửi"; } worksheet.Cell(currentRow, 5).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center); //Ngày tạo worksheet.Cell(currentRow, 6).DataType = XLDataType.DateTime; worksheet.Cell(currentRow, 6).SetValue <DateTime>(comment.Comments[j].CreatedTime.ToLocalTime()); currentRow = currentRow + 1; } ; } using (var stream = new MemoryStream()) { workbook.SaveAs(stream); var content = stream.ToArray(); return(File( content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", String.Format("binh_luan_{0}.xlsx", Id))); } } } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public async Task StreamingPipelineAsync(IVideoDetailService videoDetailService, string Id, string Token) { try { await _facebookApi.VideoStreamingAsync(Id, Token, async (body) => { PageInfoResponse objPageInfo = await _facebookApi.GetPageInfoAsync(Token); var lstCategory = new List <CategoryModel>(); foreach (var catergory in objPageInfo.Categories) { lstCategory.Add(new CategoryModel() { CategoryId = catergory.CategoryId, CategoryName = catergory.CategoryName }); } PageModel model = new PageModel() { PageId = objPageInfo.PageId, PageName = objPageInfo.PageName, Categories = lstCategory, About = objPageInfo.About, Address = objPageInfo.Address, Link = objPageInfo.Link, Emails = objPageInfo.Emails, Website = objPageInfo.Website, FanCount = objPageInfo.FanCount, CheckinCount = objPageInfo.CheckinCount, CanCheckin = objPageInfo.CanCheckin, NewLikeCount = objPageInfo.NewLikeCount, CanPost = objPageInfo.CanPost, Videos = new List <VideoModel>() }; var pagesModel = _pageRepository.FindAsync(() => Builders <PageModel> .Filter.Eq("PageId", objPageInfo.PageId)).Result.FirstOrDefault(); if (pagesModel == null) { await _pageRepository.InsertOneAsync(model); } List <string> lstPhone = new List <string>(); ReplyTemplateModel replyTemplate = new ReplyTemplateModel(); bool IsSendWhenDetectedPhone = false; bool IsSendWhenDetectedGoodWord = false; bool IsDeleteWhenDetectedBadWord = false; bool IsHidden = false; bool IsHiddenWhenNegSentiment = false; bool IsSendWhenDetectedPhoneResult = false; bool IsSendWhenDetectedGoodWordResult = false; bool IsDeleteWhenDetectedBadWordResult = false; bool IsHiddenResult = false; Match objBadWord = null; Match objGoodWord = null; VideoDetailModel videoModel = null; List <string> lstBadWord = null; List <string> lstGoodWord = null; if (pagesModel.Videos != null && pagesModel.Videos.Count > 0) { foreach (var video in pagesModel.Videos) { if (video.VideoId.Equals(Id)) { lstBadWord = video.BadWords; lstGoodWord = video.GoodWords; IsSendWhenDetectedPhone = video.IsSendWhenDetectedPhone; IsSendWhenDetectedGoodWord = video.IsSendWhenDetectedGoodWord; IsDeleteWhenDetectedBadWord = video.IsDeleteWhenDetectedBadWord; IsHidden = video.IsHidden; IsHiddenWhenNegSentiment = video.IsHiddenWhenNegSentiment; replyTemplate = video.ReplyTemplate; replyTemplate.Recipient.CommentId = body.Id; break; } } } //1. Detect phone number. foreach (Match phone in Regex.Matches(body.Message.Trim(), @"0\d{9}")) { lstPhone.Add(phone.Value); } if (lstPhone != null && lstPhone.Count > 0) { if (IsSendWhenDetectedPhone == true) { IsSendWhenDetectedPhoneResult = await _facebookApi.SendPrivateReplyAsync(replyTemplate.ToModelReq(), Token); } if (IsHidden == true && IsHiddenResult == false) { //IsHiddenResult = await _facebookApi.HiddenCommentAsync(body.Id, Token, true); IsHiddenResult = await _facebookApi.DeleteCommentAsync(body.Id, Token); } } //2. Sentiment NLP if (IsHiddenWhenNegSentiment && IsHiddenResult == false) { if (_sentimentNLPApi.SentimentNLP(body.Message.Trim()).Result) { //IsHiddenResult = await _facebookApi.HiddenCommentAsync(body.Id, Token, true); IsHiddenResult = await _facebookApi.DeleteCommentAsync(body.Id, Token); } } //3. Detect bad word and delete comment. if (lstBadWord != null) { if (IsDeleteWhenDetectedBadWord == true) { foreach (string badWord in lstBadWord) { objBadWord = Regex.Match(body.Message.Trim().ToLower(), badWord.Trim().ToLower()); if (objBadWord.Success) { IsDeleteWhenDetectedBadWordResult = await _facebookApi.DeleteCommentAsync(body.Id, Token); } } } } //4. Detect good word and send private reply if (lstGoodWord != null) { if (IsSendWhenDetectedGoodWord == true) { foreach (string goodWord in lstGoodWord) { objGoodWord = Regex.Match(body.Message.Trim().ToLower(), goodWord.Trim().ToLower()); if (objGoodWord.Success) { IsSendWhenDetectedGoodWordResult = await _facebookApi.SendPrivateReplyAsync(replyTemplate.ToModelReq(), Token); } } } } //5. Insert or Update new document. videoModel = videoDetailService.FindAsync(() => Builders <VideoDetailModel> .Filter.Eq("FbId", body.From.Id)).Result.FirstOrDefault(); var commentModel = new CommentModel() { CommentId = body.Id, Message = body.Message, IsSendWhenDetectedPhoneResult = IsSendWhenDetectedPhoneResult, IsSendWhenDetectedGoodWordResult = IsSendWhenDetectedGoodWordResult, IsDeleteWhenDetectedBadWordResult = IsDeleteWhenDetectedBadWordResult, IsHiddenResult = IsHiddenResult, CreatedTime = body.CreatedTime }; if (videoModel == null) { videoModel = new VideoDetailModel(); videoModel.FbId = body.From.Id; videoModel.FbName = body.From.Name; videoModel.Phones = lstPhone; videoModel.Comments.Add(commentModel); await videoDetailService.InsertOneAsync(videoModel); } else { if (lstPhone != null && lstPhone.Count > 0) { foreach (string phone in lstPhone) { videoModel.Phones.Add(phone); } } videoModel.Comments.Add(commentModel); await videoDetailService.UpdateOneAsync(() => Builders <VideoDetailModel> .Filter.Eq("FbId", body.From.Id), () => Builders <VideoDetailModel> .Update.Set("Comments", videoModel.Comments).Set("Phones", videoModel.Phones)); } }); } catch (HttpRequestException ex) { throw ex; } catch (Exception ex) { throw ex; } }