Commit ac91d71f authored by 黄奎's avatar 黄奎

新增缓存

parent ac0ca2db
...@@ -10,5 +10,7 @@ namespace EduSpider.Cache ...@@ -10,5 +10,7 @@ namespace EduSpider.Cache
/// 用户登录缓存Key /// 用户登录缓存Key
/// </summary> /// </summary>
public static string User_Login_Key = "JJSW_Login_"; public static string User_Login_Key = "JJSW_Login_";
public static string ClassIn_Cookie_Key = "ClassInCookie";
} }
} }
...@@ -40,6 +40,44 @@ namespace EduSpider.Cache.User ...@@ -40,6 +40,44 @@ namespace EduSpider.Cache.User
} }
} }
/// <summary>
/// 设置Class登录Cookie
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="model"></param>
/// <param name="JwtExpirTime"></param>
public static void ClassInCookieSet(string cacheKey, ClassInTokenItem model, int JwtExpirTime)
{
try
{
TimeSpan ts = GetExpirTime(JwtExpirTime);
redis.StringSet<ClassInTokenItem>(cacheKey, model, ts);
}
catch (Exception ex)
{
LogHelper.WriteError("ClassInCookieSet", "缓存设置失败", ex);
}
}
/// <summary>
/// 获取ClassInToken
/// </summary>
/// <param name="cacheKey"></param>
/// <returns></returns>
public static ClassInTokenItem GetClassInCookie(string cacheKey)
{
ClassInTokenItem tokenInfo = new ClassInTokenItem();
try
{
tokenInfo = redis.StringGet<ClassInTokenItem>(cacheKey);
}
catch (Exception ex)
{
LogHelper.WriteError("GetUserLoginInfo", "获取缓存失败", ex);
}
return tokenInfo;
}
/// <summary> /// <summary>
/// 获取缓存时长 /// 获取缓存时长
/// </summary> /// </summary>
...@@ -80,7 +118,9 @@ namespace EduSpider.Cache.User ...@@ -80,7 +118,9 @@ namespace EduSpider.Cache.User
{ {
} }
} }
/// <summary> /// <summary>
/// 获取缓存 /// 获取缓存
/// </summary> /// </summary>
...@@ -92,11 +132,12 @@ namespace EduSpider.Cache.User ...@@ -92,11 +132,12 @@ namespace EduSpider.Cache.User
object info = redis.StringGet<object>(cacheKey); object info = redis.StringGet<object>(cacheKey);
return info; return info;
} }
catch (Exception) catch (Exception ex)
{ {
return ""; return "";
} }
} }
/// <summary> /// <summary>
/// 账号仓储层对象 /// 账号仓储层对象
...@@ -160,4 +201,9 @@ namespace EduSpider.Cache.User ...@@ -160,4 +201,9 @@ namespace EduSpider.Cache.User
} }
} }
public class ClassInTokenItem
{
public string CacheCookies { get; set; }
}
} }
\ No newline at end of file
...@@ -49,6 +49,7 @@ ...@@ -49,6 +49,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\EduSpider.Cache\EduSpider.Cache.csproj" />
<ProjectReference Include="..\EduSpider.IRepository\EduSpider.IRepository.csproj" /> <ProjectReference Include="..\EduSpider.IRepository\EduSpider.IRepository.csproj" />
<ProjectReference Include="..\EduSpider.Model\EduSpider.Model.csproj" /> <ProjectReference Include="..\EduSpider.Model\EduSpider.Model.csproj" />
<ProjectReference Include="..\EduSpider.Repository\EduSpider.Repository.csproj" /> <ProjectReference Include="..\EduSpider.Repository\EduSpider.Repository.csproj" />
......
...@@ -39,10 +39,8 @@ namespace TicketSpider.Spiders.ClassInRule ...@@ -39,10 +39,8 @@ namespace TicketSpider.Spiders.ClassInRule
if (string.IsNullOrEmpty(_loginCookies)) if (string.IsNullOrEmpty(_loginCookies))
{ {
_loginCookies = string.Empty; _loginCookies = string.Empty;
var cnt = 0; var cnt = 0;
Random r = new Random(); Random r = new Random();
while (cnt < 4) while (cnt < 4)
{ {
cnt++; cnt++;
...@@ -71,8 +69,7 @@ namespace TicketSpider.Spiders.ClassInRule ...@@ -71,8 +69,7 @@ namespace TicketSpider.Spiders.ClassInRule
public static bool RunAsync() public static bool RunAsync()
{ {
var flag = false; var flag = false;
IWebElement inputEle;
IWebElement pwdEle;
var option = new FirefoxOptions(); var option = new FirefoxOptions();
option.AddArgument("--incognito"); option.AddArgument("--incognito");
string IsOpenChrome = VTX.FW.Helper.ConfigHelper.GetAppsettings("IsOpenChrome"); string IsOpenChrome = VTX.FW.Helper.ConfigHelper.GetAppsettings("IsOpenChrome");
...@@ -86,9 +83,8 @@ namespace TicketSpider.Spiders.ClassInRule ...@@ -86,9 +83,8 @@ namespace TicketSpider.Spiders.ClassInRule
//option.AddArgument("headless"); //option.AddArgument("headless");
//火狐隐身模式 //火狐隐身模式
option.AddArgument("--headless"); option.AddArgument("--headless");
//option.AddArgument("--disable-gpu"); //option.AddArgument("--disable-gpu");
// option.AddArgument("ingore-certificate-errors"); // option.AddArgument("ingore-certificat e-errors");
} }
option.AddArgument("--disable-infobars"); option.AddArgument("--disable-infobars");
option.AddArgument("window-size=1920*1080"); option.AddArgument("window-size=1920*1080");
...@@ -112,67 +108,78 @@ namespace TicketSpider.Spiders.ClassInRule ...@@ -112,67 +108,78 @@ namespace TicketSpider.Spiders.ClassInRule
//等待页面元素加载完成 //等待页面元素加载完成
//默认等待100秒 //默认等待100秒
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100)); WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100));
IWebElement inputEle;
//等待页面上按钮加载 IWebElement pwdEle;
inputEle = wait.Until((d) => try
{ {
return d.FindElement(By.Name("phoneNum")); //等待页面上按钮加载
}); inputEle = wait.Until((d) =>
pwdEle = wait.Until((d) => {
return d.FindElement(By.Name("phoneNum"));
});
pwdEle = wait.Until((d) =>
{
return d.FindElement(By.Name("loginPwd"));
});
if (inputEle != null && pwdEle != null)
{
inputEle.SendKeys("18140082327");
pwdEle.SendKeys("ls123456");
}
}
catch (Exception ex)
{ {
return d.FindElement(By.Name("loginPwd")); InfoHelper.WriteLine("未知异常,程序退出");
}); VTX.FW.Helper.LogHelper.WriteInfo("RunAsync", $"未知异常,程序退出");
if (inputEle != null && pwdEle != null)
}
var buttonEle = driver.FindElement(By.CssSelector(".submit-btn"));
Thread.Sleep(1000);
//开始登录
buttonEle.Click();
//开始模拟登录
Random r = new Random();
for (var loginCount = 1; loginCount <= 2; loginCount++)
{ {
inputEle.SendKeys("18140082327"); if (loginCount > 1)
pwdEle.SendKeys("ls123456"); {
var buttonEle = driver.FindElement(By.CssSelector(".submit-btn")); int num = r.Next(1, 10);
Thread.Sleep(1000); Thread.Sleep(1000 * num);
//开始登录 }
buttonEle.Click(); InfoHelper.WriteLine($"正在进行第{loginCount}次模拟登录...");
//开始模拟登录 VTX.FW.Helper.LogHelper.WriteInfo("RunAsync", $"正在进行第{loginCount}次模拟登录...");
Random r = new Random(); var login = FireFoxBeginSliderLoginHandler(driver);
for (var loginCount = 1; loginCount <= 3; loginCount++) if (login.IsCompleted)
{ {
if (loginCount > 1) driver.SwitchTo().DefaultContent();
Thread.Sleep(5 * 1000);
var cookies = driver.Manage().Cookies.AllCookies;
InfoHelper.WriteLine($"操作完成,正在等待结果");
VTX.FW.Helper.LogHelper.WriteInfo("RunAsync", $"操作完成,正在等待结果");
//处理
foreach (var item in cookies)
{ {
int num = r.Next(1, 10); _loginCookies += item.Name + "=" + item.Value + ";";
Thread.Sleep(1000 * num);
} }
InfoHelper.WriteLine($"正在进行第{loginCount}次模拟登录..."); _loginCookies = _loginCookies.Substring(0, _loginCookies.Length - 1);
VTX.FW.Helper.LogHelper.WriteInfo("RunAsync", $"正在进行第{loginCount}次模拟登录..."); if (!string.IsNullOrEmpty(_loginCookies) && _loginCookies.Contains("sensorsdata2015jssdkcross") && _loginCookies.Contains("_eeos_traffic"))
var login = FireFoxBeginSliderLoginHandler(driver);
if (login.IsCompleted)
{ {
driver.SwitchTo().DefaultContent(); flag = true;
Thread.Sleep(5 * 1000); break;
var cookies = driver.Manage().Cookies.AllCookies;
InfoHelper.WriteLine($"操作完成,正在等待结果");
VTX.FW.Helper.LogHelper.WriteInfo("RunAsync", $"操作完成,正在等待结果");
//处理
foreach (var item in cookies)
{
_loginCookies += item.Name + "=" + item.Value + ";";
}
_loginCookies = _loginCookies.Substring(0, _loginCookies.Length - 1);
if (!string.IsNullOrEmpty(_loginCookies) && _loginCookies.Contains("sensorsdata2015jssdkcross") && _loginCookies.Contains("_eeos_traffic"))
{
flag = true;
break;
}
else
{
_loginCookies = "";
}
} }
else else
{ {
InfoHelper.WriteLine("未知异常,程序退出"); _loginCookies = "";
VTX.FW.Helper.LogHelper.WriteInfo("RunAsync", $"未知异常,程序退出");
break;
} }
} }
else
{
InfoHelper.WriteLine("未知异常,程序退出");
VTX.FW.Helper.LogHelper.WriteInfo("RunAsync", $"未知异常,程序退出");
break;
}
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
......
...@@ -18,7 +18,21 @@ namespace TicketSpider.Spiders.ClassInRule ...@@ -18,7 +18,21 @@ namespace TicketSpider.Spiders.ClassInRule
if (string.IsNullOrEmpty(loginCookies)) if (string.IsNullOrEmpty(loginCookies))
{ {
ClassInAccountManager.ClearCookie(); ClassInAccountManager.ClearCookie();
loginCookies = ClassInAccountManager.GetInstance(); string key = EduSpider.Cache.CacheKey.ClassIn_Cookie_Key;
int ExpirTime = 60 * 60 * 12;
var cacheCookie = EduSpider.Cache.User.UserReidsCache.GetClassInCookie(key);
if (cacheCookie != null && !string.IsNullOrEmpty(cacheCookie.CacheCookies.ToString()))
{
loginCookies = cacheCookie.CacheCookies;
}
else
{
loginCookies = ClassInAccountManager.GetInstance();
if (!string.IsNullOrEmpty(loginCookies))
{
EduSpider.Cache.User.UserReidsCache.ClassInCookieSet(key, new EduSpider.Cache.User.ClassInTokenItem() { CacheCookies=loginCookies}, ExpirTime);
}
}
} }
InfoHelper.WriteLine("cookies:" + loginCookies); InfoHelper.WriteLine("cookies:" + loginCookies);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment