2020-01-06 00:21:05 +00:00
|
|
|
|
using System;
|
2020-08-12 22:23:02 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-01-06 00:21:05 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
2020-05-21 22:12:51 +00:00
|
|
|
|
using System.Net.Http;
|
2020-01-06 00:21:05 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2020-12-31 06:44:42 +00:00
|
|
|
|
using System.Threading;
|
2020-01-06 00:21:05 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Web;
|
2020-08-12 22:23:02 +00:00
|
|
|
|
using F23.StringSimilarity;
|
|
|
|
|
using HtmlAgilityPack;
|
2020-01-06 00:21:05 +00:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Wabbajack.Common;
|
2020-12-29 23:15:47 +00:00
|
|
|
|
using Wabbajack.Lib.LibCefHelpers;
|
2020-01-06 00:21:05 +00:00
|
|
|
|
using Wabbajack.Lib.Validation;
|
2020-12-31 06:44:42 +00:00
|
|
|
|
using Wabbajack.Lib.WebAutomation;
|
2020-01-06 00:21:05 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Lib.Downloaders
|
|
|
|
|
{
|
2020-12-31 06:44:42 +00:00
|
|
|
|
|
|
|
|
|
interface IWaitForWindowDownloader
|
|
|
|
|
{
|
|
|
|
|
public Task WaitForNextRequestWindow();
|
|
|
|
|
}
|
2020-01-06 00:21:05 +00:00
|
|
|
|
// IPS4 is the site used by LoversLab, VectorPlexus, etc. the general mechanics of each site are the
|
|
|
|
|
// same, so we can fairly easily abstract the state.
|
|
|
|
|
// Pass in the state type via TState
|
2020-12-31 06:44:42 +00:00
|
|
|
|
public abstract class AbstractIPS4Downloader<TDownloader, TState> : AbstractNeedsLoginDownloader, IDownloader, IWaitForWindowDownloader
|
2020-03-04 12:10:49 +00:00
|
|
|
|
where TState : AbstractIPS4Downloader<TDownloader, TState>.State<TDownloader>, new()
|
2020-01-06 00:21:05 +00:00
|
|
|
|
where TDownloader : IDownloader
|
|
|
|
|
{
|
2020-12-29 23:15:47 +00:00
|
|
|
|
|
2020-12-31 06:44:42 +00:00
|
|
|
|
private DateTime LastRequestTime = default;
|
|
|
|
|
protected long RequestsPerMinute = 20;
|
|
|
|
|
private TimeSpan RequestDelay => TimeSpan.FromMinutes(1) / RequestsPerMinute;
|
|
|
|
|
|
2020-05-28 23:44:58 +00:00
|
|
|
|
protected AbstractIPS4Downloader(Uri loginUri, string encryptedKeyName, string cookieDomain, string loginCookie = "ips4_member_id")
|
|
|
|
|
: base(loginUri, encryptedKeyName, cookieDomain, loginCookie)
|
2020-04-09 20:20:34 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2020-01-22 19:57:48 +00:00
|
|
|
|
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public async Task<AbstractDownloadState?> GetDownloaderState(dynamic archiveINI, bool quickMode)
|
2020-08-12 22:23:02 +00:00
|
|
|
|
{
|
2020-01-06 00:21:05 +00:00
|
|
|
|
Uri url = DownloaderUtils.GetDirectURL(archiveINI);
|
2020-08-12 22:23:02 +00:00
|
|
|
|
return await GetDownloaderStateFromUrl(url, quickMode);
|
|
|
|
|
}
|
2020-01-06 00:21:05 +00:00
|
|
|
|
|
2020-12-31 06:44:42 +00:00
|
|
|
|
public async Task WaitForNextRequestWindow()
|
|
|
|
|
{
|
|
|
|
|
TimeSpan delay;
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
if (LastRequestTime < DateTime.UtcNow - RequestDelay)
|
|
|
|
|
{
|
|
|
|
|
LastRequestTime = DateTime.UtcNow;
|
|
|
|
|
delay = TimeSpan.Zero;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LastRequestTime += RequestDelay;
|
|
|
|
|
delay = LastRequestTime - DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils.Log($"Waiting for {delay.TotalSeconds} to make request via {typeof(TDownloader).Name}");
|
|
|
|
|
await Task.Delay(delay);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 22:23:02 +00:00
|
|
|
|
public async Task<AbstractDownloadState?> GetDownloaderStateFromUrl(Uri url, bool quickMode)
|
|
|
|
|
{
|
2020-01-27 17:16:44 +00:00
|
|
|
|
var absolute = true;
|
|
|
|
|
if (url == null || url.Host != SiteURL.Host) return null;
|
2020-04-04 17:40:58 +00:00
|
|
|
|
|
|
|
|
|
if (url.PathAndQuery.StartsWith("/applications/core/interface/file/attachment"))
|
|
|
|
|
{
|
|
|
|
|
return new TState
|
|
|
|
|
{
|
|
|
|
|
IsAttachment = true,
|
|
|
|
|
FullURL = url.ToString()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 22:14:59 +00:00
|
|
|
|
if (url.PathAndQuery.StartsWith("/index.php?"))
|
|
|
|
|
{
|
|
|
|
|
var id2 = HttpUtility.ParseQueryString(url.Query)["r"];
|
|
|
|
|
var parsed = HttpUtility.ParseQueryString(url.Query);
|
2021-01-01 00:06:56 +00:00
|
|
|
|
var name = parsed[null]!.Split("/", StringSplitOptions.RemoveEmptyEntries).Last();
|
2020-02-11 22:14:59 +00:00
|
|
|
|
return new TState
|
|
|
|
|
{
|
2020-04-04 17:40:58 +00:00
|
|
|
|
FullURL = url.AbsolutePath,
|
2021-01-01 00:06:56 +00:00
|
|
|
|
FileID = id2!,
|
2020-02-11 22:14:59 +00:00
|
|
|
|
FileName = name
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-05-28 23:44:58 +00:00
|
|
|
|
|
|
|
|
|
if (url.PathAndQuery.StartsWith("/files/getdownload"))
|
|
|
|
|
{
|
|
|
|
|
return new TState
|
|
|
|
|
{
|
|
|
|
|
FullURL = url.ToString(),
|
|
|
|
|
IsAttachment = true
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-12-16 00:09:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (url.PathAndQuery.StartsWith("/files/download/") && long.TryParse(url.PathAndQuery.Split("/").Last(), out var fileId))
|
|
|
|
|
{
|
|
|
|
|
return new TState
|
|
|
|
|
{
|
|
|
|
|
FullURL = url.ToString(),
|
|
|
|
|
IsAttachment = true
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-02-11 22:14:59 +00:00
|
|
|
|
|
2020-01-27 17:16:44 +00:00
|
|
|
|
if (!url.PathAndQuery.StartsWith("/files/file/"))
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(url.Query)) return null;
|
|
|
|
|
if (!url.Query.Substring(1).StartsWith("/files/file/")) return null;
|
|
|
|
|
absolute = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 00:09:59 +00:00
|
|
|
|
|
2020-01-27 17:16:44 +00:00
|
|
|
|
var id = HttpUtility.ParseQueryString(url.Query)["r"];
|
|
|
|
|
var file = absolute
|
|
|
|
|
? url.AbsolutePath.Split('/').Last(s => s != "")
|
|
|
|
|
: url.Query.Substring(1).Split('/').Last(s => s != "");
|
|
|
|
|
|
2020-01-06 00:21:05 +00:00
|
|
|
|
return new TState
|
|
|
|
|
{
|
2020-04-04 17:40:58 +00:00
|
|
|
|
FullURL = url.AbsolutePath,
|
2021-01-01 00:06:56 +00:00
|
|
|
|
FileID = id!,
|
2020-01-06 00:21:05 +00:00
|
|
|
|
FileName = file
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-08-12 22:23:02 +00:00
|
|
|
|
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public class State<TStateDownloader> : AbstractDownloadState, IMetaState
|
|
|
|
|
where TStateDownloader : IDownloader
|
2020-01-06 00:21:05 +00:00
|
|
|
|
{
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public string FullURL { get; set; } = string.Empty;
|
2020-04-04 17:40:58 +00:00
|
|
|
|
public bool IsAttachment { get; set; }
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public string FileID { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
public string FileName { get; set; } = string.Empty;
|
2020-03-26 23:02:15 +00:00
|
|
|
|
|
|
|
|
|
// from IMetaState
|
2020-04-23 10:38:43 +00:00
|
|
|
|
public Uri URL => IsAttachment ? new Uri("https://www.wabbajack.org/") : new Uri($"{Site}/files/file/{FileName}");
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public string? Name { get; set; }
|
|
|
|
|
public string? Author { get; set; }
|
|
|
|
|
public string? Version { get; set; }
|
2020-04-15 12:05:05 +00:00
|
|
|
|
public Uri? ImageURL { get; set; }
|
2020-03-26 23:02:15 +00:00
|
|
|
|
public virtual bool IsNSFW { get; set; }
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public string? Description { get; set; }
|
2020-01-06 00:21:05 +00:00
|
|
|
|
|
2020-01-22 19:57:48 +00:00
|
|
|
|
private static bool IsHTTPS => Downloader.SiteURL.AbsolutePath.StartsWith("https://");
|
|
|
|
|
private static string URLPrefix => IsHTTPS ? "https://" : "http://";
|
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
[JsonIgnore]
|
2020-03-04 12:10:49 +00:00
|
|
|
|
public static string Site => string.IsNullOrWhiteSpace(Downloader.SiteURL.Query)
|
2020-01-27 17:16:44 +00:00
|
|
|
|
? $"{URLPrefix}{Downloader.SiteURL.Host}"
|
|
|
|
|
: Downloader.SiteURL.ToString();
|
|
|
|
|
|
2020-03-04 12:10:49 +00:00
|
|
|
|
public static AbstractNeedsLoginDownloader Downloader => (AbstractNeedsLoginDownloader)(object)DownloadDispatcher.GetInstance<TDownloader>();
|
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
[JsonIgnore]
|
2020-01-11 04:15:53 +00:00
|
|
|
|
public override object[] PrimaryKey
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-08-24 22:20:50 +00:00
|
|
|
|
return string.IsNullOrWhiteSpace(FileID)
|
2020-04-04 17:40:58 +00:00
|
|
|
|
? IsAttachment
|
|
|
|
|
? new object[] {Downloader.SiteURL, IsAttachment, FullURL}
|
|
|
|
|
: new object[] {Downloader.SiteURL, FileName}
|
2020-01-27 17:16:44 +00:00
|
|
|
|
: new object[] {Downloader.SiteURL, FileName, FileID};
|
2020-01-11 04:15:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-06 00:21:05 +00:00
|
|
|
|
|
|
|
|
|
public override bool IsWhitelisted(ServerWhitelist whitelist)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 22:30:43 +00:00
|
|
|
|
public override async Task<bool> Download(Archive a, AbsolutePath destination)
|
2020-01-06 00:21:05 +00:00
|
|
|
|
{
|
2020-12-31 06:44:42 +00:00
|
|
|
|
await ((IWaitForWindowDownloader)Downloader).WaitForNextRequestWindow();
|
2020-12-29 23:15:47 +00:00
|
|
|
|
return await ResolveDownloadStream(a, destination, false);
|
2020-01-06 00:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 06:44:42 +00:00
|
|
|
|
private async Task<bool> ResolveDownloadStream(Archive a, AbsolutePath path, bool quickMode, CancellationToken? token = null)
|
2020-01-06 00:21:05 +00:00
|
|
|
|
{
|
2020-12-29 23:15:47 +00:00
|
|
|
|
|
|
|
|
|
|
2020-01-06 00:21:05 +00:00
|
|
|
|
TOP:
|
2020-04-04 17:40:58 +00:00
|
|
|
|
string url;
|
|
|
|
|
if (IsAttachment)
|
|
|
|
|
{
|
|
|
|
|
url = FullURL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-08-25 01:34:57 +00:00
|
|
|
|
var csrfURL = string.IsNullOrWhiteSpace(FileID)
|
2020-04-04 17:40:58 +00:00
|
|
|
|
? $"{Site}/files/file/{FileName}/?do=download"
|
|
|
|
|
: $"{Site}/files/file/{FileName}/?do=download&r={FileID}";
|
2020-12-31 06:44:42 +00:00
|
|
|
|
var html = await GetStringAsync(new Uri(csrfURL), token);
|
2020-01-06 00:21:05 +00:00
|
|
|
|
|
2020-04-04 17:40:58 +00:00
|
|
|
|
var pattern = new Regex("(?<=csrfKey=).*(?=[&\"\'])|(?<=csrfKey: \").*(?=[&\"\'])");
|
|
|
|
|
var matches = pattern.Matches(html).Cast<Match>();
|
2020-01-06 00:21:05 +00:00
|
|
|
|
|
|
|
|
|
var csrfKey = matches.Where(m => m.Length == 32).Select(m => m.ToString()).FirstOrDefault();
|
|
|
|
|
|
2020-12-30 00:05:19 +00:00
|
|
|
|
if (!Downloader.IsCloudFlareProtected && csrfKey == null)
|
2020-08-25 01:34:57 +00:00
|
|
|
|
{
|
|
|
|
|
Utils.Log($"Returning null from IPS4 Downloader because no csrfKey was found");
|
2020-12-29 23:15:47 +00:00
|
|
|
|
return false;
|
2020-08-25 01:34:57 +00:00
|
|
|
|
}
|
2020-01-06 00:21:05 +00:00
|
|
|
|
|
2020-04-04 17:40:58 +00:00
|
|
|
|
var sep = Site.EndsWith("?") ? "&" : "?";
|
2020-12-31 06:44:42 +00:00
|
|
|
|
url = FileID == null
|
|
|
|
|
? $"{Site}/files/file/{FileName}/{sep}do=download&confirm=1&t=1&csrfKey={csrfKey}"
|
|
|
|
|
: $"{Site}/files/file/{FileName}/{sep}do=download&r={FileID}&confirm=1&t=1&csrfKey={csrfKey}";
|
2020-04-04 17:40:58 +00:00
|
|
|
|
}
|
2020-12-29 23:15:47 +00:00
|
|
|
|
|
|
|
|
|
if (Downloader.IsCloudFlareProtected)
|
|
|
|
|
{
|
|
|
|
|
using var driver = await Downloader.GetAuthedDriver();
|
2020-12-31 06:44:42 +00:00
|
|
|
|
var size = await driver.NavigateToAndDownload(new Uri(url), path, quickMode: quickMode, token);
|
2020-12-29 23:15:47 +00:00
|
|
|
|
|
|
|
|
|
if (a.Size == 0 || size == 0 || a.Size == size) return true;
|
2020-01-06 00:21:05 +00:00
|
|
|
|
|
2020-12-29 23:15:47 +00:00
|
|
|
|
Utils.Log($"Bad Header Content sizes {a.Size} vs {size}");
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var streamResult = await GetDownloadAsync(new Uri(url));
|
2020-01-06 00:21:05 +00:00
|
|
|
|
if (streamResult.StatusCode != HttpStatusCode.OK)
|
|
|
|
|
{
|
2020-03-04 12:10:49 +00:00
|
|
|
|
Utils.ErrorThrow(new InvalidOperationException(), $"{Downloader.SiteName} servers reported an error for file: {FileID}");
|
2020-01-06 00:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-27 17:16:44 +00:00
|
|
|
|
var contentType = streamResult.Content.Headers.ContentType;
|
2020-01-06 00:21:05 +00:00
|
|
|
|
|
2021-01-01 00:06:56 +00:00
|
|
|
|
if (contentType!.MediaType != "application/json")
|
2020-04-24 01:58:18 +00:00
|
|
|
|
{
|
|
|
|
|
var headerVar = a.Size == 0 ? "1" : a.Size.ToString();
|
|
|
|
|
long headerContentSize = 0;
|
|
|
|
|
if (streamResult.Content.Headers.Contains("Content-Length"))
|
|
|
|
|
{
|
|
|
|
|
headerVar = streamResult.Content.Headers.GetValues("Content-Length").FirstOrDefault();
|
|
|
|
|
if (headerVar != null)
|
|
|
|
|
long.TryParse(headerVar, out headerContentSize);
|
|
|
|
|
}
|
2020-08-26 04:03:43 +00:00
|
|
|
|
|
2020-08-25 01:34:57 +00:00
|
|
|
|
if (a.Size != 0 && headerContentSize != 0 && a.Size != headerContentSize)
|
|
|
|
|
{
|
|
|
|
|
Utils.Log($"Bad Header Content sizes {a.Size} vs {headerContentSize}");
|
2020-12-29 23:15:47 +00:00
|
|
|
|
return false;
|
2020-08-25 01:34:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 23:15:47 +00:00
|
|
|
|
await using (var os = await path.Create())
|
|
|
|
|
await using (var ins = await streamResult.Content.ReadAsStreamAsync())
|
|
|
|
|
{
|
|
|
|
|
if (a.Size == 0)
|
|
|
|
|
{
|
|
|
|
|
Utils.Status($"Downloading {a.Name}");
|
|
|
|
|
await ins.CopyToAsync(os);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await ins.CopyToWithStatusAsync(headerContentSize, os, $"Downloading {a.Name}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
streamResult.Dispose();
|
|
|
|
|
|
|
|
|
|
return true;
|
2020-04-24 01:58:18 +00:00
|
|
|
|
}
|
2020-01-27 17:16:44 +00:00
|
|
|
|
|
|
|
|
|
// Sometimes LL hands back a json object telling us to wait until a certain time
|
2020-04-06 20:48:54 +00:00
|
|
|
|
var times = (await streamResult.Content.ReadAsStringAsync()).FromJsonString<WaitResponse>();
|
2020-01-27 17:16:44 +00:00
|
|
|
|
var secs = times.Download - times.CurrentTime;
|
|
|
|
|
for (int x = 0; x < secs; x++)
|
2020-01-06 00:21:05 +00:00
|
|
|
|
{
|
2020-12-29 23:15:47 +00:00
|
|
|
|
if (quickMode) return true;
|
2020-03-04 12:10:49 +00:00
|
|
|
|
Utils.Status($"Waiting for {secs} at the request of {Downloader.SiteName}", Percent.FactoryPutInRange(x, secs));
|
2020-12-29 23:15:47 +00:00
|
|
|
|
Utils.Log($"Waiting for {secs} at the request of {Downloader.SiteName}, {secs - x} remaining");
|
2020-01-27 17:16:44 +00:00
|
|
|
|
await Task.Delay(1000);
|
2020-01-06 00:21:05 +00:00
|
|
|
|
}
|
2020-02-26 05:05:33 +00:00
|
|
|
|
streamResult.Dispose();
|
2020-01-27 17:16:44 +00:00
|
|
|
|
Utils.Status("Retrying download");
|
|
|
|
|
goto TOP;
|
2020-01-06 00:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 06:44:42 +00:00
|
|
|
|
private async Task DeleteOldDownloadCookies(Driver driver)
|
|
|
|
|
{
|
|
|
|
|
await driver.DeleteCookiesWhere(c => c.Name.StartsWith("ips4_downloads_delay_") && c.Value == "-1");
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-06 00:21:05 +00:00
|
|
|
|
private class WaitResponse
|
|
|
|
|
{
|
|
|
|
|
[JsonProperty("download")]
|
|
|
|
|
public int Download { get; set; }
|
|
|
|
|
[JsonProperty("currentTime")]
|
|
|
|
|
public int CurrentTime { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 06:44:42 +00:00
|
|
|
|
public override async Task<bool> Verify(Archive a, CancellationToken? token)
|
2020-01-06 00:21:05 +00:00
|
|
|
|
{
|
2020-12-31 06:44:42 +00:00
|
|
|
|
await ((IWaitForWindowDownloader)Downloader).WaitForNextRequestWindow();
|
2020-12-29 23:15:47 +00:00
|
|
|
|
await using var tp = new TempFile();
|
2020-12-31 06:44:42 +00:00
|
|
|
|
var isValid = await ResolveDownloadStream(a, tp.Path, true, token: token);
|
2020-12-29 23:15:47 +00:00
|
|
|
|
return isValid;
|
2020-01-06 00:21:05 +00:00
|
|
|
|
}
|
2020-08-12 22:23:02 +00:00
|
|
|
|
|
2020-01-06 00:21:05 +00:00
|
|
|
|
public override IDownloader GetDownloader()
|
|
|
|
|
{
|
|
|
|
|
return DownloadDispatcher.GetInstance<TDownloader>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 22:23:02 +00:00
|
|
|
|
public override async Task<(Archive? Archive, TempFile NewFile)> FindUpgrade(Archive a, Func<Archive, Task<AbsolutePath>> downloadResolver)
|
|
|
|
|
{
|
2020-12-31 06:44:42 +00:00
|
|
|
|
await ((IWaitForWindowDownloader)Downloader).WaitForNextRequestWindow();
|
2020-08-12 22:23:02 +00:00
|
|
|
|
var files = await GetFilesInGroup();
|
|
|
|
|
var nl = new Levenshtein();
|
|
|
|
|
|
2020-08-24 22:20:50 +00:00
|
|
|
|
foreach (var newFile in files.OrderBy(f => nl.Distance(a.Name.ToLowerInvariant(), f.Name.ToLowerInvariant())))
|
2020-08-12 22:23:02 +00:00
|
|
|
|
{
|
2020-08-26 04:03:43 +00:00
|
|
|
|
/*
|
2020-08-24 22:20:50 +00:00
|
|
|
|
var existing = await downloadResolver(newFile);
|
2020-08-26 04:03:43 +00:00
|
|
|
|
if (existing != default) return (newFile, new TempFile());*/
|
2020-08-24 22:20:50 +00:00
|
|
|
|
|
|
|
|
|
var tmp = new TempFile();
|
|
|
|
|
await DownloadDispatcher.PrepareAll(new[] {newFile.State});
|
|
|
|
|
if (await newFile.State.Download(newFile, tmp.Path))
|
|
|
|
|
{
|
|
|
|
|
newFile.Size = tmp.Path.Size;
|
2021-01-09 19:04:11 +00:00
|
|
|
|
var tmpHash = await tmp.Path.FileHashAsync();
|
|
|
|
|
if (tmpHash == null) return default;
|
|
|
|
|
newFile.Hash = tmpHash.Value;
|
2020-08-24 22:20:50 +00:00
|
|
|
|
return (newFile, tmp);
|
|
|
|
|
}
|
2020-08-12 22:23:02 +00:00
|
|
|
|
|
2020-08-24 22:20:50 +00:00
|
|
|
|
await tmp.DisposeAsync();
|
|
|
|
|
}
|
2020-08-12 22:23:02 +00:00
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 01:34:57 +00:00
|
|
|
|
public override async Task<bool> ValidateUpgrade(Hash srcHash, AbstractDownloadState newArchiveState)
|
|
|
|
|
{
|
|
|
|
|
return !string.IsNullOrWhiteSpace(FileID);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 22:23:02 +00:00
|
|
|
|
public async Task<List<Archive>> GetFilesInGroup()
|
|
|
|
|
{
|
2020-12-31 06:44:42 +00:00
|
|
|
|
var token = new CancellationTokenSource();
|
|
|
|
|
token.CancelAfter(TimeSpan.FromMinutes(10));
|
|
|
|
|
|
|
|
|
|
var others = await GetHtmlAsync(new Uri($"{Site}/files/file/{FileName}?do=download"), token.Token);
|
2020-08-12 22:23:02 +00:00
|
|
|
|
|
|
|
|
|
var pairs = others.DocumentNode.SelectNodes("//a[@data-action='download']")
|
|
|
|
|
.Select(item => (item.GetAttributeValue("href", ""),
|
|
|
|
|
item.ParentNode.ParentNode.SelectNodes("//div//h4//span").First().InnerText));
|
|
|
|
|
|
|
|
|
|
List<Archive> archives = new List<Archive>();
|
|
|
|
|
foreach (var (url, name) in pairs)
|
|
|
|
|
{
|
2020-08-24 22:20:50 +00:00
|
|
|
|
var urlDecoded = HttpUtility.HtmlDecode(url);
|
|
|
|
|
var ini = new[] {"[General]", $"directURL={urlDecoded}"};
|
2020-08-12 22:23:02 +00:00
|
|
|
|
var state = (AbstractDownloadState)(await DownloadDispatcher.ResolveArchive(
|
|
|
|
|
string.Join("\n", ini).LoadIniString(), false));
|
|
|
|
|
if (state == null) continue;
|
|
|
|
|
|
|
|
|
|
archives.Add(new Archive(state) {Name = name});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return archives;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 12:23:07 +00:00
|
|
|
|
public override string GetManifestURL(Archive a)
|
2020-02-02 12:15:29 +00:00
|
|
|
|
{
|
2020-04-04 17:53:05 +00:00
|
|
|
|
return IsAttachment ? FullURL : $"{Site}/files/file/{FileName}/?do=download&r={FileID}";
|
2020-02-02 12:15:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 06:44:42 +00:00
|
|
|
|
public async Task<string> GetStringAsync(Uri uri, CancellationToken? token = null)
|
2020-12-29 23:15:47 +00:00
|
|
|
|
{
|
|
|
|
|
if (!Downloader.IsCloudFlareProtected)
|
|
|
|
|
return await Downloader.AuthedClient.GetStringAsync(uri);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using var driver = await Downloader.GetAuthedDriver();
|
2020-12-31 17:51:24 +00:00
|
|
|
|
await ((IWaitForWindowDownloader)Downloader).WaitForNextRequestWindow();
|
2020-12-31 06:44:42 +00:00
|
|
|
|
await DeleteOldDownloadCookies(driver);
|
|
|
|
|
|
2020-12-29 23:15:47 +00:00
|
|
|
|
//var drivercookies = await Helpers.GetCookies("loverslab.com");
|
|
|
|
|
|
|
|
|
|
//var cookies = await ClientAPI.GetAuthInfo<Helpers.Cookie[]>("loverslabcookies");
|
|
|
|
|
//await Helpers.IngestCookies(uri.ToString(), cookies);
|
2020-12-31 06:44:42 +00:00
|
|
|
|
await driver.NavigateTo(uri, token);
|
|
|
|
|
if ((token ?? CancellationToken.None).IsCancellationRequested)
|
|
|
|
|
return "";
|
2020-12-29 23:15:47 +00:00
|
|
|
|
|
|
|
|
|
var source = await driver.GetSourceAsync();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Downloader.AuthedClient.Cookies.Add(drivercookies.Where(dc => dc.Name == "cf_clearance")
|
|
|
|
|
.Select(dc => new Cookie
|
|
|
|
|
{
|
|
|
|
|
Name = dc.Name,
|
|
|
|
|
Domain = dc.Domain,
|
|
|
|
|
Value = dc.Value,
|
|
|
|
|
Path = dc.Path
|
|
|
|
|
})
|
|
|
|
|
.FirstOrDefault());
|
|
|
|
|
|
|
|
|
|
var source = await Downloader.AuthedClient.GetStringAsync(uri);
|
|
|
|
|
*/
|
|
|
|
|
return source;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 06:44:42 +00:00
|
|
|
|
public async Task<HtmlDocument> GetHtmlAsync(Uri s, CancellationToken? token = null)
|
2020-12-29 23:15:47 +00:00
|
|
|
|
{
|
2020-12-31 06:44:42 +00:00
|
|
|
|
var body = await GetStringAsync(s, token);
|
2020-12-29 23:15:47 +00:00
|
|
|
|
var doc = new HtmlDocument();
|
|
|
|
|
doc.LoadHtml(body);
|
|
|
|
|
return doc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<HttpResponseMessage> GetDownloadAsync(Uri uri)
|
|
|
|
|
{
|
|
|
|
|
if (!Downloader.IsCloudFlareProtected)
|
|
|
|
|
return await Downloader.AuthedClient.GetAsync(uri);
|
|
|
|
|
|
|
|
|
|
using var driver = await Downloader.GetAuthedDriver();
|
|
|
|
|
TaskCompletionSource<Uri?> promise = new TaskCompletionSource<Uri?>();
|
|
|
|
|
driver.DownloadHandler = uri1 =>
|
|
|
|
|
{
|
|
|
|
|
promise.SetResult(uri);
|
|
|
|
|
};
|
|
|
|
|
await driver.NavigateTo(uri);
|
|
|
|
|
|
|
|
|
|
var url = await promise.Task;
|
|
|
|
|
if (url == null) throw new Exception("No Url to download");
|
|
|
|
|
var location = await driver.GetLocation();
|
|
|
|
|
return await Helpers.GetClient(await Helpers.GetCookies(), location!.ToString()).GetAsync(uri);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-11 04:15:53 +00:00
|
|
|
|
public override string[] GetMetaIni()
|
|
|
|
|
{
|
2020-04-04 17:40:58 +00:00
|
|
|
|
if (IsAttachment)
|
|
|
|
|
return new[] {"[General]", $"directURL={FullURL}"};
|
|
|
|
|
|
|
|
|
|
if (FileID == null)
|
|
|
|
|
return new[] {"[General]", $"directURL={Site}/files/file/{FileName}"};
|
2020-02-11 22:14:59 +00:00
|
|
|
|
|
2020-04-04 17:40:58 +00:00
|
|
|
|
if (Site.EndsWith("?"))
|
|
|
|
|
{
|
2020-01-11 04:15:53 +00:00
|
|
|
|
return new[]
|
|
|
|
|
{
|
2020-04-04 17:40:58 +00:00
|
|
|
|
"[General]", $"directURL={Site}/files/file/{FileName}&do=download&r={FileID}&confirm=1&t=1"
|
2020-01-11 04:15:53 +00:00
|
|
|
|
};
|
2020-04-04 17:40:58 +00:00
|
|
|
|
|
2020-02-11 22:14:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-11 04:15:53 +00:00
|
|
|
|
return new[]
|
|
|
|
|
{
|
2020-04-04 17:40:58 +00:00
|
|
|
|
"[General]", $"directURL={Site}/files/file/{FileName}/?do=download&r={FileID}&confirm=1&t=1"
|
2020-01-11 04:15:53 +00:00
|
|
|
|
};
|
2020-04-04 17:40:58 +00:00
|
|
|
|
|
2020-01-11 04:15:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 12:10:49 +00:00
|
|
|
|
public virtual async Task<bool> LoadMetaData()
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-01-06 00:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|