/// <summary> /// 获取关注信息。 /// </summary> /// <param name="startDate">起始日期。</param> /// <param name="endDate">结束日期。</param> /// <returns>关注信息数组。</returns> public AttentionInfo[] GetAttentions(DateTime startDate, DateTime endDate) { //得到AppId和插件Token。 var info = GetAppIdAndPluginToken(); var appId = info.Key; var pluginToken = info.Value; //关注数据请求Url。 var url = string.Format( "https://mta.qq.com/mta/wechat/ctr_user_summary/get_table_data/?start_date={0}&end_date={1}&need_compare=0&start_compare_date=&end_compare_date=&app_id=&source_list=-1&source_show=35%2C3%2C43%2C17%2C0&appid={2}&pluginid=luopan&token={3}&from=&devtype=2&time_type=day&ajax=1", startDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"), appId, pluginToken); using (var client = new LastingWebClient(_cookieContainer)) { var data = client.DownloadData(url); var json = Encoding.UTF8.GetString(data); var weiXinAttention = new JavaScriptSerializer().Deserialize <WeiXinAttentionInfo>(json); return((weiXinAttention == null || weiXinAttention.Data == null) ? new AttentionInfo[0] : weiXinAttention.Data.Select(i => new AttentionInfo { CancelUser = i.GetCancelUser(), CumulateUser = i.GetCumulateUser(), Date = i.RefDate, NewUser = i.GetNewUser(), NetUser = i.GetNewUser(), }).ToArray()); } }
/// <summary> /// 用户微信用户信息。 /// </summary> /// <returns>用户信息。</returns> public UserInfo GetUserInfo() { using (var client = new LastingWebClient(_cookieContainer)) { var html = Encoding.UTF8.GetString(client.DownloadData( string.Format( "https://mp.weixin.qq.com/cgi-bin/settingpage?t=setting/index&action=index&token={0}&lang=zh_CN", LoginResult.Token))); var document = new HtmlDocument(); document.LoadHtml(html); var offset = 0; var type = GetMetaContent(document, 4); switch (type) { case "服务号": offset = 0; break; default: offset = 1; break; } var user = new UserInfo { Picture = new Lazy <byte[]>(() => { using (var c = new LastingWebClient(_cookieContainer)) { return(c.DownloadData( document.DocumentNode.CssSelect(".avatar").FirstOrDefault().GetAttributeValue("src").Insert(0, "https://mp.weixin.qq.com"))); } }), Name = GetMetaContent(document, 2), LoginEmail = GetMetaContent(document, 10 - offset), OriginalId = GetMetaContent(document, 11 - offset), UserName = GetMetaContent(document, 3), Type = type, AuthenticateStatus = GetMetaContent(document, 6), Body = GetMetaContent(document, 9 - offset), Area = GetMetaContent(document, 8 - offset), Description = GetMetaContent(document, 5), QrCode = new Lazy <byte[]>(() => { using (var c = new LastingWebClient(_cookieContainer)) { return(c.DownloadData( document.DocumentNode.CssSelect(".verifyInfo").FirstOrDefault().GetAttributeValue("href").Insert(0, "https://mp.weixin.qq.com"))); } }) }; return(user); } }
private KeyValuePair <string, string> GetAppIdAndPluginToken() { using (var client = new LastingWebClient(_cookieContainer)) { client.Headers.Add("Referer", string.Format("https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token={0}", _loginResult.Token)); var url = string.Format( "https://mp.weixin.qq.com/misc/pluginloginpage?action=stat_user_summary&pluginid=luopan&t=statistics/index&token={0}&lang=zh_CN", _loginResult.Token); var html = Encoding.UTF8.GetString(client.DownloadData(url)); var appId = GetJsonValue(html, "appid : '"); var pluginToken = GetJsonValue(html, "pluginToken : '"); return(new KeyValuePair <string, string>(appId, pluginToken)); } }
/// <summary> /// 获取开发者凭据。 /// </summary> /// <returns>如果具有开发者凭据则返回否则返回null。</returns> public DevelopCredential GetDevelopCredential() { using (var client = new LastingWebClient(_cookieContainer)) { var url = string.Format( "https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token={0}&lang=zh_CN", _loginResult.Token); var html = Encoding.UTF8.GetString(client.DownloadData(url)); var document = new HtmlDocument(); document.LoadHtml(html); var items = document.DocumentNode.CssSelect(".developer_info_item").ToArray(); if (items.Count() < 4) { return(null); } var item = items.First(); var controls = item.CssSelect(".frm_controls").ToArray(); string appSecret; using (var reader = new StringReader(controls.Last().InnerText.Trim())) appSecret = reader.ReadLine(); if (!string.IsNullOrWhiteSpace(appSecret)) { var index = appSecret.Trim().IndexOf(" ", StringComparison.Ordinal); if (index != -1) { appSecret = appSecret.Substring(0, index); } } return(new DevelopCredential { AppId = controls.First().InnerText.Trim(), AppSecret = appSecret }); } }
/// <summary> /// 用户微信用户信息。 /// </summary> /// <returns>用户信息。</returns> public UserInfo GetUserInfo() { using (var client = new LastingWebClient(_cookieContainer)) { var html = Encoding.UTF8.GetString(client.DownloadData( string.Format( "https://mp.weixin.qq.com/cgi-bin/settingpage?t=setting/index&action=index&token={0}&lang=zh_CN", LoginResult.Token))); var document = new HtmlDocument(); document.LoadHtml(html); var offset = 0; var type = GetMetaContent(document, 4); switch (type) { case "服务号": offset = 0; break; default: offset = 1; break; } var user = new UserInfo { Picture = new Lazy<byte[]>(() => { using (var c = new LastingWebClient(_cookieContainer)) { return c.DownloadData( document.DocumentNode.CssSelect(".avatar").FirstOrDefault().GetAttributeValue("src").Insert(0, "https://mp.weixin.qq.com")); } }), Name = GetMetaContent(document, 2), LoginEmail = GetMetaContent(document, 10 - offset), OriginalId = GetMetaContent(document, 11 - offset), UserName = GetMetaContent(document, 3), Type = type, AuthenticateStatus = GetMetaContent(document, 6), Body = GetMetaContent(document, 9 - offset), Area = GetMetaContent(document, 8 - offset), Description = GetMetaContent(document, 5), QrCode = new Lazy<byte[]>(() => { using (var c = new LastingWebClient(_cookieContainer)) { return c.DownloadData( document.DocumentNode.CssSelect(".verifyInfo").FirstOrDefault().GetAttributeValue("href").Insert(0, "https://mp.weixin.qq.com")); } }) }; return user; } }
private KeyValuePair<string, string> GetAppIdAndPluginToken() { using (var client = new LastingWebClient(_cookieContainer)) { client.Headers.Add("Referer", string.Format("https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token={0}", _loginResult.Token)); var url = string.Format( "https://mp.weixin.qq.com/misc/pluginloginpage?action=stat_user_summary&pluginid=luopan&t=statistics/index&token={0}&lang=zh_CN", _loginResult.Token); var html = Encoding.UTF8.GetString(client.DownloadData(url)); var appId = GetJsonValue(html, "appid : '"); var pluginToken = GetJsonValue(html, "pluginToken : '"); return new KeyValuePair<string, string>(appId, pluginToken); } }
/// <summary> /// 获取关注信息。 /// </summary> /// <param name="startDate">起始日期。</param> /// <param name="endDate">结束日期。</param> /// <returns>关注信息数组。</returns> public AttentionInfo[] GetAttentions(DateTime startDate, DateTime endDate) { //得到AppId和插件Token。 var info = GetAppIdAndPluginToken(); var appId = info.Key; var pluginToken = info.Value; //关注数据请求Url。 var url = string.Format( "https://mta.qq.com/mta/wechat/ctr_user_summary/get_table_data/?start_date={0}&end_date={1}&need_compare=0&start_compare_date=&end_compare_date=&app_id=&source_list=-1&source_show=35%2C3%2C43%2C17%2C0&appid={2}&pluginid=luopan&token={3}&from=&devtype=2&time_type=day&ajax=1", startDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"), appId, pluginToken); using (var client = new LastingWebClient(_cookieContainer)) { var data = client.DownloadData(url); var json = Encoding.UTF8.GetString(data); var weiXinAttention = new JavaScriptSerializer().Deserialize<WeiXinAttentionInfo>(json); return (weiXinAttention == null || weiXinAttention.Data == null) ? new AttentionInfo[0] : weiXinAttention.Data.Select(i => new AttentionInfo { CancelUser = i.GetCancelUser(), CumulateUser = i.GetCumulateUser(), Date = i.RefDate, NewUser = i.GetNewUser(), NetUser = i.GetNewUser(), }).ToArray(); } }
/// <summary> /// 获取开发者凭据。 /// </summary> /// <returns>如果具有开发者凭据则返回否则返回null。</returns> public DevelopCredential GetDevelopCredential() { using (var client = new LastingWebClient(_cookieContainer)) { var url = string.Format( "https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token={0}&lang=zh_CN", _loginResult.Token); var html = Encoding.UTF8.GetString(client.DownloadData(url)); var document = new HtmlDocument(); document.LoadHtml(html); var items = document.DocumentNode.CssSelect(".developer_info_item").ToArray(); if (items.Count() < 4) return null; var item = items.First(); var controls = item.CssSelect(".frm_controls").ToArray(); string appSecret; using (var reader = new StringReader(controls.Last().InnerText.Trim())) appSecret = reader.ReadLine(); if (!string.IsNullOrWhiteSpace(appSecret)) { var index = appSecret.Trim().IndexOf(" ", StringComparison.Ordinal); if (index != -1) { appSecret = appSecret.Substring(0, index); } } return new DevelopCredential { AppId = controls.First().InnerText.Trim(), AppSecret = appSecret }; } }