Esempio n. 1
0
        public IActionResult CreateOrUpdate([FromBody] CmsContent data)
        {
            if (string.IsNullOrEmpty(data.Title))
            {
                return(Json(new JsonResponse <string> {
                    code = 1, message = "Title can not be empty"
                }));
            }
            if (data.Id == null || data.Id == Guid.Empty)
            {
                data.Id = Guid.NewGuid();
            }

            data = data.CalculateThumbnail();

            using (var db = new MoneyNoteDbContext())
            {
                var exited = db.CmsContents.FirstOrDefault(i => i.Id == data.Id);
                if (exited == null)
                {
                    data.CountView = 0;
                    db.CmsContents.Add(data);
                }
                else
                {
                    exited.ParentId        = data.ParentId;
                    exited.Title           = data.Title;
                    exited.Thumbnail       = data.Thumbnail;
                    exited.UrlRef          = data.UrlRef;
                    exited.Description     = data.Description;
                    exited.IsDeleted       = data.IsDeleted;
                    exited.ThumbnailWidth  = data.ThumbnailWidth;
                    exited.ThumbnailHeight = data.ThumbnailHeight;
                    exited.VideoWidth      = data.VideoWidth;
                    exited.VideoHeight     = data.VideoHeight;
                    exited.IsPublished     = data.IsPublished;
                }

                db.SaveChanges();

                var existed = db.CmsRelations.Where(i => i.ContentId == data.Id).ToList();
                db.RemoveRange(existed);
                db.SaveChanges();

                db.AddRange(data.CategoryIds.Select(c => new CmsRelation
                {
                    ContentId  = data.Id,
                    CategoryId = c
                }).ToList());
                db.SaveChanges();
            }

            return(Json(new JsonResponse <CmsContent> {
                data = data
            }));
        }
        public CmsContent CrawlVideo(string url, out Exception ex)
        {
            ex = null;
            try
            {
                using (var httpClient = new HttpClient())
                {
                    httpClient.BaseAddress = new Uri("https://www.youtube.com/");
                    string result;

                    result = httpClient.GetStringAsync(url).GetAwaiter().GetResult();

                    int.TryParse(FindYoutubeContent(result, "<meta property=\"og:image:width\" content=\"", "\">"), out int tw);
                    int.TryParse(FindYoutubeContent(result, "<meta property=\"og:image:height\" content=\"", "\">"), out int th);
                    int.TryParse(FindYoutubeContent(result, "<meta property=\"og:video:width\" content=\"", "\">"), out int vw);
                    int.TryParse(FindYoutubeContent(result, "<meta property=\"og:video:height\" content=\"", "\">"), out int vh);

                    CmsContent cmsContent = new CmsContent
                    {
                        Title           = FindYoutubeContent(result, "<meta property=\"og:title\" content=\"", "\">"),
                        Thumbnail       = FindYoutubeContent(result, "<meta property=\"og:image\" content=\"", "\">"),
                        UrlRef          = FindYoutubeContent(result, "<meta property=\"og:url\" content=\"", "\">"),
                        Description     = FindYoutubeContent(result, "\\\"description\\\":{\\\"simpleText\\\":\\\"", "\\\"}"),
                        ThumbnailHeight = th,
                        ThumbnailWidth  = tw,
                        VideoHeight     = vh,
                        VideoWidth      = vw,
                    };

                    cmsContent = cmsContent.CalculateThumbnail();

                    return(cmsContent);
                }
            }
            catch (Exception ex1)
            {
                ex = ex1;
                return(null);
            }
        }