示例#1
0
        internal async Task Run()
        {
            _logger.LogInformation(DateTime.Now.ToString() + " - Start Job - ");

            var records = new List <Dictionary <string, string> >();
            var config  = new Dictionary <string, string>();

            #region load data
            using (var jsonop = new JsonOP()) {
                _logger.LogInformation("Loading previous records");
                try {
                    records = jsonop.LoadData(recordPath);
                } catch (Exception ex) {
                    _logger.LogError("Error loading previous records!");
                    _logger.LogError("Error message: {0}", ex.Message);
                }
                _logger.LogInformation("Done");

                _logger.LogInformation("Loading configs");
                try {
                    config = jsonop.LoadConfig(configPath);
                } catch (Exception ex) {
                    _logger.LogError("Error loading configs!");
                    _logger.LogError("Error message: {0}", ex.Message);
                }
                _logger.LogInformation("Done");
            }
            #endregion

            _logger.LogInformation("Getting page source");
            var webGet  = new HtmlWeb();
            var htmlDoc = webGet.Load(URL);
            _logger.LogInformation("Done");

            _logger.LogInformation("Start processing data");
            await ProcessingData(htmlDoc : htmlDoc, records : records, chatId : config["CHAT_ID"], token : config["TOKEN"]);

            _logger.LogInformation("Done");

            _logger.LogInformation(DateTime.Now.ToString() + " - End Job - ");
        }
示例#2
0
        internal async Task ProcessingData(HtmlDocument htmlDoc, List <Dictionary <string, string> > records, string chatId, string token)
        {
            var pushList   = new List <string>();
            var recordList = new List <Dictionary <string, string> >();

            var articles = htmlDoc.DocumentNode.CssSelect("div.post-column article header h2 a");

            foreach (var each in articles)
            {
                #region get article titles and links
                var title = each.InnerText.ToString();
                var link  = each.Attributes["href"].Value.ToString();

                _logger.LogInformation("Found new info: {0}", title);
                #endregion

                #region save titles and links to List
                var tmp = new Dictionary <string, string> {
                    { "title", title },
                    { "url", link }
                };
                recordList.Add(tmp);
                #endregion

                #region decide if push or not
                bool isPush = true;
                foreach (var i in records)
                {
                    if (i["title"] == title && i["url"] == link)
                    {
                        isPush = false;
                    }
                }
                #endregion

                #region push code
                if (isPush)
                {
                    _logger.LogInformation("Add {0} to push list", title);

                    //try to get links
                    var possibleLinks = TryGetLinks(url: link);

                    var tmpstr = "<b>EpicBundle 信息</b>\n\n";
                    tmpstr += "<i>" + title + "</i>\n";
                    tmpstr += "文章链接: " + link + "\n";
                    tmpstr += "可能的领取链接:\n";

                    foreach (var i in possibleLinks)
                    {
                        tmpstr += i + "\n";
                    }

                    pushList.Add(tmpstr);
                }
                #endregion
            }

            #region send notifications
            _logger.LogInformation("Sending notifications");
            await SendNotification(id : chatId, token : token, msgs : pushList);

            _logger.LogInformation("Done");
            #endregion

            #region writing records
            _logger.LogInformation("Writing records");
            using var jsonop = new JsonOP();
            try {
                jsonop.WriteData(data: recordList, path: recordPath);
            } catch (Exception ex) {
                _logger.LogError("Writing records failed!");
                _logger.LogError("Error message: {0}", ex.Message);
            }
            #endregion
        }