wabbajack/Wabbajack.Lib/Downloaders/ModDBDownloader.cs

77 lines
2.3 KiB
C#
Raw Normal View History

using System.Net.Http;
2019-10-12 22:31:07 +00:00
using System.Text.RegularExpressions;
using System.Threading.Tasks;
2019-10-12 22:31:07 +00:00
using Wabbajack.Common;
using Wabbajack.Lib.Validation;
2019-10-12 22:31:07 +00:00
namespace Wabbajack.Lib.Downloaders
2019-10-12 22:31:07 +00:00
{
public class ModDBDownloader : IDownloader, IUrlDownloader
2019-10-12 22:31:07 +00:00
{
2019-12-07 03:50:50 +00:00
public async Task<AbstractDownloadState> GetDownloaderState(dynamic archiveINI)
2019-10-12 22:31:07 +00:00
{
2019-11-21 15:51:57 +00:00
var url = archiveINI?.General?.directURL;
return GetDownloaderState(url);
}
2019-10-12 22:31:07 +00:00
public AbstractDownloadState GetDownloaderState(string url)
{
2019-10-12 22:31:07 +00:00
if (url != null && url.StartsWith("https://www.moddb.com/downloads/start"))
{
return new State
{
Url = url
};
}
return null;
}
2019-12-07 02:45:13 +00:00
public async Task Prepare()
2019-10-12 22:31:07 +00:00
{
}
public class State : AbstractDownloadState
{
public string Url { get; set; }
public override bool IsWhitelisted(ServerWhitelist whitelist)
{
// Everything from Moddb is whitelisted
return true;
}
public override async Task Download(Archive a, string destination)
2019-10-12 22:31:07 +00:00
{
2019-12-06 05:59:57 +00:00
var newURL = await GetDownloadUrl();
await new HTTPDownloader.State {Url = newURL}.Download(a, destination);
2019-10-12 22:31:07 +00:00
}
2019-12-06 05:59:57 +00:00
private async Task<string> GetDownloadUrl()
2019-10-12 22:31:07 +00:00
{
var client = new HttpClient();
2019-12-06 05:59:57 +00:00
var result = await client.GetStringAsync(Url);
2019-10-12 22:31:07 +00:00
var regex = new Regex("https:\\/\\/www\\.moddb\\.com\\/downloads\\/mirror\\/.*(?=\\\")");
var match = regex.Match(result);
2019-11-21 15:51:57 +00:00
var newURL = match.Value;
return newURL;
2019-10-12 22:31:07 +00:00
}
public override async Task<bool> Verify()
2019-10-12 22:31:07 +00:00
{
2019-12-06 05:59:57 +00:00
var newURL = await GetDownloadUrl();
return await new HTTPDownloader.State { Url = newURL }.Verify();
2019-10-12 22:31:07 +00:00
}
public override IDownloader GetDownloader()
{
return DownloadDispatcher.GetInstance<ModDBDownloader>();
}
public override string GetReportEntry(Archive a)
{
return $"* ModDB - [{a.Name}]({Url})";
}
2019-10-12 22:31:07 +00:00
}
}
}