Fix Mediafire downloading

This commit is contained in:
Timothy Baldridge 2022-08-09 17:41:11 -06:00
parent 68f801a6c0
commit ee9b61be12
3 changed files with 22 additions and 8 deletions

View File

@ -28,12 +28,13 @@ public class DownloadUrl : IVerb
var command = new Command("download-url"); var command = new Command("download-url");
command.Add(new Option<Uri>(new[] {"-u", "-url"}, "Url to parse")); command.Add(new Option<Uri>(new[] {"-u", "-url"}, "Url to parse"));
command.Add(new Option<AbsolutePath>(new[] {"-o", "-output"}, "Output file")); command.Add(new Option<AbsolutePath>(new[] {"-o", "-output"}, "Output file"));
command.Add(new Option<bool>(new [] {"-p", "--proxy"}, "Use the Wabbajack Proxy (default: true)"));
command.Description = "Downloads a file to a given output"; command.Description = "Downloads a file to a given output";
command.Handler = CommandHandler.Create(Run); command.Handler = CommandHandler.Create(Run);
return command; return command;
} }
private async Task<int> Run(Uri url, AbsolutePath output) private async Task<int> Run(Uri url, AbsolutePath output, bool proxy = true)
{ {
var parsed = _dispatcher.Parse(url); var parsed = _dispatcher.Parse(url);
if (parsed == null) if (parsed == null)
@ -44,7 +45,8 @@ public class DownloadUrl : IVerb
} }
var archive = new Archive() {State = parsed, Name = output.FileName.ToString()}; var archive = new Archive() {State = parsed, Name = output.FileName.ToString()};
var hash = await _dispatcher.Download(archive, output, CancellationToken.None); ;
var hash = await _dispatcher.Download(archive, output, CancellationToken.None, proxy); ;
Console.WriteLine($"Download complete: {output.Size().ToFileSizeString()} {hash} {hash.ToHex()} {(long)hash}"); Console.WriteLine($"Download complete: {output.Size().ToFileSizeString()} {hash} {hash.ToHex()} {(long)hash}");
return 0; return 0;
} }

View File

@ -37,11 +37,11 @@ public class DownloadDispatcher
_useProxyCache = useProxyCache; _useProxyCache = useProxyCache;
} }
public async Task<Hash> Download(Archive a, AbsolutePath dest, CancellationToken token) public async Task<Hash> Download(Archive a, AbsolutePath dest, CancellationToken token, bool? proxy = null)
{ {
using var downloadScope = _logger.BeginScope("Downloading {Name}", a.Name); using var downloadScope = _logger.BeginScope("Downloading {Name}", a.Name);
using var job = await _limiter.Begin("Downloading " + a.Name, a.Size, token); using var job = await _limiter.Begin("Downloading " + a.Name, a.Size, token);
return await Download(a, dest, job, token); return await Download(a, dest, job, token, proxy);
} }
public async Task<Archive> MaybeProxy(Archive a, CancellationToken token) public async Task<Archive> MaybeProxy(Archive a, CancellationToken token)
@ -68,13 +68,13 @@ public class DownloadDispatcher
return a; return a;
} }
public async Task<Hash> Download(Archive a, AbsolutePath dest, Job<DownloadDispatcher> job, CancellationToken token) public async Task<Hash> Download(Archive a, AbsolutePath dest, Job<DownloadDispatcher> job, CancellationToken token, bool? useProxy = null)
{ {
if (!dest.Parent.DirectoryExists()) if (!dest.Parent.DirectoryExists())
dest.Parent.CreateDirectory(); dest.Parent.CreateDirectory();
var downloader = Downloader(a); var downloader = Downloader(a);
if (_useProxyCache && downloader is IProxyable p) if ((useProxy ?? _useProxyCache) && downloader is IProxyable p)
{ {
var uri = p.UnParse(a.State); var uri = p.UnParse(a.State);
var newUri = await _wjClient.MakeProxyUrl(a, uri); var newUri = await _wjClient.MakeProxyUrl(a, uri);

View File

@ -115,10 +115,22 @@ public class MediaFireDownloader : ADownloader<DTOs.DownloadStates.MediaFire>, I
await job.Report((int) job.Size, (CancellationToken) token); await job.Report((int) job.Size, (CancellationToken) token);
var body = new HtmlDocument(); var body = new HtmlDocument();
body.LoadHtml(bodyData); body.LoadHtml(bodyData);
var node = body.DocumentNode.DescendantsAndSelf().First(d => d.HasClass("input") && d.HasClass("popsok") && var node = body.DocumentNode.DescendantsAndSelf().FirstOrDefault(d => d.HasClass("input") && d.HasClass("popsok") &&
d.GetAttributeValue("aria-label", "") == d.GetAttributeValue("aria-label", "") ==
"Download file"); "Download file");
return new Uri(node.GetAttributeValue("href", "not-found")); if (node != null)
return new Uri(node.GetAttributeValue("href", "not-found"));
var startText = "window.location.href = '";
var start = body.DocumentNode.InnerHtml.IndexOf(startText, StringComparison.CurrentCultureIgnoreCase);
if (start != -1)
{
var end = body.DocumentNode.InnerHtml.IndexOf("\'", start + startText.Length,
StringComparison.CurrentCultureIgnoreCase);
var data = body.DocumentNode.InnerHtml[(start + startText.Length)..end];
return new Uri(data);
}
} }
return state.Url; return state.Url;