Browse Source

更改内核下载源

master
feng 3 years ago
parent
commit
8f2550a822
  1. 5
      JdShopListener/JdShopListener/ApiHelper.cs
  2. 2
      JdShopListener/JdShopListener/MainWindow.xaml
  3. 53
      JdShopListener/JdShopListener/MainWindowViewModel.cs
  4. 2
      JdShopListener/JdShopListener/Properties/PublishProfiles/FolderProfile.pubxml.user
  5. 105
      JdShopListener/JdShopListener/PuppeteerConsole.cs

5
JdShopListener/JdShopListener/ApiHelper.cs

@ -21,6 +21,11 @@ namespace Utils
{
get
{
#if DEBUG
jwtToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxNDM5OTA3NzYwNzU3MDE4NjI0IiwidGVhbUlkIjoiMTUxODUwNTk1MTg1Mjc2MTA4OCIsImV4cCI6MTY4MzUwNzk1NX0.jLRfN4ztX7DZnTacG3-SlvXj6jJiQIlTnaXOdTWSXjQ";
return jwtToken;
#endif
if (string.IsNullOrEmpty(jwtToken))
{
jwtToken = Utils.MemoryHelper.GetMemoryToken();

2
JdShopListener/JdShopListener/MainWindow.xaml

@ -6,7 +6,7 @@
xmlns:local="clr-namespace:JdShopListener"
mc:Ignorable="d"
DataContext="{x:Static local:MainWindowViewModel.MainViewModel}"
Title="跟屁虫" Height="450" Width="800" >
Title="跟屁虫V1.0.1" Height="450" Width="800" >
<Window.Resources>
<Style
TargetType="{x:Type TabItem}">

53
JdShopListener/JdShopListener/MainWindowViewModel.cs

@ -115,19 +115,35 @@ namespace JdShopListener
/// </summary>
public void InitWeb()
{
string floader= AppDomain.CurrentDomain.SetupInformation.ApplicationBase + ".local-chromium";
if (System.IO.Directory.Exists(floader))
var dir = PuppeteerConsole.DefaultDirectory;
if (!PuppeteerConsole.TryGetChromeExePath(dir, out var exeFile))
{
return;
}
IsInitLoding = true;
InitText = "检测到必要文件丢失,开始下载。。。";
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
webClient.DownloadFileTaskAsync("https://qymds.oss-cn-hangzhou.aliyuncs.com/chromium/.local-chromium.zip", AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "tempZip.zip").Wait();
exeFile = PuppeteerConsole.DownloadAsync(dir, 999889).Result;
if (exeFile is null)
{
throw new IOException("Download of chrome.exe failed.");
}
IsInitLoding = false;
}
//string floader= AppDomain.CurrentDomain.SetupInformation.ApplicationBase + ".local-chromium";
//if (System.IO.Directory.Exists(floader))
//{
// return;
//}
//WebClient webClient = new WebClient();
//webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;
//webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
//webClient.DownloadFileTaskAsync("https://qymds.oss-cn-hangzhou.aliyuncs.com/chromium/.local-chromium.zip", AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "tempZip.zip").Wait();
}
private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
@ -1236,15 +1252,20 @@ namespace JdShopListener
// var fetcher= new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision).Result;
string url = "https://passport.jd.com/uc/login";
;
// 创建一个浏览器执行实例
using var browser = Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true,
//using var browser = Puppeteer.LaunchAsync(new LaunchOptions
//{
// Headless = true,
// //Args = new string[] { "--no-sandbox" }
//}).Result;
//Args = new string[] { "--no-sandbox" }
}).Result;
try
{
using var browser = PuppeteerConsole.LauncherBrowser(true).Result;
// 打开一个页面
var page = browser.NewPageAsync().Result;
@ -1265,6 +1286,10 @@ namespace JdShopListener
{
WpfNoticeMsg.NoticeMessage.Show("初始化内核失败,请尝试关闭跟屁虫重新启动!", "错误");
}
finally {
PuppeteerConsole.CloseAllBrowsers();
}
AddLog("获取到Cookie" + JDCookie);
}

2
JdShopListener/JdShopListener/Properties/PublishProfiles/FolderProfile.pubxml.user

