2020-12-23 13:13:52 -05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Net.Http;
|
2021-01-04 09:43:27 -05:00
|
|
|
using System.Text;
|
2020-12-23 13:13:52 -05:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Threading.Tasks;
|
2020-12-28 09:27:37 -05:00
|
|
|
using auto_creamapi.Messenger;
|
2020-12-23 13:13:52 -05:00
|
|
|
using auto_creamapi.Utils;
|
|
|
|
using HttpProgress;
|
2020-12-28 09:27:37 -05:00
|
|
|
using MvvmCross.Plugin.Messenger;
|
2021-01-04 09:43:27 -05:00
|
|
|
using SevenZip;
|
2020-12-23 13:13:52 -05:00
|
|
|
|
|
|
|
namespace auto_creamapi.Services
|
|
|
|
{
|
2020-12-25 10:49:51 -05:00
|
|
|
public interface IDownloadCreamApiService
|
|
|
|
{
|
2020-12-28 09:27:37 -05:00
|
|
|
public Task<string> Download(string username, string password);
|
2021-01-04 09:43:27 -05:00
|
|
|
public Task Extract(string filename);
|
2020-12-25 10:49:51 -05:00
|
|
|
}
|
2020-12-28 09:27:37 -05:00
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
public class DownloadCreamApiService : IDownloadCreamApiService
|
2020-12-23 13:13:52 -05:00
|
|
|
{
|
|
|
|
private const string ArchivePassword = "cs.rin.ru";
|
2020-12-28 09:27:37 -05:00
|
|
|
private readonly IMvxMessenger _messenger;
|
2020-12-23 13:13:52 -05:00
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
public DownloadCreamApiService(IMvxMessenger messenger)
|
2020-12-23 13:13:52 -05:00
|
|
|
{
|
2020-12-28 09:27:37 -05:00
|
|
|
_messenger = messenger;
|
2020-12-25 10:49:51 -05:00
|
|
|
}
|
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
public async Task<string> Download(string username, string password)
|
2020-12-23 13:13:52 -05:00
|
|
|
{
|
2020-12-25 10:49:51 -05:00
|
|
|
MyLogger.Log.Debug("Download");
|
2020-12-23 13:13:52 -05:00
|
|
|
var container = new CookieContainer();
|
|
|
|
var handler = new HttpClientHandler {CookieContainer = container};
|
|
|
|
var client = new HttpClient(handler);
|
|
|
|
var formContent = new FormUrlEncodedContent(new[]
|
|
|
|
{
|
|
|
|
new KeyValuePair<string, string>("username", username),
|
|
|
|
new KeyValuePair<string, string>("password", password),
|
|
|
|
new KeyValuePair<string, string>("redirect", "./ucp.php?mode=login"),
|
|
|
|
new KeyValuePair<string, string>("login", "login")
|
|
|
|
});
|
2020-12-25 10:49:51 -05:00
|
|
|
MyLogger.Log.Debug("Download: post login");
|
2021-01-13 07:04:35 -05:00
|
|
|
var response1 = await client.PostAsync("https://cs.rin.ru/forum/ucp.php?mode=login", formContent)
|
|
|
|
.ConfigureAwait(false);
|
2020-12-23 13:13:52 -05:00
|
|
|
MyLogger.Log.Debug($"Login Status Code: {response1.EnsureSuccessStatusCode().StatusCode.ToString()}");
|
|
|
|
var cookie = container.GetCookies(new Uri("https://cs.rin.ru/forum/ucp.php?mode=login"))
|
|
|
|
.FirstOrDefault(c => c.Name.Contains("_sid"));
|
|
|
|
MyLogger.Log.Debug($"Login Cookie: {cookie}");
|
2021-01-13 07:04:35 -05:00
|
|
|
var response2 = await client.GetAsync("https://cs.rin.ru/forum/viewtopic.php?t=70576")
|
|
|
|
.ConfigureAwait(false);
|
2020-12-23 13:13:52 -05:00
|
|
|
MyLogger.Log.Debug(
|
|
|
|
$"Download Page Status Code: {response2.EnsureSuccessStatusCode().StatusCode.ToString()}");
|
|
|
|
var content = response2.Content.ReadAsStringAsync();
|
2021-01-13 07:04:35 -05:00
|
|
|
var contentResult = await content.ConfigureAwait(false);
|
2020-12-23 13:13:52 -05:00
|
|
|
|
|
|
|
var expression =
|
|
|
|
new Regex(".*<a href=\"\\.(?<url>\\/download\\/file\\.php\\?id=.*)\">(?<filename>.*)<\\/a>.*");
|
|
|
|
using var reader = new StringReader(contentResult);
|
|
|
|
string line;
|
|
|
|
var archiveFileList = new Dictionary<string, string>();
|
2021-01-13 07:04:35 -05:00
|
|
|
while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
|
2020-12-23 13:13:52 -05:00
|
|
|
{
|
|
|
|
var match = expression.Match(line);
|
|
|
|
// ReSharper disable once InvertIf
|
|
|
|
if (match.Success)
|
|
|
|
{
|
|
|
|
archiveFileList.Add(match.Groups["filename"].Value,
|
|
|
|
$"https://cs.rin.ru/forum{match.Groups["url"].Value}");
|
|
|
|
MyLogger.Log.Debug(archiveFileList.LastOrDefault().Key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MyLogger.Log.Debug("Choosing first element from list...");
|
|
|
|
var (filename, url) = archiveFileList.FirstOrDefault();
|
2020-12-28 09:27:37 -05:00
|
|
|
if (File.Exists(filename))
|
2020-12-23 13:13:52 -05:00
|
|
|
{
|
2020-12-28 09:27:37 -05:00
|
|
|
MyLogger.Log.Information($"{filename} already exists, skipping download...");
|
|
|
|
return filename;
|
2020-12-23 13:13:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
MyLogger.Log.Information("Start download...");
|
|
|
|
var progress = new Progress<ICopyProgress>(
|
2020-12-28 09:27:37 -05:00
|
|
|
x => _messenger.Publish(new ProgressMessage(this, "Downloading...", filename, x)));
|
|
|
|
await using var fileStream = File.OpenWrite(filename);
|
2020-12-23 13:13:52 -05:00
|
|
|
var task = client.GetAsync(url, fileStream, progress);
|
2021-01-13 07:04:35 -05:00
|
|
|
await task.ConfigureAwait(false);
|
2020-12-23 13:13:52 -05:00
|
|
|
if (task.IsCompletedSuccessfully)
|
2020-12-28 09:27:37 -05:00
|
|
|
_messenger.Publish(new ProgressMessage(this, "Downloading...", filename, 1.0));
|
|
|
|
MyLogger.Log.Information("Download done.");
|
|
|
|
return filename;
|
2020-12-23 13:13:52 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 09:43:27 -05:00
|
|
|
public async Task Extract(string filename)
|
2020-12-23 13:13:52 -05:00
|
|
|
{
|
2020-12-25 10:49:51 -05:00
|
|
|
MyLogger.Log.Debug("Extract");
|
2021-01-04 09:43:27 -05:00
|
|
|
var cwd = Directory.GetCurrentDirectory();
|
|
|
|
const string nonlogBuild = "nonlog_build";
|
|
|
|
const string steamApi64Dll = "steam_api64.dll";
|
|
|
|
const string steamApiDll = "steam_api.dll";
|
2020-12-28 09:27:37 -05:00
|
|
|
MyLogger.Log.Information($@"Start extraction of ""{filename}""...");
|
2021-01-13 07:06:28 -05:00
|
|
|
var nonlogBuildPath = Path.Combine(cwd, nonlogBuild);
|
|
|
|
if (Directory.Exists(nonlogBuildPath))
|
|
|
|
Directory.Delete(nonlogBuildPath, true);
|
|
|
|
_messenger.Publish(new ProgressMessage(this, "Extracting...", filename, 1.0));
|
|
|
|
SevenZipBase.SetLibraryPath(Path.Combine(cwd, "resources/7z.dll"));
|
|
|
|
using (var extractor =
|
|
|
|
new SevenZipExtractor(filename, ArchivePassword, InArchiveFormat.Rar)
|
|
|
|
{PreserveDirectoryStructure = false})
|
2021-01-13 07:04:35 -05:00
|
|
|
{
|
2021-01-13 07:06:28 -05:00
|
|
|
await extractor.ExtractFilesAsync(
|
|
|
|
cwd,
|
|
|
|
$@"{nonlogBuild}\{steamApi64Dll}",
|
|
|
|
$@"{nonlogBuild}\{steamApiDll}"
|
|
|
|
).ConfigureAwait(false);
|
|
|
|
}
|
2021-01-13 07:04:35 -05:00
|
|
|
|
2021-01-13 07:06:28 -05:00
|
|
|
if (File.Exists(Path.Combine(nonlogBuildPath, steamApi64Dll)))
|
|
|
|
File.Move(
|
|
|
|
Path.Combine(cwd, nonlogBuild, steamApi64Dll),
|
|
|
|
Path.Combine(cwd, steamApi64Dll),
|
|
|
|
true
|
|
|
|
);
|
2021-01-13 07:04:35 -05:00
|
|
|
|
2021-01-13 07:06:28 -05:00
|
|
|
if (File.Exists(Path.Combine(nonlogBuildPath, steamApiDll)))
|
|
|
|
File.Move(
|
|
|
|
Path.Combine(nonlogBuildPath, steamApiDll),
|
|
|
|
Path.Combine(cwd, steamApiDll),
|
|
|
|
true
|
|
|
|
);
|
2021-01-13 07:04:35 -05:00
|
|
|
|
2021-01-13 07:06:28 -05:00
|
|
|
if (Directory.Exists(nonlogBuildPath))
|
|
|
|
Directory.Delete(nonlogBuildPath, true);
|
|
|
|
MyLogger.Log.Information("Extraction done!");
|
2020-12-23 13:13:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|