yz_server/Server/Timers/UpdateCookiesTimer.cs

159 lines
8.4 KiB
C#
Raw Permalink Normal View History

2022-04-16 07:48:12 +00:00
using Common.DbExtends.Extends;
using Common.Models.UnqTables;
using Common.Utils;
using CsharpHttpHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Server.Timers
{
public class UpdateCookiesTimer : MyTimer
{
private static string[] Urls = new string[]
{
"http://www.alimama.com/index.htm",
"https://ssp.tanx.com/api/userinfo?callback=userInfoCallback",
"http://ssp.tanx.com/api/userinfo?callback=userInfoCallback",
"https://media.alimama.com/user/base_info.htm",
"https://media.alimama.com/verify/risk.htm",
"https://media.alimama.com/",
"https://media.alimama.com/product.htm",
"https://pub.alimama.com/myunion.htm",
"https://tbk.bbs.taobao.com/list.html",
"https://pub.alimama.com/manage/overview/index.htm",
"https://pub.alimama.com/common/getUnionPubContextInfo.json",
"https://media.alimama.com/violationu2/violation2_page.json?r=mx_794&toPage={pageIndex}&pageSize={pageSize}",
"https://pub.alimama.com/third/manage/record/adzone.htmhttps://pub.alimama.com/third/manage/record/site.htm?tab=self_web_site",//媒体备案管理 - 自有平台(网站)
"https://pub.ama.com/third/manage/record/site.htm?tab=self_web_site&sencecode=self_app&pageNo={pageIndex}&pageSize={pageSize}",//媒体备案管理 - 自有平台(App客户端)
"https://pub.alimama.com/third/manage/record/site.htm?tab=self_web_site&sencecode=self_pc_client&pageNo={pageIndex}&pageSize={pageSize}",//媒体备案管理 - 自有平台(PC客户端)
"https://pub.alimama.com/third/manage/record/site.htm?tab=self_web_site&sencecode=self_offline&pageNo={pageIndex}&pageSize={pageSize}",//媒体备案管理 - 自有平台(线下阵地)
"https://pub.alimama.com/third/manage/record/adzone.htm",//推广位
"https://pub.alimama.com/third/manage/record/site.htm?tab=all",//媒体备案管理 - 全部
"https://pub.alimama.com/third/manage/record/site.htm?tab=self_web_site&sencecode=self_miniprogram&pageNo={pageIndex}&pageSize={pageSize}",//媒体备案管理 - 自有平台(小程序/快应用)
"https://pub.alimama.com/third/manage/record/site.htm?tab=self_web_site&sencecode=self_operating_system&pageNo=1&pageSize={pageSize}",//媒体备案管理 - 自有平台(操作系统)
"https://pub.alimama.com/third/manage/record/site.htm?tab=self_web_site&sencecode=self_hardware_device&pageNo=1&pageSize={pageSize}",//媒体备案管理 - 自有平台(硬件设备)
"https://pub.alimimama.com/third/manage/record/site.htm?tab=other_social",//媒体备案管理 - 他方平台(社交平台)
"https://pub.alimama.com/third/manage/record/site.htm?tab=other_social&sencecode=other_content&pageNo=1&pageSize={pageSize}",//媒体备案管理 - 他方平台(内容平台)
"https://pub.alimama.com/third/manage/record/site.htm?tab=traffic_purchasing",//媒体备案管理 - 无自有阵地
"http://c.tanx.com/",
//"https://pub.alimama.com/openapi/json2/1/gateway.unionpub/xt.entry.json",////内容库查询
"https://pub.alimama.com/manage/user/new_privilege.htm",
"https://media.alimama.com/violationu2/violation2_page.json?toPage={pageIndex}&pageSize={pageSize}&punishStatus={punishStatus}",
"https://pub.alimama.com/manage/overview/index.htm",
"https://pub.alimama.com/openapi/param2/1/gateway.unionpub/report.getTbkOrderDetails.json?t={timespan}000&_tb_token_={tb_token}&jumpType=0&positionIndex=&pageNo={pageIndex}&startTime={开始时间}&endTime={结束时间}&queryType{queryType}&tkStatus=&memberType=&pageSize={pageSize}",
"https://pub.alimama.com/common/getUnionPubContextInfo.json",
"https://media.alimama.com/violationu2/warning_instance_page.json?r=mx_544&toPage={pageIndex}&pageSize={pageSize}",
"https://everyservice.cdn.taobao.com/widget/queryServiceStrategy",
"https://everyhelp.taobao.com/version/getWidgetVersion",
"https://media.alimama.com/violation/violation_list.htm",
"https://c.tanx.com/"
};
private Client client = Client.SingleClient;
protected override void Run(object state, bool timedOut)
{
//通过cookise更新不需要多线程防止速度过快被限制IP
try
{
var lianmengs = client.Db.Queryable<Lianmeng>().Where(f => f.LianmengType == Common.Models.Enums.LianmengType. && f.IsCookiValid == true).ToList();
foreach (var item in lianmengs)
{
UpdateAlimamaCookie(item);
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
client.Db.OnLog("更新Cookies异常",ex);
}
}
/// <summary>
/// 更新Cookies
/// </summary>
/// <param name="lm">需要更新的联盟账号</param>
private void UpdateAlimamaCookie(Lianmeng lm)
{
var url = Urls[new Random(Guid.NewGuid().GetHashCode()).Next(0,Urls.Length)];
if (UpdateCookies(lm, url, string.Empty))
{
client.Db.Save(lm);
}
}
/// <summary>
/// 更新Cookies
/// </summary>
/// <param name="lm">更新哪个联盟</param>
/// <param name="url">需要更新的URL</param>
/// <param name="postData">POST数据</param>
/// <returns>如果有更新会返回true</returns>
private bool UpdateCookies(Lianmeng lm,string url,string postData)
{
var flag = false;
try
{
//当前页码
var pageIndex = new Random(Guid.NewGuid().GetHashCode()).Next(1, 5).ToString();
//每页显示条数
var pageSize = "40";
//提取tb_token
var tb_token = HttpExtend.GetCookiesValue("_tb_token", lm.Cookies);
//获取punishStatus
var punishStatus = new Random(Guid.NewGuid().GetHashCode()).Next(1, 9).ToString();
url = url.Replace("{pageSize}", pageSize)
.Replace("{pageIndex}", pageIndex)
.Replace("{tb_token}", tb_token)
.Replace("{punishStatus}", punishStatus)
.Replace("{开始时间}", DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd"))
.Replace("{开始时间}", DateTime.Now.ToString("yyyy-MM-dd"))
.Replace("{timespan}", HttpExtend.GetTimeStamp())
.Replace("{12位随机}", Util.GetTimespan(DateTime.Now).ToString());
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = url,
Method = string.IsNullOrEmpty(postData) ? "get" : "post",
IsToLower = false,
Cookie = lm.Cookies,
Referer = "http://pub.alimama.com/myunion.htm",
Postdata = postData,
Timeout = 3000,
ReadWriteTimeout = 3000,
UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.154 Safari/537.36 LBBROWSER",
ContentType = "application/x-www-form-urlencoded",
Allowautoredirect = true,
ProtocolVersion = System.Net.HttpVersion.Version11
};
var result = http.GetHtml(item);
if (result != null && result.Html.Contains("noLogin\":true"))
{
lm.IsCookiValid = false;
flag = true;
}
var cookies = result.Cookie;
if (!string.IsNullOrEmpty(cookies))
{
lm.Cookies = result.UpdateCookies(result.Cookie);
flag = true;
}
}
catch (Exception)
{
}
return flag;
}
}
}