コード例 #1
0
ファイル: Models.cs プロジェクト: xingx001/wechat_spider_core
        public WeChatArticle ConvertModel()
        {
            WeChatArticle result  = new WeChatArticle();
            Type          type    = result.GetType();
            DateTime      dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));

            foreach (var item in typeof(ArticleModel).GetProperties())
            {
                foreach (var dbmodel in typeof(WeChatArticle).GetProperties())
                {
                    if (item.Name == dbmodel.Name && item.Name != "tagid")
                    {
                        if (item.Name == "update_time" || item.Name == "create_time")
                        {
                            int.TryParse(item.GetValue(this).ToString(), out int time);
                            TimeSpan toNow    = new TimeSpan(long.Parse(time.ToString() + "0000000"));
                            DateTime targetDt = dtStart.Add(toNow);
                            type.GetProperty(item.Name).SetValue(result, targetDt);
                            continue;
                        }
                        type.GetProperty(item.Name).SetValue(result, item.GetValue(this));
                    }
                }
            }
            return(result);
        }
コード例 #2
0
        private static void TestCommon()
        {
            //初始化被观察者(微信公众号)
            TengXun wechatArticle = new WeChatArticle();
            //初始化被观察者(和平精英)
            TengXun game = new PeaceElite();

            //给微信公众号添加粉丝(观察者)
            wechatArticle.Add(new WeChatArticleFans("微信公众号粉丝:张三"));
            wechatArticle.Add(new WeChatArticleFans("微信公众号粉丝:李四"));
            wechatArticle.Add(new WeChatArticleFans("微信公众号粉丝:王五"));
            wechatArticle.Add(new WeChatArticleFans("微信公众号粉丝:赵柳"));
            Console.WriteLine("----------------------------------------------------------");
            //给和平精英添加玩家(观察者)
            game.Add(new TengXunGamer("和平精英玩家:张无忌"));
            game.Add(new TengXunGamer("和平精英玩家:秋刀"));
            game.Add(new TengXunGamer("和平精英玩家:琅琊榜"));
            game.Add(new TengXunGamer("和平精英玩家:金箍棒"));
            game.Add(new TengXunGamer("和平精英玩家:紧箍咒"));
            Console.WriteLine("----------------------------------------------------------");
            //微信公众号发通知(发表新文章)
            wechatArticle.SendMessage("一篇新文章", "来学习设计模式至观察者模式吧");
            Console.WriteLine("----------------------------------------------------------");
            //和平精英发通知(新动态)
            game.SendMessage("和平精英更新啦", "新玩法:火力模式,精英们快来保卫和平吧!");
            Console.WriteLine("----------------------------------------------------------");
            //取消关注
            game.Remove(new TengXunGamer("和平精英玩家:张无忌"));
            Console.WriteLine("----------------------------------------------------------");
            //再发送新的通知,取消关注的人就收不到推送了
            game.SendMessage("(和平精英)您有一条新消息", "万圣节,大家放开嗨");
            Console.WriteLine("----------------------------------------------------------");
        }
コード例 #3
0
        /// <summary>
        /// 获取历史文章列表回调
        /// </summary>
        /// <param name="articleResponseModel"></param>
        private async void ArticleResponseHandlerCallback(ArticleResponseModel articleResponseModel)
        {
            LogService.Info($"当前条数:{articleResponseModel.app_msg_list.Count},准备写入到数据库");
            int oldArticleCount = articleResponseModel.app_msg_list.Count(a => a.create_time < LastStartTimeSpan);

            if (oldArticleCount < articleResponseModel.app_msg_list.Count)
            {
                List <WeChatArticle> saveList = new List <WeChatArticle>();
                articleResponseModel.app_msg_list.ForEach(async item =>
                {
                    var article = await spiderHandler.QueryArticleByAid(item.aid);
                    if (article.Count >= 1 || item.create_time < LastStartTimeSpan)
                    {
                        LogService.Info($"文章({item.aid})已经存在,跳过保存");
                        return;
                    }
                    WeChatArticle model = item.ConvertModel();
                    model.Id            = IdWorkContext.ID_WORKER.NextId();
                    model.CreateDate    = DateTime.Now;
                    saveList.Add(model);
                });
                if (saveList.Count == 0)
                {
                    await NextTask();
                }
                else
                {
                    try
                    {
                        await spiderHandler.InsertArticleByList(saveList, CurrentTask.SpiderId);
                    }
                    catch (Exception e)
                    {
                        LogService.Error($"将文章写入到数据库出错{e.Message}", e);
                    }

                    LogService.Info("准备翻页,休眠30秒");
                    Thread.Sleep(1000 * 30);
                    CurrentTask.ToQueryArticleList(++CurrentTask.CurrentPage);
                }
            }
            else
            {
                await NextTask();
            }
        }