@ -4,6 +4,6 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<History>True|2022-01-03T09:04:13.2257910Z;True|2022-01-03T16:54:29.6063209+08:00;True|2022-01-02T11:31:19.6646883+08:00;True|2021-11-13T14:54:09.0591524+08:00;True|2021-11-13T14:52:48.4056995+08:00;True|2021-11-13T14:51:49.0309726+08:00;True|2021-11-13T14:48:43.2272103+08:00;True|2021-11-13T14:47:27.9721804+08:00;True|2021-11-13T14:45:09.2474573+08:00;True|2021-11-13T14:41:32.7950744+08:00;True|2021-11-13T14:36:57.1688384+08:00;True|2021-11-13T14:34:06.7538906+08:00;True|2021-11-13T11:23:58.1124027+08:00;False|2021-11-13T11:23:39.3148111+08:00;False|2021-11-13T11:22:51.2298917+08:00;True|2021-11-13T10:44:44.3567647+08:00;True|2021-11-13T10:42:01.8636514+08:00;True|2021-11-13T10:38:36.0623458+08:00;True|2021-11-13T10:20:29.4761762+08:00;True|2021-11-13T10:08:51.4924474+08:00;True|2021-11-12T15:27:50.0418057+08:00;True|2021-11-12T15:06:23.1220477+08:00;True|2021-11-11T18:06:38.5161322+08:00;True|2021-11-11T17:53:13.2835443+08:00;True|2021-11-11T17:52:00.9170918+08:00;True|2021-11-11T17:51:37.4852682+08:00;True|2021-11-11T17:49:30.9386192+08:00;True|2021-11-11T17:44:28.5146341+08:00;True|2021-11-11T17:42:26.8480671+08:00;True|2021-11-11T17:37:14.4108790+08:00;True|2021-11-11T17:30:25.4460722+08:00;</History>
<History>True|2022-05-08T01:29:38.6199429Z;True|2022-01-03T17:04:13.2257910+08:00;True|2022-01-03T16:54:29.6063209+08:00;True|2022-01-02T11:31:19.6646883+08:00;True|2021-11-13T14:54:09.0591524+08:00;True|2021-11-13T14:52:48.4056995+08:00;True|2021-11-13T14:51:49.0309726+08:00;True|2021-11-13T14:48:43.2272103+08:00;True|2021-11-13T14:47:27.9721804+08:00;True|2021-11-13T14:45:09.2474573+08:00;True|2021-11-13T14:41:32.7950744+08:00;True|2021-11-13T14:36:57.1688384+08:00;True|2021-11-13T14:34:06.7538906+08:00;True|2021-11-13T11:23:58.1124027+08:00;False|2021-11-13T11:23:39.3148111+08:00;False|2021-11-13T11:22:51.2298917+08:00;True|2021-11-13T10:44:44.3567647+08:00;True|2021-11-13T10:42:01.8636514+08:00;True|2021-11-13T10:38:36.0623458+08:00;True|2021-11-13T10:20:29.4761762+08:00;True|2021-11-13T10:08:51.4924474+08:00;True|2021-11-12T15:27:50.0418057+08:00;True|2021-11-12T15:06:23.1220477+08:00;True|2021-11-11T18:06:38.5161322+08:00;True|2021-11-11T17:53:13.2835443+08:00;True|2021-11-11T17:52:00.9170918+08:00;True|2021-11-11T17:51:37.4852682+08:00;True|2021-11-11T17:49:30.9386192+08:00;True|2021-11-11T17:44:28.5146341+08:00;True|2021-11-11T17:42:26.8480671+08:00;True|2021-11-11T17:37:14.4108790+08:00;True|2021-11-11T17:30:25.4460722+08:00;</History>
</PropertyGroup>
</Project>

105
JdShopListener/JdShopListener/PuppeteerConsole.cs

@ -0,0 +1,105 @@
using PuppeteerSharp;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace JdShopListener
{
public static class PuppeteerConsole
{
private readonly static SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1);
private readonly static ConcurrentBag<Browser> _browsers = new ConcurrentBag<Browser>();
public static IReadOnlyCollection<Browser> Browsers => _browsers;
public static DirectoryInfo DefaultDirectory => new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SpiderX", "local-chromium"));
public static async Task<Browser> LauncherBrowser(bool useHeadless, DirectoryInfo directory = null, int revision = 999889)
{
var dir = directory ?? DefaultDirectory;
if (!TryGetChromeExePath(dir, out var exeFile))
{
exeFile = await DownloadAsync(dir, revision);
if (exeFile is null)
{
throw new IOException("Download of chrome.exe failed.");
}
}
var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = useHeadless,
ExecutablePath = exeFile.FullName
});
_browsers.Add(browser);
return browser;
}
public static void CloseAllBrowsers()
{
if (_browsers.IsEmpty)
{
return;
}
var bs = _browsers.ToArray();
_browsers.Clear();
foreach (var b in bs)
{
b?.Dispose();
}
}
public static async Task<FileInfo> DownloadAsync(DirectoryInfo directory, int revision)
{
await _semaphoreSlim.WaitAsync();
if (!directory.Exists)
{
directory.Create();
}
var fetchOpt = new BrowserFetcherOptions()
{
Host = Environment.GetEnvironmentVariable("PUPPETEER_CHROMIUM_DOWNLOADHOST"),
Path = directory.FullName
};
var fetcher = new BrowserFetcher(fetchOpt);
if (revision < 1)
{
string revisionStr = Environment.GetEnvironmentVariable("PUPPETEER_CHROMIUM_REVISION");
if (!int.TryParse(revisionStr, out revision) || revision < 1)
{
revision = BrowserFetcher.DefaultRevision;
}
}
var info = await fetcher.DownloadAsync(revision).ConfigureAwait(false);
_semaphoreSlim.Release();
if (!string.IsNullOrWhiteSpace(info.ExecutablePath))
{
return new FileInfo(info.ExecutablePath);
}
TryGetChromeExePath(directory, out var exeFile);
return exeFile;
}
public static bool TryGetChromeExePath(DirectoryInfo directory, out FileInfo exeFile)
{
exeFile = null;
if (!directory.Exists)
{
return false;
}
var files = directory.GetFiles("chrome.exe", SearchOption.AllDirectories);
if (files is null || files.Length < 1)
{
return false;
}
exeFile = files[0];
return true;
}
}
}
Loading…
Cancel
Save