<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HtmlAgilityPack;
using log4net;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using WeGouSharp.Model;

namespace WeGouSharp.Core
{
    /// <inheritdoc />
    internal class WechatSogouApi : WechatSogouBasic
    {
        readonly ILog _logger;
        private readonly Browser _browser;
        private IConfiguration _configuration;

        public WechatSogouApi(ILog logger, Browser browser, IConfiguration configuration) : base(logger, browser,
            configuration)
        {
            _logger = logger;
            _browser = browser;
            _configuration = configuration;
        }

        /// <summary>
        /// 搜索公众号
        /// </summary>
        /// <param name="keyword">搜索关键字</param>
        /// <param name="page">第几页</param>
        /// <returns></returns>
        public async Task<List<OfficialAccount>> SearchOfficialAccountAsync(string keyword, int page = 1)
        {
            var accountList = new List<OfficialAccount>();

            //var text = this._SearchAccount_Html(keyword, page);
            var text = await SearchAccountHtmlAsync(keyword, page);
            var pageDoc = new HtmlDocument();
            pageDoc.LoadHtml(text);
            var targetArea = pageDoc.DocumentNode.SelectNodes("//ul[@class='news-list2']/li");
            if (targetArea == null) return null;
            foreach (var node in targetArea)
            {
                var accountInfo = new OfficialAccount();
                try
                {
                    //链接中包含了&amp; html编码符，要用htmdecode，不是urldecode
                    accountInfo.AccountPageurl =
                        WebUtility.HtmlDecode(node.SelectSingleNode("div/div[@class='img-box']/a")
                            .GetAttributeValue("href", ""));
                    //accountInfo.ProfilePicture = node.SelectSingleNode("div/div[1]/a/img").InnerHtml;
                    accountInfo.ProfilePicture = WebUtility.HtmlDecode(node
                        .SelectSingleNode("div/div[@class='img-box']/a/img").GetAttributeValue("src", ""));

                    if (accountInfo.ProfilePicture != null && accountInfo.ProfilePicture.StartsWith("//"))
                    {
                        accountInfo.ProfilePicture = "http:" + accountInfo.ProfilePicture;
                    }


                    accountInfo.Name = node.SelectSingleNode("div/div[2]/p[1]").InnerText.Trim()
                        .Replace("<!--red_beg-->", "").Replace("<!--red_end-->", "");
                    accountInfo.WeChatId = node.SelectSingleNode("div/div[2]/p[2]/label").InnerText.Trim();
                    accountInfo.QrCode =
                        WebUtility.HtmlDecode(node.SelectSingleNode("div/div[3]/span/img")
                            .GetAttributeValue("src", ""));
                    accountInfo.Introduction = node.SelectSingleNode("dl[1]/dd").InnerText.Trim()
                        .Replace("<!--red_beg-->", "").Replace("<!--red_end-->", "");
                    //早期的账号认证和后期的认证显示不一样？，对比 bitsea 和 NUAA_1952 两个账号
                    //现在改为包含该script的即认证了
                    accountInfo.IsAuth = node.InnerText.Contains("document.write(authname('2'))");
                    accountList.Add(accountInfo);
                }
                catch (Exception e)
                {
                    _logger.Warn(e);
                }
            }<commit_msg>fix get account info<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HtmlAgilityPack;
using log4net;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using WeGouSharp.Model;

namespace WeGouSharp.Core
{
    /// <inheritdoc />
    internal class WechatSogouApi : WechatSogouBasic
    {
        readonly ILog _logger;
        private readonly Browser _browser;
        private IConfiguration _configuration;

        public WechatSogouApi(ILog logger, Browser browser, IConfiguration configuration) : base(logger, browser,
            configuration)
        {
            _logger = logger;
            _browser = browser;
            _configuration = configuration;
        }

        /// <summary>
        /// 搜索公众号
        /// </summary>
        /// <param name="keyword">搜索关键字</param>
        /// <param name="page">第几页</param>
        /// <returns></returns>
        public async Task<List<OfficialAccount>> SearchOfficialAccountAsync(string keyword, int page = 1)
        {
            var accountList = new List<OfficialAccount>();

            //var text = this._SearchAccount_Html(keyword, page);
            var text = await SearchAccountHtmlAsync(keyword, page);
            var pageDoc = new HtmlDocument();
            pageDoc.LoadHtml(text);
            var targetArea = pageDoc.DocumentNode.SelectNodes("//ul[@class='news-list2']/li");
            if (targetArea == null) return null;
            foreach (var node in targetArea)
            {
                var accountInfo = new OfficialAccount();
                try
                {
                    //链接中包含了&amp; html编码符，要用htmdecode，不是urldecode
                    accountInfo.AccountPageurl =
                        WebUtility.HtmlDecode(node.SelectSingleNode("div/div[@class='img-box']/a")
                            .GetAttributeValue("href", ""));
                    //accountInfo.ProfilePicture = node.SelectSingleNode("div/div[1]/a/img").InnerHtml;
                    accountInfo.ProfilePicture = WebUtility.HtmlDecode(node
                        .SelectSingleNode("div/div[@class='img-box']/a/img").GetAttributeValue("src", ""));

                    if (accountInfo.ProfilePicture != null && accountInfo.ProfilePicture.StartsWith("//"))
                    {
                        accountInfo.ProfilePicture = "http:" + accountInfo.ProfilePicture;
                    }


                    accountInfo.Name = node.SelectSingleNode("div/div[2]/p[1]").InnerText.Trim()
                        .Replace("<!--red_beg-->", "").Replace("<!--red_end-->", "");
                    accountInfo.WeChatId = node.SelectSingleNode("div/div[2]/p[2]/label").InnerText.Trim();
                    accountInfo.QrCode =
                        WebUtility.HtmlDecode(node.SelectSingleNode("div/div[4]/span/img")
                            .GetAttributeValue("src", ""));
                    accountInfo.Introduction = node.SelectSingleNode("dl[1]/dd").InnerText.Trim()
                        .Replace("<!--red_beg-->", "").Replace("<!--red_end-->", "");
                    //早期的账号认证和后期的认证显示不一样？，对比 bitsea 和 NUAA_1952 两个账号
                    //现在改为包含该script的即认证了
                    accountInfo.IsAuth = node.InnerText.Contains("document.write(authname('2'))");
                    accountList.Add(accountInfo);
                }
                catch (Exception e)
                {
                    _logger.Warn(e);
                }
            }
{'fraction_non_alphanumeric': 0.12473721093202522, 'fraction_numerical': 0.004204625087596356, 'mean_word_length': 1.8094488188976379, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3816038', 'n_tokens_mistral': 1964, 'n_tokens_neox': 1926, 'n_words': 303}