Skip to content

Instantly share code, notes, and snippets.

@DartPower
Last active April 2, 2023 14:10
Show Gist options
  • Save DartPower/95abb867ff5477cf5b026b9e0c7b1964 to your computer and use it in GitHub Desktop.
Save DartPower/95abb867ff5477cf5b026b9e0c7b1964 to your computer and use it in GitHub Desktop.
SporeCreatureDownload.cs
using System;
using System.Net;
namespace SporeCreatureDownload
{
public class Program
{
static void WriteLog(string message, ConsoleColor color)
{
Console.Write("[{0:yyyy-MM-dd HH:mm:ss}] ", DateTime.Now);
Console.ForegroundColor = color;
Console.Write(message);
Console.ResetColor();
Console.WriteLine();
}
static readonly string baseURL = "http://static.spore.com/static/thumb/501/";
static readonly string downloadDir = "test";
static async Task Main(string[] args)
{
await DownloadImages();
}
static async Task DownloadImages()
{
int countOut = 0;
int countIn = 0;
int image = 0;
double progressValue = 0;
while (countOut < 36)
{
string a = ThreeDigits(countOut);
while (countIn < 1000)
{
string b = ThreeDigits(countIn);
while (image < 1000)
{
string c = ThreeDigits(image);
string imageURL = string.Format("{0}{1}/{2}/501{1}{2}{3}.png", baseURL, a, b, c);
MakeDir(downloadDir + '/' + a + '/' + b);
string filePath = Path.Combine(downloadDir, a, b, $"501{a}{b}{c}.png");
if (File.Exists(filePath))
{
WriteLog($"100% {imageURL} Skipped", ConsoleColor.Yellow);
progressValue = (double)(countOut * 1000000 + countIn * 1000 + image) / 36000000;
}
else
{
try
{
using (var client = new WebClient())
{
byte[] data = await client.DownloadDataTaskAsync(imageURL);
if (data.Length > 0)
{
File.WriteAllBytes(filePath, data);
WriteLog($"100% {imageURL} Downloaded", ConsoleColor.Green);
progressValue = (double)(countOut * 1000000 + countIn * 1000 + image) / 36000000;
}
else
{
WriteLog($"0% {imageURL} Missing", ConsoleColor.Red);
}
}
}
catch (WebException)
{
WriteLog($"0% {imageURL} Missing", ConsoleColor.Red);
}
}
image++;
}
image = 0;
countIn++;
}
countIn = 0;
countOut++;
}
Console.WriteLine("All images downloaded successfully!");
}
static void MakeDir(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
static string ThreeDigits(int n)
{
return n.ToString("D3");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment