Skip to content

Commit

Permalink
Add filter to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xwilarg committed Aug 17, 2022
1 parent 1113461 commit 68b76c4
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 22 deletions.
7 changes: 2 additions & 5 deletions BooruSharp/Booru/ABooru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public async Task CheckAvailabilityAsync()
/// Add booru authentification to current request
/// </summary>
/// <param name="message">The request that is going to be sent</param>
protected virtual void AddAuth(HttpRequestMessage message)
protected virtual void PreRequest(HttpRequestMessage message)
{ }

/// <summary>
Expand Down Expand Up @@ -244,10 +244,7 @@ private protected async Task<string> GetJsonAsync(string url)
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

var message = new HttpRequestMessage(HttpMethod.Get, url);
if (Auth != null)
{
AddAuth(message);
}
PreRequest(message);
var msg = await HttpClient.SendAsync(message);

if (msg.StatusCode == HttpStatusCode.Forbidden)
Expand Down
3 changes: 3 additions & 0 deletions BooruSharp/Booru/Derpibooru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ public Derpibooru() : base("derpibooru.org")

/// <inheritdoc/>
public override bool IsSafe => false;

/// <inheritdoc/>
protected override int FilterID => 56027;
}
}
3 changes: 3 additions & 0 deletions BooruSharp/Booru/Ponybooru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ public Ponybooru() : base("ponybooru.org")

/// <inheritdoc/>
public override bool IsSafe => false;

/// <inheritdoc/>
protected override int FilterID => 2;
}
}
13 changes: 11 additions & 2 deletions BooruSharp/Booru/Template/BooruOnRails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@ protected BooruOnRails(string domain, BooruOptions options = BooruOptions.None)
| BooruOptions.NoLastComments | BooruOptions.NoWiki | BooruOptions.NoRelated)
{ }

/// <summary>
/// ID used to set filter and have access to as many posts as possible
/// </summary>
protected abstract int FilterID { get; }

/// <inheritdoc/>
protected override void AddAuth(HttpRequestMessage message)
protected override void PreRequest(HttpRequestMessage message)
{
var uriBuilder = new UriBuilder(message.RequestUri.AbsoluteUri);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query["key"] = Auth.PasswordHash;
query["filter_id"] = $"{FilterID}";
if (Auth != null)
{
query["key"] = Auth.PasswordHash;
}
uriBuilder.Query = query.ToString();
message.RequestUri = new Uri(uriBuilder.ToString());
}
Expand Down
7 changes: 5 additions & 2 deletions BooruSharp/Booru/Template/Danbooru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ protected Danbooru(string domain, BooruOptions options = BooruOptions.None)
{ }

/// <inheritdoc/>
protected override void AddAuth(HttpRequestMessage message)
protected override void PreRequest(HttpRequestMessage message)
{
message.Headers.Add("Cookie", "user_id=" + Auth.UserId + ";pass_hash=" + Auth.PasswordHash);
if (Auth != null)
{
message.Headers.Add("Cookie", "user_id=" + Auth.UserId + ";pass_hash=" + Auth.PasswordHash);
}
}

private protected override JToken ParseFirstPostSearchResult(object json)
Expand Down
9 changes: 6 additions & 3 deletions BooruSharp/Booru/Template/E621.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ protected E621(string domain, BooruOptions options = BooruOptions.None)
{ }

/// <inheritdoc/>
protected override void AddAuth(HttpRequestMessage message)
protected override void PreRequest(HttpRequestMessage message)
{
string encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(Auth.UserId + ":" + Auth.PasswordHash));
message.Headers.Add("Authorization", $"Basic {encoded}");
if (Auth != null)
{
string encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(Auth.UserId + ":" + Auth.PasswordHash));
message.Headers.Add("Authorization", $"Basic {encoded}");
}
}

private protected override JToken ParseFirstPostSearchResult(object json)
Expand Down
7 changes: 5 additions & 2 deletions BooruSharp/Booru/Template/Gelbooru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ protected Gelbooru(string domain, BooruOptions options = BooruOptions.None)
{ }

