wabbajack/Wabbajack.Lib/Downloaders/MediaFireDownloader.cs

98 lines
2.9 KiB
C#
Raw Normal View History

2019-10-24 01:22:11 +00:00
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Wabbajack.Lib.Validation;
using Wabbajack.Lib.WebAutomation;
namespace Wabbajack.Lib.Downloaders
{
public class MediaFireDownloader : IUrlDownloader
{
2019-12-07 03:50:50 +00:00
public async Task<AbstractDownloadState> GetDownloaderState(dynamic archiveINI)
2019-10-24 01:22:11 +00:00
{
2019-11-21 15:51:57 +00:00
Uri url = DownloaderUtils.GetDirectURL(archiveINI);
2019-10-24 01:22:11 +00:00
if (url == null || url.Host != "www.mediafire.com") return null;
return new State
{
Url = url.ToString()
};
}
public class State : AbstractDownloadState
{
public string Url { get; set; }
public override object[] PrimaryKey { get => new object[] {Url};}
2019-10-24 01:22:11 +00:00
public override bool IsWhitelisted(ServerWhitelist whitelist)
{
return whitelist.AllowedPrefixes.Any(p => Url.StartsWith(p));
}
public override async Task Download(Archive a, string destination)
2019-10-24 01:22:11 +00:00
{
var result = await Resolve();
await result.Download(a, destination);
2019-10-24 01:22:11 +00:00
}
public override async Task<bool> Verify(Archive a)
2019-10-24 01:22:11 +00:00
{
return await Resolve() != null;
2019-10-24 01:22:11 +00:00
}
private async Task<HTTPDownloader.State> Resolve()
{
using (var d = await Driver.Create())
{
await d.NavigateTo(new Uri("http://www.mediafire.com/file/agiqzm1xwebczpx/WABBAJACK_TEST_FILE.tx"));
// MediaFire creates the link after all the JS loads
await Task.Delay(1000);
2019-11-21 15:51:57 +00:00
var newURL = await d.GetAttr("a.input", "href");
if (newURL == null || !newURL.StartsWith("http")) return null;
2019-10-24 01:22:11 +00:00
return new HTTPDownloader.State()
{
Client = new HttpClient(),
2019-11-21 15:51:57 +00:00
Url = newURL
2019-10-24 01:22:11 +00:00
};
}
}
public override IDownloader GetDownloader()
{
return DownloadDispatcher.GetInstance<MediaFireDownloader>();
}
public override string GetReportEntry(Archive a)
{
return $"* [{a.Name} - {Url}]({Url})";
}
public override string[] GetMetaIni()
{
return new []
{
"[General]",
$"directURL={Url}"
};
}
2019-10-24 01:22:11 +00:00
}
2019-12-07 02:45:13 +00:00
public async Task Prepare()
2019-10-24 01:22:11 +00:00
{
}
public AbstractDownloadState GetDownloaderState(string u)
{
var url = new Uri(u);
if (url.Host != "www.mediafire.com") return null;
return new State
{
Url = url.ToString()
};
}
}
}