wabbajack/Wabbajack.Lib/Downloaders/MediaFireDownloader.cs

100 lines
3.0 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;
2020-03-25 22:30:43 +00:00
using Wabbajack.Common;
2020-04-22 23:11:06 +00:00
using Wabbajack.Common.Serialization.Json;
2019-10-24 01:22:11 +00:00
using Wabbajack.Lib.Validation;
using Wabbajack.Lib.WebAutomation;
namespace Wabbajack.Lib.Downloaders
{
public class MediaFireDownloader : IUrlDownloader
{
public async Task<AbstractDownloadState?> GetDownloaderState(dynamic archiveINI, bool quickMode)
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.ToString());
2019-10-24 01:22:11 +00:00
}
2020-04-22 23:11:06 +00:00
[JsonName("MediaFireDownloader+State")]
2019-10-24 01:22:11 +00:00
public class State : AbstractDownloadState
{
public string Url { get; }
2019-10-24 01:22:11 +00:00
public override object[] PrimaryKey => new object[] { Url };
public State(string url)
{
Url = url;
}
2019-10-24 01:22:11 +00:00
public override bool IsWhitelisted(ServerWhitelist whitelist)
{
return whitelist.AllowedPrefixes.Any(p => Url.StartsWith(p));
2019-10-24 01:22:11 +00:00
}
2020-03-25 22:30:43 +00:00
public override async Task<bool> Download(Archive a, AbsolutePath destination)
2019-10-24 01:22:11 +00:00
{
var result = await Resolve();
if (result == null) return false;
return 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()
2019-10-24 01:22:11 +00:00
{
using (var d = await Driver.Create())
{
await d.NavigateTo(new Uri(Url));
2019-10-24 01:22:11 +00:00
// 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;
return new HTTPDownloader.State(newURL)
2019-10-24 01:22:11 +00:00
{
Client = new Common.Http.Client(),
2019-10-24 01:22:11 +00:00
};
}
}
public override IDownloader GetDownloader()
{
return DownloadDispatcher.GetInstance<MediaFireDownloader>();
}
public override string GetManifestURL(Archive a)
{
return 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)
2019-10-24 01:22:11 +00:00
{
var url = new Uri(u);
if (url.Host != "www.mediafire.com") return null;
return new State(url.ToString());
2019-10-24 01:22:11 +00:00
}
}
}