Last active
October 4, 2024 04:03
-
-
Save nor0x/5a906e2fd28aa3a202a4565bc3366646 to your computer and use it in GitHub Desktop.
download all images from MKBHD's https://panels.art
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//downloads all image data from https://storage.googleapis.com/panels-cdn/ | |
//add auth to your buckets guys 🧐 | |
using System.Text.Json; | |
using System.IO ; | |
var allUrl = "https://storage.googleapis.com/panels-cdn/data/20240730/all.json"; | |
var json = await new System.Net.Http.HttpClient().GetStringAsync(allUrl); | |
List<string> urls = new(); | |
var doc = JsonDocument.Parse(json); | |
ExtractUrls(doc.RootElement, urls); | |
Console.WriteLine($"found {urls.Count} urls"); | |
if(!Directory.Exists("downloads")) | |
{ | |
Directory.CreateDirectory("downloads"); | |
} | |
var options = new ParallelOptions { MaxDegreeOfParallelism = 10 }; | |
Parallel.ForEach(urls, options, url => | |
{ | |
var fileName = Path.GetFileName(url); | |
var filePath = Path.Combine("downloads", fileName); | |
if (!File.Exists(filePath)) | |
{ | |
Console.WriteLine($"downloading {url}"); | |
using var client = new System.Net.Http.HttpClient(); | |
var data = client.GetByteArrayAsync(url).Result; | |
File.WriteAllBytes(filePath, data); | |
} | |
else | |
{ | |
Console.WriteLine($"skipping {url}"); | |
} | |
}); | |
static void ExtractUrls(JsonElement element, List<string> urls) | |
{ | |
if (element.ValueKind == JsonValueKind.Object) | |
{ | |
foreach (var property in element.EnumerateObject()) | |
{ | |
if (property.Name == "url") | |
{ | |
urls.Add(property.Value.GetString()); | |
} | |
else | |
{ | |
ExtractUrls(property.Value, urls); | |
} | |
} | |
} | |
else if (element.ValueKind == JsonValueKind.Array) | |
{ | |
foreach (var item in element.EnumerateArray()) | |
{ | |
ExtractUrls(item, urls); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment