Skip to content

Instantly share code, notes, and snippets.

@QuiltMeow
Created October 31, 2024 17:05
Show Gist options
  • Select an option

  • Save QuiltMeow/5c4a989d6d405a2b794b55b76e2169d6 to your computer and use it in GitHub Desktop.

Select an option

Save QuiltMeow/5c4a989d6d405a2b794b55b76e2169d6 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading;
namespace WebClientDownloader
{
public static class Program
{
private const string DOWNLOAD_FOLDER = "download/";
private static readonly int THREAD_COUNT = Environment.ProcessorCount;
private static readonly ISet<string> fileNameSet = new HashSet<string>();
private static int downloadCount;
private static int totalCount;
private static string getFileName(string url)
{
string fileName = Path.GetFileName(new Uri(url).LocalPath);
string ret = fileName;
int count = 1;
while (fileNameSet.Contains(ret))
{
ret = fileName + " (" + count++ + ")";
}
return ret;
}
private static void downloadFile(string url)
{
string fileName = getFileName(url);
try
{
using (WebClient client = new WebClient())
{
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36");
client.DownloadFile(url, DOWNLOAD_FOLDER + fileName);
Console.WriteLine("檔案 " + fileName + " (" + url + ") 下載完成 (" + ++downloadCount + " / " + totalCount + ")");
}
}
catch (Exception ex)
{
Console.Error.WriteLine("下載檔案 " + fileName + " (" + url + ") 時發生例外狀況,即將重新下載 : " + ex.Message);
downloadFileAsync(url);
}
}
private static void downloadFileAsync(string url)
{
ThreadPool.QueueUserWorkItem(callback =>
{
downloadFile(url);
});
}
public static bool isValidHTTPURL(string url)
{
Uri uri;
if (!Uri.TryCreate(url, UriKind.Absolute, out uri))
{
return false;
}
string scheme = uri.Scheme;
return scheme == Uri.UriSchemeHttp || scheme == Uri.UriSchemeHttps;
}
private static void pause()
{
Console.Write("請按任意鍵繼續 ...");
Console.ReadKey(true);
}
public static void Main(string[] argument)
{
if (argument.Length <= 0)
{
Console.WriteLine("請指定下載網址檔案");
pause();
return;
}
IList<string> urlList = new List<string>();
try
{
using (FileStream fs = new FileStream(argument[0], FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader(fs))
{
string url;
while ((url = sr.ReadLine()) != null)
{
if (!isValidHTTPURL(url))
{
continue;
}
urlList.Add(url);
}
}
}
totalCount = urlList.Count;
}
catch (Exception ex)
{
Console.Error.WriteLine("讀取檔案時發生例外狀況 : " + ex.Message);
pause();
return;
}
Directory.CreateDirectory(DOWNLOAD_FOLDER);
ThreadPool.SetMaxThreads(THREAD_COUNT, THREAD_COUNT);
Console.WriteLine("下載正在進行中 ...");
foreach (string url in urlList)
{
downloadFileAsync(url);
}
Console.Write("按下 Enter 鍵終止執行");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment