Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
OnlyFart committed Sep 30, 2024
1 parent 3f8c718 commit 5b2c603
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

namespace Core.Logic.Getters;

public class BookmateGetter : GetterBase {
public BookmateGetter(BookGetterConfig config) : base(config) { }
public class BooksYandexGetter : GetterBase {
public BooksYandexGetter(BookGetterConfig config) : base(config) { }

protected override Uri SystemUrl => new("https://bookmate.ru/");
protected override Uri SystemUrl => new("https://books.yandex.ru/");

protected override string GetId(Uri url) {
return url.GetSegment(2);
Expand Down
60 changes: 31 additions & 29 deletions Core/Logic/Getters/LitresGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
Expand Down Expand Up @@ -39,15 +40,15 @@ private static long GetCurrentMilli() {
return (long)javaSpan.TotalMilliseconds / 1000;
}

private Uri GetFullUri(LitresArt art, string path, LitresFile file) {
private Uri GetFullUri(LitresArt art, string path, LitresFile file, string host = null) {
var ts = GetCurrentMilli();

var inputBytes = Encoding.ASCII.GetBytes($"{ts}:{art.Id}:{SECRET_KEY}");
var hashBytes = MD5.HashData(inputBytes);

// Текстовая книга
if (art.ArtType != LitresArtTypeEnum.Audio) {
var result = new Uri($"https://catalit.litres.ru/pages/{path}?art={art.Id}&sid={_authData.Sid}&uilang=ru&libapp={APP}&timestamp={ts}&md5={Convert.ToHexString(hashBytes).ToLower()}");
var result = new Uri($"https://{host ?? "catalit.litres.ru"}/pages/{path}?art={art.Id}&sid={_authData.Sid}&uilang=ru&libapp={APP}&timestamp={ts}&md5={Convert.ToHexString(hashBytes).ToLower()}");
if (file == default) {
return result.AppendQueryParameter("type", "fb3");
}
Expand All @@ -61,13 +62,15 @@ private Uri GetFullUri(LitresArt art, string path, LitresFile file) {
}

// Аудиокнига
return new Uri($"https://ios.litres.ru/pages/{path}/{art.Id}/{file.Id}.mp3?sid={_authData.Sid}&uilang=ru&libapp={APP}&timestamp={ts}&md5={Convert.ToHexString(hashBytes).ToLower()}");
return new Uri($"https://{host ?? "ios.litres.ru"}/pages/{path}/{art.Id}/{file.Id}.mp3?sid={_authData.Sid}&uilang=ru&libapp={APP}&timestamp={ts}&md5={Convert.ToHexString(hashBytes).ToLower()}");
}

private LitresAuthResponseData _authData = new(){
Sid = "6ufp4b2wbx1acc4k1f0wdo571762c0fn"
};

private LitresMe _me;

public override async Task Authorize() {
if (!Config.HasCredentials) {
Config.Client.DefaultRequestHeaders.Add("Session-Id", _authData.Sid);
Expand All @@ -87,8 +90,9 @@ public override async Task Authorize() {
_authData = await File.ReadAllTextAsync(saveCreds).ContinueWith(t => t.Result.Deserialize<LitresAuthResponseData>());
Config.Client.DefaultRequestHeaders.Add("Session-Id", _authData.Sid);

using var me = await Config.Client.GetAsync("https://api.litres.ru/foundation/api/users/me/detailed");
if (me.StatusCode == HttpStatusCode.OK) {
var checkResponse = await Config.Client.GetFromJsonAsync<LitresStaticResponse<LitresMe>>("https://api.litres.ru/foundation/api/users/me/detailed");
if (checkResponse?.Payload?.Data != default) {
_me = checkResponse.Payload.Data;
return;
}
}
Expand All @@ -102,6 +106,12 @@ public override async Task Authorize() {
throw new Exception($"Не удалось авторизоваться. {_authData.ErrorMessage}");
}

var meResponse = await Config.Client.GetFromJsonAsync<LitresStaticResponse<LitresMe>>("https://api.litres.ru/foundation/api/users/me/detailed");
if (meResponse?.Payload?.Data == default) {
return;
}

_me = meResponse.Payload.Data;
Config.Client.DefaultRequestHeaders.Add("Session-Id", _authData.Sid);
await File.WriteAllTextAsync(saveCreds, JsonSerializer.Serialize(_authData));
}
Expand Down Expand Up @@ -175,11 +185,7 @@ private async Task FillAdditional(Book book, LitresArt art) {
using var fileResponse = await GetFileResponse(art, file);
if (fileResponse != default) {
var tempFile = await CreateTempFile(fileResponse);
if (art.ArtType != LitresArtTypeEnum.Audio) {
book.AdditionalFiles.Add(AdditionalTypeEnum.Books, tempFile);
} else {
book.AdditionalFiles.Add(AdditionalTypeEnum.Audio, tempFile);
}
book.AdditionalFiles.Add(art.ArtType != LitresArtTypeEnum.Audio ? AdditionalTypeEnum.Books : AdditionalTypeEnum.Audio, tempFile);
}
}
} else if (art.ArtType != LitresArtTypeEnum.Audio) {
Expand Down Expand Up @@ -305,26 +311,22 @@ private async Task<IEnumerable<TempFile>> GetImages(HtmlNode doc, LitresBook boo
}

private async Task<HttpResponseMessage> GetFileResponse(LitresArt art, LitresFile file) {
if (_authData != null) {
var uri = GetFullUri(art, "download_book_j", file);
var response = await Config.Client.GetAsync(uri);
if (response.StatusCode == HttpStatusCode.OK && response.Headers.AcceptRanges.Any()) {
Config.Logger.LogInformation($"Дополнительный файл доступен по ссылке {uri}");
return response;
}
var paths = new []{"download_book_j", "download_book_subscr", "download_my_book_j"};
var hosts = new List<string> { null };
if (_me.PartnerSubscriptions?.Subscriptions != default) {
hosts.AddRange(_me.PartnerSubscriptions.Subscriptions.Where(s => s.IsActive && s.Type == (art.ArtType == LitresArtTypeEnum.Audio ? "audio" : "text")).Select(s => s.Host).Distinct());
}

uri = GetFullUri(art, "download_book_subscr", file);
response = await Config.Client.GetAsync(uri);
if (response.StatusCode == HttpStatusCode.OK && response.Headers.AcceptRanges.Any()) {
Config.Logger.LogInformation($"Дополнительный файл доступен по ссылке {uri}");
return response;
}

uri = GetFullUri(art, "download_my_book_j", file);
response = await Config.Client.GetAsync(uri);
if (response.StatusCode == HttpStatusCode.OK && response.Headers.AcceptRanges.Any()) {
Config.Logger.LogInformation($"Дополнительный файл доступен по ссылке {uri}");
return response;
if (_authData != null) {
foreach (var host in hosts) {
foreach (var path in paths) {
var uri = GetFullUri(art, path, file, host);
var response = await Config.Client.GetAsync(uri);
if (response.StatusCode == HttpStatusCode.OK && response.Headers.AcceptRanges.Count != 0) {
Config.Logger.LogInformation($"Дополнительный файл доступен по ссылке {uri}");
return response;
}
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions Core/Types/Litres/Response/LitresMe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Text.Json.Serialization;

namespace Core.Types.Litres.Response;

public class LitresMe {
[JsonPropertyName("partner_subscriptions")]
public LitresPartnerSubscriptions PartnerSubscriptions { get; set; }
}
8 changes: 8 additions & 0 deletions Core/Types/Litres/Response/LitresPartnerSubscriptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Text.Json.Serialization;

namespace Core.Types.Litres.Response;

public class LitresPartnerSubscriptions {
[JsonPropertyName("subscriptions")]
public LitresSubscription[] Subscriptions { get; set; }
}
14 changes: 14 additions & 0 deletions Core/Types/Litres/Response/LitresSubscription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text.Json.Serialization;

namespace Core.Types.Litres.Response;

public class LitresSubscription {
[JsonPropertyName("is_active")]
public bool IsActive { get; set; }

[JsonPropertyName("host")]
public string Host { get; set; }

[JsonPropertyName("type")]
public string Type { get; set; }
}

0 comments on commit 5b2c603

Please sign in to comment.