/// <inheritdoc/>
protected override void AddAuth(HttpRequestMessage message)
protected override void PreRequest(HttpRequestMessage message)
{
message.Headers.Add("Cookie", "user_id=" + Auth.UserId + ";pass_hash=" + Auth.PasswordHash);
if (Auth != null)
{
message.Headers.Add("Cookie", "user_id=" + Auth.UserId + ";pass_hash=" + Auth.PasswordHash);
}
}

/// <inheritdoc/>
Expand Down
7 changes: 5 additions & 2 deletions BooruSharp/Booru/Template/Gelbooru02.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ protected Gelbooru02(string domain, BooruOptions options = BooruOptions.None)
}

/// <inheritdoc/>
protected override void AddAuth(HttpRequestMessage message)
protected override void PreRequest(HttpRequestMessage message)
{
message.Headers.Add("Cookie", "user_id=" + Auth.UserId + ";pass_hash=" + Auth.PasswordHash);
if (Auth != null)
{
message.Headers.Add("Cookie", "user_id=" + Auth.UserId + ";pass_hash=" + Auth.PasswordHash);
}
}

private protected override JToken ParseFirstPostSearchResult(object json)
Expand Down
7 changes: 5 additions & 2 deletions BooruSharp/Booru/Template/Moebooru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ protected Moebooru(string domain, BooruOptions options = BooruOptions.None)
{ }

/// <inheritdoc/>
protected override void AddAuth(HttpRequestMessage message)
protected override void PreRequest(HttpRequestMessage message)
{
message.Headers.Add("Cookie", "user_id=" + Auth.UserId + ";pass_hash=" + Auth.PasswordHash);
if (Auth != null)
{
message.Headers.Add("Cookie", "user_id=" + Auth.UserId + ";pass_hash=" + Auth.PasswordHash);
}
}

private protected override JToken ParseFirstPostSearchResult(object json)
Expand Down
13 changes: 11 additions & 2 deletions BooruSharp/Booru/Template/Philomena.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@ protected Philomena(string domain, BooruOptions options = BooruOptions.None)
| BooruOptions.NoLastComments | BooruOptions.NoWiki | BooruOptions.NoRelated)
{ }

/// <summary>
/// ID used to set filter and have access to as many posts as possible
/// </summary>
protected abstract int FilterID { get; }

/// <inheritdoc/>
protected override void AddAuth(HttpRequestMessage message)
protected override void PreRequest(HttpRequestMessage message)
{
var uriBuilder = new UriBuilder(message.RequestUri.AbsoluteUri);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query["key"] = Auth.PasswordHash;
query["filter_id"] = $"{FilterID}";
if (Auth != null)
{
query["key"] = Auth.PasswordHash;
}
uriBuilder.Query = query.ToString();
message.RequestUri = new Uri(uriBuilder.ToString());
}
Expand Down
7 changes: 5 additions & 2 deletions BooruSharp/Booru/Template/Sankaku.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ protected Sankaku(string domain, BooruOptions options = BooruOptions.None)
{ }

/// <inheritdoc/>
protected override void AddAuth(HttpRequestMessage message) // TODO: Doesn't work rn
protected override void PreRequest(HttpRequestMessage message) // TODO: Doesn't work rn
{
message.Headers.Add("Cookie", "user_id=" + Auth.UserId + ";pass_hash=" + Auth.PasswordHash);
if (Auth != null)
{
message.Headers.Add("Cookie", "user_id=" + Auth.UserId + ";pass_hash=" + Auth.PasswordHash);
}
}

private protected override JToken ParseFirstPostSearchResult(object json)
Expand Down
3 changes: 3 additions & 0 deletions BooruSharp/Booru/Twibooru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ public Twibooru() : base("twibooru.org")

/// <inheritdoc/>
public override bool IsSafe => false;

/// <inheritdoc/>
protected override int FilterID => 2;
}
}

0 comments on commit 68b76c4

Please sign in to comment.