Пример #1
0
        public async Task <HttpResponseMessage> RssImport([FromBody] RssImportModel rss)
        {
            var profile = GetProfile();

            rss.ProfileId = profile.Id;
            rss.Root      = Url.Content("~/");

            return(await _rss.Import(rss));
        }
Пример #2
0
        public void CanParseRssFeed(string feed)
        {
            using (var context = new BlogifierDbContext(_options))
            {
                var storage = new BlogStorage("test");
                var path = Path.Combine(GetRoot(), feed);

                var uow = new UnitOfWork(context);

                var profile = uow.Profiles.Single(p => p.IdentityName == "test");

                if(profile == null)
                {
                    profile = new Profile();
                    profile.IdentityName = "test";
                    profile.AuthorName = "test";
                    profile.AuthorEmail = "*****@*****.**";
                    profile.Title = "test";
                    profile.Slug = "test";
                    profile.Description = "the test";
                    profile.BlogTheme = "Standard";

                    uow.Profiles.Add(profile);
                    uow.Complete();
                }

                Assert.True(context.Profiles.ToList().Count > 0);

                var service = new RssService(uow, null);

                var model = new RssImportModel();
                model.FeedUrl = path;
                model.ProfileId = profile.Id;

                var result = service.Import(model);

                Assert.True(context.BlogPosts.ToList().Count > 1);
            }

            Assert.NotNull(feed);
        }
Пример #3
0
        public async Task <HttpResponseMessage> Import(RssImportModel model)
        {
            var response = new HttpResponseMessage(HttpStatusCode.OK);

            _model = model;

            if (model == null || string.IsNullOrEmpty(model.FeedUrl))
            {
                response.StatusCode   = HttpStatusCode.NotFound;
                response.ReasonPhrase = "RSS feed URL is required";
                return(response);
            }

            var blog = _db.Profiles.Single(b => b.Id == model.ProfileId);

            if (blog == null)
            {
                response.StatusCode   = HttpStatusCode.NotFound;
                response.ReasonPhrase = Constants.ProfileNotFound;
                return(response);
            }

            try
            {
                var storage = new BlogStorage(blog.Slug);
                var items   = GetFeedItems(model.FeedUrl);

                _logger.LogInformation(string.Format("Start importing {0} posts", items.Count));

                foreach (var item in items)
                {
                    var content = item.Body.Length > item.Description.Length ? item.Body : item.Description;

                    var desc = content.StripHtml();
                    if (desc.Length > 300)
                    {
                        desc = desc.Substring(0, 300);
                    }

                    var post = new BlogPost
                    {
                        ProfileId   = model.ProfileId,
                        Title       = item.Title,
                        Slug        = item.Title.ToSlug(),
                        Description = desc,
                        Content     = content,
                        Published   = item.PublishDate
                    };
                    if (model.ImportImages)
                    {
                        await ImportImages(post, storage);
                    }
                    if (model.ImportAttachements)
                    {
                        await ImportAttachements(post, storage);
                    }
                    _db.BlogPosts.Add(post);
                    _db.Complete();
                    _logger.LogInformation(string.Format("RSS item added : {0}", item.Title));

                    await AddCategories(item, model.ProfileId);
                }
                response.ReasonPhrase = string.Format("Imported {0} blog posts", items.Count);
                return(response);
            }
            catch (Exception ex)
            {
                _logger.LogError(string.Format("Error importing RSS : {0}", ex.Message));

                response.StatusCode   = HttpStatusCode.BadRequest;
                response.ReasonPhrase = ex.Message;
                return(response);
            }
        }