Beispiel #1
0
        static async Task DownloadImages()
        {
            Func <DownloadModel, Task <string> > asyncTask = async model =>
            {
                using (WebClient webClient = new WebClient())
                {
                    Console.WriteLine($"下载文件{model.Url}");
                    string fileName = Guid.NewGuid().ToString().Replace("-", "") + ".jpg";
                    string path     = Path.Combine(model.alias, fileName);
                    try
                    {
                        string savePath = Path.Combine(BasePath, model.alias);
                        if (!Directory.Exists(savePath))
                        {
                            Directory.CreateDirectory(savePath);
                        }
                        await webClient.DownloadFileTaskAsync(model.Url, Path.Combine(savePath, fileName));

                        Console.WriteLine($"下载完成,输出路径{path}");
                        return(path);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"下载出错:{e.Message}");
                        return("");
                    }
                }
            };
            Func <DownloadModel, Task> saveRecordAsync = async model =>
            {
                Console.WriteLine("准备保存地址到数据库");
                using (SpiderContext db = new SpiderContext())
                {
                    var article = await db.WeChatArticles.FirstOrDefaultAsync(c => c.Id == model.Id);

                    if (null != article)
                    {
                        article.Download  = true;
                        article.LocalPath = model.Path;
                        await db.SaveChangesAsync();
                    }
                }
                Console.WriteLine("保存完成");
            };

            foreach (var item in downloadModels)
            {
                item.Path = await asyncTask(item);

                if (!string.IsNullOrWhiteSpace(item.Path))
                {
                    await saveRecordAsync(item);
                }

                Console.WriteLine($"在此休眠{SleepTime / 1000}秒");
                Thread.Sleep(SleepTime);
            }
        }
Beispiel #2
0
 public async Task InsertClientSgin(long clientId)
 {
     using var db = new SpiderContext();
     db.TaskStartSigns.Add(new TaskStartSign
     {
         Id        = IdWorkContext.ID_WORKER.NextId(),
         ClientId  = clientId,
         StartDate = DateTime.Now,
         RunStatus = true
     });
     await db.SaveChangesAsync();
 }
Beispiel #3
0
        public async Task <int> SetClientSignOut(long clientId)
        {
            using var spiderContext = new SpiderContext();
            var sign = await spiderContext.TaskStartSigns.FirstOrDefaultAsync(c => c.ClientId == clientId);

            if (null == sign)
            {
                throw new NullReferenceException("未找到实体");
            }
            sign.RunStatus = false;
            return(await spiderContext.SaveChangesAsync());
        }
Beispiel #4
0
        public async Task <int> SetAccountSpiderStop(long accountId)
        {
            using var spiderContext = new SpiderContext();
            var account = await spiderContext.WeChatAccounts.FirstOrDefaultAsync(c => c.Id == accountId);

            if (null == account)
            {
                throw new NullReferenceException("未找到公众号实体");
            }
            account.TaskStartSign = null;
            return(await spiderContext.SaveChangesAsync());
        }
Beispiel #5
0
        public async Task UpdateClientSgin(long clientId)
        {
            using var spiderContext = new SpiderContext();
            var sign = await spiderContext.TaskStartSigns.FirstOrDefaultAsync(c => c.ClientId == clientId);

            if (null != sign)
            {
                sign.StartDate = DateTime.Now;
                await spiderContext.SaveChangesAsync();
            }
            else
            {
                throw new NullReferenceException("未找到实体");
            }
        }
Beispiel #6
0
        public async Task <int> InsertArticleByList(List <WeChatArticle> list, long accountId)
        {
            using var db = new SpiderContext();
            var account = await db.WeChatAccounts.FirstOrDefaultAsync(c => c.Id == accountId);

            if (null == account)
            {
                throw new NullReferenceException("未找到公众号实体");
            }
            list.ForEach(item =>
            {
                item.WeChatAccount = account;
                db.WeChatArticles.Add(item);
            });
            return(await db.SaveChangesAsync());
        }
Beispiel #7
0
 public async Task <int> UpdateWeChatAccountAsync(WeChatAccount model)
 {
     using var spiderContext = new SpiderContext();
     spiderContext.WeChatAccounts.Update(model);
     return(await spiderContext.SaveChangesAsync());
 }