wabbajack/Wabbajack.Downloaders.Nexus/NexusDownloader.cs

134 lines
4.2 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Extensions.Logging;
using Wabbajack.Downloaders.Interfaces;
using Wabbajack.DTOs;
using Wabbajack.DTOs.DownloadStates;
using Wabbajack.DTOs.Validation;
using Wabbajack.Hashing.xxHash64;
using Wabbajack.Networking.Http;
using Wabbajack.Networking.Http.Interfaces;
using Wabbajack.Networking.NexusApi;
using Wabbajack.Paths;
using Wabbajack.RateLimiter;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Downloaders;
public class NexusDownloader : ADownloader<Nexus>, IUrlDownloader
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
private readonly NexusApi _api;
private readonly HttpClient _client;
private readonly IHttpDownloader _downloader;
private readonly ILogger<NexusDownloader> _logger;
public NexusDownloader(ILogger<NexusDownloader> logger, HttpClient client, IHttpDownloader downloader,
NexusApi api)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
_logger = logger;
_client = client;
_downloader = downloader;
_api = api;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override async Task<bool> Prepare()
{
return true;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override bool IsAllowed(ServerAllowList allowList, IDownloadState state)
{
return true;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public IDownloadState? Parse(Uri uri)
{
if (uri.Host != "www.nexusmods.com")
return null;
var relPath = (RelativePath) uri.AbsolutePath;
long modId, fileId;
if (relPath.Depth != 3)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
_logger.LogWarning("Got www.nexusmods.com link but it didn't match a parsable pattern: {url}", uri);
return null;
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
if (!long.TryParse(relPath.FileName.ToString(), out modId))
return null;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var game = GameRegistry.ByNexusName[relPath.Parent.Parent.ToString()].FirstOrDefault();
if (game == null) return null;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var query = HttpUtility.ParseQueryString(uri.Query);
var fileIdStr = query.Get("file_id");
if (!long.TryParse(fileIdStr, out fileId))
return null;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
return new Nexus
{
Game = game.Game,
ModID = modId,
FileID = fileId
};
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public Uri UnParse(IDownloadState state)
{
var nstate = (Nexus) state;
return new Uri(
$"https://www.nexusmods.com/{nstate.Game.MetaData().NexusName}/mods/{nstate.ModID}/?tab=files&file_id={nstate.FileID}");
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override IDownloadState? Resolve(IReadOnlyDictionary<string, string> iniData)
{
if (iniData.TryGetValue("gameName", out var gameName) &&
iniData.TryGetValue("modID", out var modId) &&
iniData.TryGetValue("fileID", out var fileId))
2021-09-27 12:42:46 +00:00
return new Nexus
{
2021-10-23 16:51:17 +00:00
Game = GameRegistry.GetByMO2ArchiveName(gameName)!.Game,
ModID = long.Parse(modId),
FileID = long.Parse(fileId)
2021-09-27 12:42:46 +00:00
};
2021-10-23 16:51:17 +00:00
return null;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override Priority Priority => Priority.Normal;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override async Task<Hash> Download(Archive archive, Nexus state, AbsolutePath destination,
IJob job, CancellationToken token)
{
var urls = await _api.DownloadLink(state.Game.MetaData().NexusName!, state.ModID, state.FileID, token);
_logger.LogInformation("Downloading Nexus File: {game}|{modid}|{fileid}", state.Game, state.ModID,
state.FileID);
var message = new HttpRequestMessage(HttpMethod.Get, urls.info.First().URI);
return await _downloader.Download(message, destination, job, token);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override async Task<bool> Verify(Archive archive, Nexus state, IJob job, CancellationToken token)
{
try
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
var fileInfo = await _api.FileInfo(state.Game.MetaData().NexusName!, state.ModID, state.FileID, token);
return fileInfo.info.FileId == state.FileID;
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
catch (HttpException)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
return false;
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override IEnumerable<string> MetaIni(Archive a, Nexus state)
{
return new[]
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
$"gameName={state.Game.MetaData().MO2ArchiveName}", $"modID={state.ModID}", $"fileID={state.FileID}"
};
2021-09-27 12:42:46 +00:00
}
}