2019-11-21 14:25:56 +00:00
|
|
|
|
using System.Net.Http;
|
2019-10-12 21:37:16 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2019-12-06 05:29:17 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-10-12 21:37:16 +00:00
|
|
|
|
using Wabbajack.Common;
|
2020-01-07 13:50:11 +00:00
|
|
|
|
using Wabbajack.Lib.Exceptions;
|
2019-10-16 03:10:34 +00:00
|
|
|
|
using Wabbajack.Lib.Validation;
|
2019-10-12 21:37:16 +00:00
|
|
|
|
|
2019-10-16 03:10:34 +00:00
|
|
|
|
namespace Wabbajack.Lib.Downloaders
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2019-10-16 11:44:45 +00:00
|
|
|
|
public class GoogleDriveDownloader : IDownloader, IUrlDownloader
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2019-12-07 03:50:50 +00:00
|
|
|
|
public async Task<AbstractDownloadState> GetDownloaderState(dynamic archiveINI)
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var url = archiveINI?.General?.directURL;
|
2019-10-16 11:44:45 +00:00
|
|
|
|
return GetDownloaderState(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AbstractDownloadState GetDownloaderState(string url)
|
|
|
|
|
{
|
2019-10-12 21:37:16 +00:00
|
|
|
|
if (url != null && url.StartsWith("https://drive.google.com"))
|
|
|
|
|
{
|
2019-10-16 11:44:45 +00:00
|
|
|
|
var regex = new Regex("((?<=id=)[a-zA-Z0-9_-]*)|(?<=\\/file\\/d\\/)[a-zA-Z0-9_-]*");
|
2019-10-12 21:37:16 +00:00
|
|
|
|
var match = regex.Match(url);
|
|
|
|
|
return new State
|
|
|
|
|
{
|
|
|
|
|
Id = match.ToString()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 02:45:13 +00:00
|
|
|
|
public async Task Prepare()
|
2019-10-12 22:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 21:37:16 +00:00
|
|
|
|
public class State : AbstractDownloadState
|
|
|
|
|
{
|
|
|
|
|
public string Id { get; set; }
|
2020-01-01 16:19:06 +00:00
|
|
|
|
public override object[] PrimaryKey { get => new object[] {Id}; }
|
|
|
|
|
|
2019-10-12 21:37:16 +00:00
|
|
|
|
public override bool IsWhitelisted(ServerWhitelist whitelist)
|
|
|
|
|
{
|
|
|
|
|
return whitelist.GoogleIDs.Contains(Id);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-18 19:57:26 +00:00
|
|
|
|
public override async Task<bool> Download(Archive a, string destination)
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2019-12-06 05:59:57 +00:00
|
|
|
|
var state = await ToHttpState();
|
2020-01-18 19:57:26 +00:00
|
|
|
|
return await state.Download(a, destination);
|
2019-10-12 21:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 05:59:57 +00:00
|
|
|
|
private async Task<HTTPDownloader.State> ToHttpState()
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var initialURL = $"https://drive.google.com/uc?id={Id}&export=download";
|
2019-10-12 21:37:16 +00:00
|
|
|
|
var client = new HttpClient();
|
2020-01-04 03:37:36 +00:00
|
|
|
|
var response = await client.GetAsync(initialURL);
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
throw new HttpException((int)response.StatusCode, response.ReasonPhrase);
|
2019-10-12 21:37:16 +00:00
|
|
|
|
var regex = new Regex("(?<=/uc\\?export=download&confirm=).*(?=;id=)");
|
2020-01-04 03:37:36 +00:00
|
|
|
|
var confirm = regex.Match(await response.Content.ReadAsStringAsync());
|
2019-10-12 21:37:16 +00:00
|
|
|
|
var url = $"https://drive.google.com/uc?export=download&confirm={confirm}&id={Id}";
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var httpState = new HTTPDownloader.State {Url = url, Client = client};
|
|
|
|
|
return httpState;
|
2019-10-12 21:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 22:55:55 +00:00
|
|
|
|
public override async Task<bool> Verify(Archive a)
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2019-12-06 05:59:57 +00:00
|
|
|
|
var state = await ToHttpState();
|
2020-01-13 22:55:55 +00:00
|
|
|
|
return await state.Verify(a);
|
2019-10-12 21:37:16 +00:00
|
|
|
|
}
|
2019-10-12 22:15:20 +00:00
|
|
|
|
|
|
|
|
|
public override IDownloader GetDownloader()
|
|
|
|
|
{
|
|
|
|
|
return DownloadDispatcher.GetInstance<GoogleDriveDownloader>();
|
|
|
|
|
}
|
2019-10-12 22:54:25 +00:00
|
|
|
|
|
|
|
|
|
public override string GetReportEntry(Archive a)
|
|
|
|
|
{
|
|
|
|
|
return $"* GoogleDrive - [{a.Name}](https://drive.google.com/uc?id={Id}&export=download)";
|
|
|
|
|
}
|
2020-01-11 04:15:53 +00:00
|
|
|
|
|
|
|
|
|
public override string[] GetMetaIni()
|
|
|
|
|
{
|
|
|
|
|
return new [] {"[General]",$"directURL=https://drive.google.com/uc?id={Id}&export=download"};
|
|
|
|
|
}
|
2019-10-12 21:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|