forked from fiseleo/BlueArchiveDownloaderJP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloadAPK.cs
145 lines (126 loc) · 5.61 KB
/
DownloadAPK.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System.Diagnostics;
using PuppeteerSharp;
namespace BAdownload
{
class Program
{
static async Task Main(string[] args)
{
// 解析參數:假設程式呼叫方式為
// BAdownload.exe -f 1.456789
// 則需要抓取 -f 後的值來組合下載連結。
string rootDirectory = Directory.GetCurrentDirectory();
if (!Directory.Exists(Path.Combine(rootDirectory, "Downloads", "XAPK")))
{
Directory.CreateDirectory(Path.Combine(rootDirectory, "Downloads", "XAPK"));
}
var downloadPath = Path.Combine(rootDirectory, "Downloads", "XAPK");
if (Directory.Exists(downloadPath)){
var files = Directory.GetFiles(downloadPath);
foreach (var file in files)
{
File.Delete(file);
}
}
string versionArg = null;
for (int i = 0; i < args.Length; i++)
{
if (args[i].Equals("-f", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length)
{
versionArg = args[i + 1];
i++;
}
}
// 根據是否有 versionArg 來決定組合的網址
string downloadUrl;
if (!string.IsNullOrEmpty(versionArg) && versionArg.StartsWith("1."))
{
// 取小數點後的部分。例如 "1.53.323417" 只取 "323417"
var versionCode = versionArg.Substring(5); // 從 index=3 開始擷取
downloadUrl = $"https://d.apkpure.com/b/XAPK/com.YostarJP.BlueArchive?versionCode={versionCode}&nc=arm64-v8a&sv=24";
}
else
{
// 沒有指定 -f 參數,或格式不符合,改用 latest
downloadUrl = "https://d.apkpure.com/b/XAPK/com.YostarJP.BlueArchive?version=latest";
}
Console.WriteLine($"準備下載網址: {downloadUrl}");
// 下載/更新 Chromium
var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync(); // 不加任何參數
// 啟動瀏覽器(Headless = false 方便除錯觀察;若要隱藏視窗可以設為 true)
var launchOptions = new LaunchOptions
{
Headless = false, // 顯示瀏覽器視窗
DefaultViewport = null // 不限制視窗大小
};
using var browser = await Puppeteer.LaunchAsync(launchOptions);
var page = await browser.NewPageAsync();
// 設定下載行為:將檔案下載到程式執行目錄 (或指定其他資料夾)
var client = await page.Target.CreateCDPSessionAsync();
await client.SendAsync("Page.setDownloadBehavior", new
{
behavior = "allow",
downloadPath = Path.Combine(rootDirectory, "Downloads", "XAPK"),
});
// 導向至下載頁面,等待網頁載入完成
// 由於 Cloudflare 可能有「五秒盾」或 JS Challenge,可以用 NetworkIdle2/NetworkIdle0 盡量等待網頁完成
try
{
Console.WriteLine("嘗試載入頁面,等待 Cloudflare 驗證...");
await page.GoToAsync(downloadUrl, WaitUntilNavigation.Networkidle2);
}
catch (Exception ex)
{
Console.WriteLine($"載入頁面時發生錯誤: {ex.Message}");
}
await Task.Delay(5000); // 等待 5 秒看是否需要額外 Cloudflare 檢查
Console.WriteLine("開始等待檔案下載...");
string downloadedFile = await WaitForDownloadedFileAsync(downloadPath, TimeSpan.FromSeconds(600));
if (downloadedFile != null)
{
Console.WriteLine($"檔案下載完成: {downloadedFile}");
}
else
{
Console.WriteLine("下載逾時或沒有偵測到下載檔案。");
}
Console.WriteLine("下載程序結束,關閉瀏覽器。");
await browser.CloseAsync();
await UnXAPK.UnXAPKMain(args);
}
public static async Task<string> WaitForDownloadedFileAsync(string downloadDir, TimeSpan timeout){
var watch = Stopwatch.StartNew();
var initialFiles = Directory.GetFiles(downloadDir).ToHashSet();
while (watch.Elapsed < timeout)
{
var currentFiles = Directory.GetFiles(downloadDir).ToHashSet();
var newFiles = currentFiles.Except(initialFiles).ToList();
if (newFiles.Count > 0)
{
var newFile = newFiles[0];
bool isFileReady = false;
for (int i = 0; i < 10; i++)
{
try
{
using (var fileStream = File.Open(newFiles[0], FileMode.Open, FileAccess.Read, FileShare.None))
{
isFileReady = fileStream.Length > 0;
break;
}
}
catch (Exception)
{
await Task.Delay(1000);
}
if (isFileReady) break;
}
return newFile;
}
await Task.Delay(500);
}
return null;
}
}
}