wabbajack/Wabbajack.Lib/Downloaders/MediaFireDownloader.cs

104 lines
3.4 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;
2019-10-24 01:22:11 +00:00
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, CancellationToken? token)
2019-10-24 01:22:11 +00:00
{
return await Resolve(token) != null;
2019-10-24 01:22:11 +00:00
}
private async Task<HTTPDownloader.State?> Resolve(CancellationToken? token = null)
2019-10-24 01:22:11 +00:00
{
var client = new Http.Client();
var result = await client.GetAsync(Url, HttpCompletionOption.ResponseHeadersRead, token:token);
if (!result.IsSuccessStatusCode)
return null;
if (result.Content.Headers.ContentType.MediaType.StartsWith("text/html",
StringComparison.OrdinalIgnoreCase))
{
var body = await client.GetHtmlAsync(Url);
var node = body.DocumentNode.DescendantsAndSelf().First(d => d.HasClass("input") && d.HasClass("popsok") &&
d.GetAttributeValue("aria-label", "") == "Download file");
return new HTTPDownloader.State(node.GetAttributeValue("href", "not-found"));
}
return new HTTPDownloader.State(Url);
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
}
}
}