示例#1
0
        public async Task <Response <string> > BulkInsertHotNewsAsync([FromBody] IList <InsertHotNewsDto> dtos)
        {
            var response = new Response <string>();

            string spider = HttpContext.Request.Headers["spider"];

            if (spider != "python")
            {
                response.Msg = "缺少HEADERS值";
                return(response);
            }

            var hotNews = dtos.Select(x => new HotNews
            {
                Id       = Guid.NewGuid().GenerateNumber(),
                Title    = x.Title,
                Url      = x.Url,
                SourceId = x.SourceId,
                Date     = DateTime.Now
            });

            _context.HotNews.RemoveRange(_context.HotNews
                                         .Where(x => dtos.Select(z => z.SourceId)
                                                .Contains(x.SourceId)));
            await _context.SaveChangesAsync();

            await _context.HotNews.AddRangeAsync(hotNews);

            await _context.SaveChangesAsync();

            response.Result = "新增成功";
            return(response);
        }
        public async Task <Response <string> > BulkInsertWallpaperAsync([FromBody] InsertWallpaperInput input)
        {
            var response = new Response <string>();

            string spider = HttpContext.Request.Headers["spider"];

            if (spider != "python")
            {
                response.Msg = "缺少HEADERS值";
                return(response);
            }

            var wallpapers = input.Wallpapers.SelectToList(x => new Wallpaper
            {
                Id        = Guid.NewGuid().GenerateNumber(),
                Title     = x.Title,
                Url       = x.Url,
                Type      = (int)input.Type,
                Timestamp = x.Url.Split("/").Last().Split("_").First()
            });

            var urls = await _context.Wallpapers
                       .Where(x => x.Type == wallpapers.FirstOrDefault().Type)
                       .SelectToListAsync(x => x.Url);

            wallpapers = wallpapers.Where(x => !urls.Contains(x.Url)).ToList();

            await _context.Wallpapers.AddRangeAsync(wallpapers);

            await _context.SaveChangesAsync();

            response.Result = "新增成功";
            return(response);
        }
示例#3
0
        public async Task <Response <string> > InsertAlbumAsync([FromBody] AlbumDto dto)
        {
            var response = new Response <string>();

            var album = new Album
            {
                Id       = Extension.GenerateGuid(),
                Name     = dto.Name,
                ImgUrl   = dto.ImgUrl,
                Date     = DateTime.Now,
                IsPublic = dto.IsPublic,
                Password = dto.Password
            };
            await _context.Albums.AddAsync(album);

            await _context.SaveChangesAsync();

            response.Result = "新增成功";
            return(response);
        }
示例#4
0
        public async Task <Response <string> > InsertChickenSoupAsync([FromBody] IList <string> list)
        {
            var response = new Response <string>();

            if (!list.Any())
            {
                response.Msg = "鸡汤呢?";
                return(response);
            }

            var entities = list.Select(x => new ChickenSoup {
                Content = x
            });

            await _context.ChickenSoups.AddRangeAsync(entities);

            await _context.SaveChangesAsync();

            response.Result = $"新增成功,{entities.Count()}";
            return(response);
        }
示例#5
0
        public async Task <Response <string> > GenerateSignatureAsync(string name, int id)
        {
            var response = new Response <string>();

            if (name.Length > 4)
            {
                response.Msg = "名字只支持1-4个字符";
                return(response);
            }

            var type = Extension.EnumToList <SignatureEnum>().FirstOrDefault(x => x.Value.Equals(id))?.Description;

            if (string.IsNullOrEmpty(type))
            {
                response.Msg = "不存在的签名类型";
                return(response);
            }

            var signatureLog = await _context.SignatureLogs.FirstOrDefaultAsync(x => x.Name.Equals(name) && x.Type.Equals(type));

            if (null != signatureLog)
            {
                response.Result = signatureLog.Url;
                return(response);
            }

            var ip = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();

            if (string.IsNullOrEmpty(ip))
            {
                ip = Request.HttpContext.Connection.RemoteIpAddress.ToString();
            }

            var url = $"{(name + id).ToMd5()}.png";

            #region 生成签名

            var signatureUrl = AppSettings.Signature.Urls.OrderBy(x => Guid.NewGuid()).Select(x => new
            {
                Url       = x.Key,
                Parameter = string.Format(x.Value, name, id)
            }).FirstOrDefault();

            var content = new StringContent(signatureUrl.Parameter);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

            using var client = _httpClient.CreateClient();
            client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0");
            var httpResponse = await client.PostAsync(signatureUrl.Url, content);

            var result = await httpResponse.Content.ReadAsStringAsync();

            var regex  = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
            var imgUrl = regex.Match(result).Groups["imgUrl"].Value;

            // 生成签名路径
            var signaturePath = Path.Combine(AppSettings.Signature.Path, url);

            // 下载签名图片至本地
            await DownloadImg(client, signaturePath, imgUrl);

            // 添加水印并且保存图片
            await AddWatermarkSaveItAsync(signaturePath);

            #endregion

            var entity = new SignatureLog
            {
                Name = name,
                Type = type,
                Url  = url,
                Ip   = ip,
                Date = DateTime.Now
            };
            await _context.SignatureLogs.AddAsync(entity);

            await _context.SaveChangesAsync();

            response.Result = url;
            return(response);
        }