Skip to content

Instantly share code, notes, and snippets.

@KristofferK
Last active December 5, 2019 08:30
Show Gist options
  • Save KristofferK/054cefd42a17e382c4b0fdcba8707e2d to your computer and use it in GitHub Desktop.
Save KristofferK/054cefd42a17e382c4b0fdcba8707e2d to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
namespace WowDownloaderOmegalul
{
class Program
{
private static CookieAwareWebClient client;
static void Main(string[] args)
{
client = GenerateWebClientWithHeaders();
var dbcs = GetDBCs();
foreach (var dbc in dbcs)
{
Console.WriteLine(dbc);
var versions = GetVersionsForDBC(dbc);
foreach (var version in versions)
{
Download(dbc, version);
}
}
}
private static CookieAwareWebClient GenerateWebClientWithHeaders()
{
var client = new CookieAwareWebClient();
client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36";
client.Headers[HttpRequestHeader.Accept] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
client.Headers[HttpRequestHeader.AcceptEncoding] = "identity";
client.Headers[HttpRequestHeader.Referer] = "https://wow.tools/dbc/";
client.Headers.Add("sec-fetch-mode", "navigate");
client.Headers.Add("sec-fetch-site", "none");
client.Headers.Add("sec-fetch-user", "?1");
client.Headers.Add("upgrade-insecure-requests", "1");
return client;
}
private static List<DBC> GetDBCs()
{
var source = client.DownloadString("https://wow.tools/dbc/");
var pattern = "<option value='([^']+)' >([^<]+)";
return Regex.Matches(source, pattern)
.OfType<Match>()
.Select(e => new DBC
{
Value = e.Groups[1].Value,
Title = e.Groups[2].Value
})
.ToList();
}
private static List<string> GetVersionsForDBC(DBC dbc)
{
var source = client.DownloadString("https://wow.tools/dbc/?dbc=" + dbc.Value);
var pattern = "<option value='([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)'";
return Regex.Matches(source, pattern).OfType<Match>().Select(e => e.Groups[1].Value).ToList();
}
private static void Download(DBC dbc, string version)
{
var directory = Path.GetTempPath() + "\\WoW-downloads\\" + dbc.Title;
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var url = $"https://wow.tools/dbc/api/export/?name={dbc.Value}&build={version}";
var filename = $"{directory}\\{version}.csv";
if (!File.Exists(filename))
{
using (var client = GenerateWebClientWithHeaders())
{
client.DownloadFileAsync(new Uri(url), filename);
}
}
}
}
class DBC
{
public string Value { get; set; }
public string Title { get; set; }
public override string ToString()
{
return $"{Title} ({Value})";
}
}
class CookieAwareWebClient : WebClient
{
public CookieAwareWebClient()
{
CookieContainer = new CookieContainer();
}
public CookieContainer CookieContainer { get; private set; }
protected override WebRequest GetWebRequest(Uri address)
{
var request = (HttpWebRequest)base.GetWebRequest(address);
request.CookieContainer = CookieContainer;
return request;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment