2019-10-12 21:37:16 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Wabbajack.Common;
|
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
|
|
|
|
{
|
|
|
|
|
public AbstractDownloadState GetDownloaderState(dynamic archive_ini)
|
|
|
|
|
{
|
|
|
|
|
var url = archive_ini?.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-10-12 22:15:20 +00:00
|
|
|
|
public void Prepare()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 21:37:16 +00:00
|
|
|
|
public class State : AbstractDownloadState
|
|
|
|
|
{
|
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
public override bool IsWhitelisted(ServerWhitelist whitelist)
|
|
|
|
|
{
|
|
|
|
|
return whitelist.GoogleIDs.Contains(Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Download(Archive a, string destination)
|
|
|
|
|
{
|
|
|
|
|
ToHttpState().Download(a, destination);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HTTPDownloader.State ToHttpState()
|
|
|
|
|
{
|
|
|
|
|
var initial_url = $"https://drive.google.com/uc?id={Id}&export=download";
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
var result = client.GetStringSync(initial_url);
|
|
|
|
|
var regex = new Regex("(?<=/uc\\?export=download&confirm=).*(?=;id=)");
|
|
|
|
|
var confirm = regex.Match(result);
|
|
|
|
|
var url = $"https://drive.google.com/uc?export=download&confirm={confirm}&id={Id}";
|
2019-10-21 19:05:03 +00:00
|
|
|
|
var http_state = new HTTPDownloader.State {Url = url, Client = client};
|
2019-10-12 21:37:16 +00:00
|
|
|
|
return http_state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Verify()
|
|
|
|
|
{
|
|
|
|
|
return ToHttpState().Verify();
|
|
|
|
|
}
|
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)";
|
|
|
|
|
}
|
2019-10-12 21:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|