예제 #1
0
        public Blog LoadStar(string blogUrl,string imgPath)
        {
            Blog blog = new Blog();
            GC.Collect();
            ServicePointManager.DefaultConnectionLimit = 200;
            HttpWebRequest request = HttpWebRequestFactory.CreateSimpleRequest(blogUrl);
            WebProxy proxy = new WebProxy("127.0.0.1", 1080);
            request.Proxy = proxy;
            try
            {
                WebResponse response = request.GetResponse();
                Stream stream = response.GetResponseStream();
                string result = "";
                using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("utf-8")))
                {
                    result = reader.ReadToEnd();
                }
                var document = new JumonyParser().Parse(result);
                blog.Name = document.FindFirst(".ProfileHeaderCard-nameLink").InnerHtml();
                blog.Description = document.FindFirst(".ProfileHeaderCard-bio").InnerHtml();
                string imgUrl = document.FindFirst(".ProfileAvatar-image").Attribute("src").Value();

                request = HttpWebRequestFactory.CreateSimpleRequest(imgUrl);
                HttpWebResponse imageResponse = (HttpWebResponse)request.GetResponse(); //反馈请求
                Stream srr = imageResponse.GetResponseStream();
                string path = imgPath + blog.Name.ToString() + ".jpg";
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                //造一个字节类型的数组来存放图片
                byte[] buff = new byte[512];
                int c = 0;
                while ((c = srr.Read(buff, 0, buff.Length)) > 0)
                {
                    fs.Write(buff, 0, c);
                }
                srr.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
            return blog;
        }
예제 #2
0
        public void CreateStar(CreateUpdateStarInput input)
        {
            Logger.Info("添加明星的输入信息(CreateUpdateStarInput对象): " + input);

            var star = new Star
            {
                Name = input.Name,
                Nickname = input.Nickname,
                ChineseName = input.ChineseName,
                Gender = input.Gender,
                Description = input.Description,
                IsBlocked = false,//添加明星默认不屏蔽
                CreationTime = DateTime.Now,
                LastUpdateTime = DateTime.Now,

            };
            //添加该明星对应的首个博客(一个明星对应多个博客,在创建明星时只能添加一个博客,创建完之后才可以增加更多)
            _starRepository.Insert(star);
            _unitOfWorkManager.Current.SaveChanges();
            var blog = new Blog
            {
                Url = input.BlogUrl,
                CreationTime = DateTime.Now,
                LastUpdateTime = DateTime.Now,
                StarId = star.Id,
            };
            blog.ResolveUrl(input.BlogUrl);
            var sameBlog = _blogRepository.GetAll().Where(b => b.Name == blog.Name).Where(b => b.Provider == blog.Provider);
            if (sameBlog.Count() > 0)
            {
                //保证微博地址在数据库中的唯一性,爬虫就不会重复下载博文
                //抛出异常后上面_starRepository.Insert(star)的操作会被回滚
                throw new UserFriendlyException("你添加的微博" + input.BlogUrl + "已经存在,不能重复添加");

            }
            else
            {
                _blogRepository.Insert(blog);
            }
        }
예제 #3
0
 public void CreateUpdateBlog(CreateUpdateBlogInput input)
 {
     //System.Web.HttpContext.Current.Server.MapPath(
     var star = _starRepository.Get(input.starId);
     if (star == null)
     {
         throw new UserFriendlyException("错误的明星ID!");
     }
     if(input.BlogUrls.Count()<3 || input.BlogIds.Count()<3)
         throw new UserFriendlyException("地址信息至少要有3条!");
     for (int i = 0; i < 3; i++)
     {
         var url = input.BlogUrls.ElementAt(i);
         var id = input.BlogIds.ElementAt(i);
         if (string.IsNullOrEmpty(url))
         {
             if (id != -1)
             {
                 //把已有的URL置为空字符串,这意味着管理者想删除该微博信息
                 _blogRepository.Delete(id);
             }
         }
         else
         {
             var blog = new Blog { Url = url };
             blog.ResolveUrl(url);
             if (!string.IsNullOrEmpty(blog.Name) && blog.Provider != BlogProvider.UNDEFINED)
             {
                 var sameBlog = _blogRepository.GetAll().Where(b => b.StarId != input.starId && b.Name == blog.Name && b.Provider == blog.Provider);
                 if (sameBlog.Count() > 0)
                 {
                     throw new UserFriendlyException("你添加的微博" + url + "已经存在,不能重复添加");
                 }
             }
             if (id != -1)
             {
                 //编辑微博
                 var existBlog = _blogRepository.Get(id);
                 if (existBlog == null)
                 {
                     throw new UserFriendlyException("错误的微博ID!");
                 }
                 existBlog.LastUpdateTime = DateTime.Now;
                 existBlog.Url = blog.Url;
                 existBlog.Name = blog.Name;
                 existBlog.Provider = blog.Provider;
                 existBlog.ProviderName = blog.ProviderName;
                 _blogRepository.Update(existBlog);
             }
             else
             {
                 //添加微博
                 blog.CreationTime = DateTime.Now;
                 blog.LastUpdateTime = DateTime.Now;
                 blog.StarId = input.starId;
                 blog.Description = "";
                 _blogRepository.Insert(blog);
             }
         }
     }
 }