Skip to content

Commit

Permalink
Merge pull request #2 from nullforce-public/addTwibooruSupport
Browse files Browse the repository at this point in the history
Add twibooru support
  • Loading branch information
nullforce authored Aug 15, 2022
2 parents 8eb9b93 + 08d4aec commit 9290a32
Show file tree
Hide file tree
Showing 29 changed files with 482 additions and 197 deletions.
File renamed without changes.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Nullforce.Api.Derpibooru
A .NET Standard library for consuming derpibooru.org API endpoints.
A .NET Standard library for consuming Philomena API endpoints.

|||
----------------------|---
**Build** | [![Build Status](https://github.com/nullforce-public/Nullforce.Api.Derpibooru/workflows/build/badge.svg?branch=main)](https://github.com/nullforce-public/Nullforce.Api.Derpibooru/actions)
**NuGet** | [![nuget](https://img.shields.io/nuget/v/Nullforce.Api.Derpibooru.svg)](https://www.nuget.org/packages/Nullforce.Api.Derpibooru/)
**NuGet (prerelease)**| [![nuget](https://img.shields.io/nuget/vpre/Nullforce.Api.Derpibooru.svg)](https://www.nuget.org/packages/Nullforce.Api.Derpibooru/)
Also Supports:
- derpibooru.org
- twibooru.org

| | |
|----------------------|---|
|**Build** | [![Build Status](https://github.com/nullforce-public/Nullforce.Api.Derpibooru/workflows/build/badge.svg?branch=main)](https://github.com/nullforce-public/Nullforce.Api.Derpibooru/actions)|
|**NuGet** | [![nuget](https://img.shields.io/nuget/v/Nullforce.Api.Derpibooru.svg)](https://www.nuget.org/packages/Nullforce.Api.Derpibooru/)|
|**NuGet (prerelease)**| [![nuget](https://img.shields.io/nuget/vpre/Nullforce.Api.Derpibooru.svg)](https://www.nuget.org/packages/Nullforce.Api.Derpibooru/)|


## Usage Example
Expand Down
16 changes: 3 additions & 13 deletions src/Nullforce.Api.Derpibooru.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.1.0</Version>
<Authors>Nullforce</Authors>
<Description>A C# wrapper for the Derpibooru API</Description>
<Copyright>Copyright 2019-2022 nullforce</Copyright>
<RepositoryUrl>https://github.com/nullforce-public/Nullforce.Api.Derpibooru</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>derpibooru</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<UseFullSemVerForNuGet>false</UseFullSemVerForNuGet>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -24,7 +14,7 @@
<RepositoryUrl>https://github.com/nullforce-public/Nullforce.Api.Derpibooru</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageIcon>icon.png</PackageIcon>
<PackageTags>derpibooru</PackageTags>
<PackageTags>derpibooru philomena twibooru</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<UseFullSemVerForNuGet>false</UseFullSemVerForNuGet>
Expand All @@ -36,12 +26,12 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Nullforce.Api.Derpibooru.JsonModels" Version="0.3.0" />
<PackageReference Include="Nullforce.Api.Derpibooru.JsonModels" Version="0.4.0" />
</ItemGroup>

<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\" />
<None Include="..\LICENSE.txt" Pack="true" PackagePath="\" />
<None Include="..\LICENSE" Pack="true" PackagePath="\" />
<None Include="..\icon.png" Pack="true" PackagePath="\" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/DerpiBase.cs → src/UrlBuilder/Derpibooru/DerpiBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Nullforce.Api.Derpibooru;
namespace Nullforce.Api.UrlBuilder.Derpibooru;

public class DerpiBase
public abstract class DerpiBase
{
protected readonly string _apiBaseUri;
protected readonly string _apiKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Nullforce.Api.Derpibooru;
namespace Nullforce.Api.UrlBuilder.Derpibooru;

public class DerpiClient
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Flurl;

namespace Nullforce.Api.Derpibooru;
namespace Nullforce.Api.UrlBuilder.Derpibooru;

public class DerpiGetFeaturedImage : DerpiBase
public class DerpiGetFeaturedImage : DerpiBase, IGetFeaturedImage
{
public DerpiGetFeaturedImage(string apiBaseUri, string apiKey)
: base(apiBaseUri, apiKey)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Flurl;

namespace Nullforce.Api.Derpibooru;
namespace Nullforce.Api.UrlBuilder.Derpibooru;

public class DerpiGetImage : DerpiBase
public class DerpiGetImage : DerpiBase, IGetImage
{
public DerpiGetImage(string apiBaseUri, string apiKey, int imageId)
: base(apiBaseUri, apiKey)
Expand All @@ -13,8 +13,8 @@ public DerpiGetImage(string apiBaseUri, string apiKey, int imageId)
/// <summary>
/// Applies a Derpibooru filter
/// </summary>
/// <param name="filterId">A user or system filter ID (See https://www.derpibooru.org/filters) </param>
public DerpiGetImage WithFilterId(int filterId)
/// <param name="filterId">A user or system filter ID (See https://www.derpibooru.org/filters)</param>
public IGetImage WithFilterId(int filterId)
{
_uri = _uri.SetQueryParam("filter_id", filterId);
return this;
Expand Down
24 changes: 12 additions & 12 deletions src/DerpiSearch.cs → src/UrlBuilder/Derpibooru/DerpiSearch.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Flurl;

namespace Nullforce.Api.Derpibooru;
namespace Nullforce.Api.UrlBuilder.Derpibooru;

public class DerpiSearch : DerpiBase
public class DerpiSearch : DerpiBase, ISearch
{
public DerpiSearch(string apiBaseUri, string apiKey)
: base(apiBaseUri, apiKey)
Expand All @@ -28,7 +28,7 @@ public DerpiSearch(string apiBaseUri, string apiKey)
/// Search my watched: my:watched, !my:watched
/// </remarks>
/// <param name="query">A query string following the syntax at https://derpibooru.org/pages/search_syntax </param>
public DerpiSearch WithQuery(string query)
public ISearch WithQuery(string query)
{
// TODO: query validation
_uri = _uri.SetQueryParam("q", query);
Expand All @@ -39,7 +39,7 @@ public DerpiSearch WithQuery(string query)
/// Applies a Derpibooru filter
/// </summary>
/// <param name="filterId">A user or system filter ID (See https://www.derpibooru.org/filters) </param>
public DerpiSearch WithFilterId(int filterId)
public ISearch WithFilterId(int filterId)
{
_uri = _uri.SetQueryParam("filter_id", filterId);
return this;
Expand All @@ -51,14 +51,14 @@ public DerpiSearch WithFilterId(int filterId)
/// <param name="filter">A system filter (See https://www.derpibooru.org/filters) </param>
public DerpiSearch WithFilterId(DerpiSystemFilter filter)
{
return WithFilterId((int)filter);
return WithFilterId((int)filter) as DerpiSearch;
}

/// <summary>
/// Applies a Derpibooru sort option
/// </summary>
/// <param name="sortOption">A sort option</param>
public DerpiSearch SortBy(string sort)
public ISearch SortBy(string sort)
{
_uri = _uri.SetQueryParam("sf", sort);
return this;
Expand Down Expand Up @@ -107,13 +107,13 @@ public DerpiSearch SortBy(DerpiSortOptions sortOption)
break;
}

return SortBy(sort);
return SortBy(sort) as DerpiSearch;
}

/// <summary>
/// Sorts the results in ascending order
/// </summary>
public DerpiSearch SortAscending()
public ISearch SortAscending()
{
_uri = _uri.SetQueryParam("sd", "asc");
return this;
Expand All @@ -122,7 +122,7 @@ public DerpiSearch SortAscending()
/// <summary>
/// Sorts the results in descending order
/// </summary>
public DerpiSearch SortDescending()
public ISearch SortDescending()
{
_uri = _uri.SetQueryParam("sd", "desc");
return this;
Expand All @@ -133,7 +133,7 @@ public DerpiSearch SortDescending()
/// </summary>
/// <param name="page">A 1-indexed page number</param>
/// <returns></returns>
public DerpiSearch Page(int page)
public ISearch Page(int page)
{
_uri = _uri.SetQueryParam("page", page);
return this;
Expand All @@ -144,9 +144,9 @@ public DerpiSearch Page(int page)
/// </summary>
/// <param name="limit">A limit between 1 and 50</param>
/// <returns></returns>
public DerpiSearch PerPage(int limit)
public ISearch PerPage(int limit)
{
_uri = _uri.SetQueryParam("perpage", limit);
_uri = _uri.SetQueryParam("per_page", limit);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Nullforce.Api.Derpibooru;
namespace Nullforce.Api.UrlBuilder.Derpibooru;

public enum DerpiSortOptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Nullforce.Api.Derpibooru;
namespace Nullforce.Api.UrlBuilder.Derpibooru;

public enum DerpiSystemFilter
{
Expand Down
6 changes: 6 additions & 0 deletions src/UrlBuilder/IGetFeaturedImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Nullforce.Api.UrlBuilder;

public interface IGetFeaturedImage
{
public string Uri { get; }
}
12 changes: 12 additions & 0 deletions src/UrlBuilder/IGetImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Nullforce.Api.UrlBuilder;

public interface IGetImage
{
public string Uri { get; }

/// <summary>
/// Applies a Derpibooru filter
/// </summary>
/// <param name="filterId">A user or system filter ID (See https://www.derpibooru.org/filters) </param>
public IGetImage WithFilterId(int filterId);
}
53 changes: 53 additions & 0 deletions src/UrlBuilder/ISearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Nullforce.Api.UrlBuilder;

public interface ISearch
{
public string Uri { get; }

/// <summary>
/// Specifies a search query filter
/// </summary>
/// <remarks>
/// Search my favorites: my:faves, !my:faves
/// Search my upvotes: my:upvotes, !my:upvotes
/// Search my downvotes: my:downvotes, !my:downvotes
/// Search my uploads: my:uploads, !my:uploads
/// Search my watched: my:watched, !my:watched
/// </remarks>
/// <param name="query">A query string following the syntax at https://derpibooru.org/pages/search_syntax </param>
public ISearch WithQuery(string query);

/// <summary>
/// Applies a Derpibooru filter
/// </summary>
/// <param name="filterId">A user or system filter ID (See https://www.derpibooru.org/filters) </param>
public ISearch WithFilterId(int filterId);

/// <summary>
/// Applies a Derpibooru sort option
/// </summary>
/// <param name="sortOption">A sort option</param>
public ISearch SortBy(string sort);

/// <summary>
/// Sorts the results in ascending order
/// </summary>
public ISearch SortAscending();

/// <summary>
/// Sorts the results in descending order
/// </summary>
public ISearch SortDescending();

/// <summary>
/// The page number for the search results
/// </summary>
/// <param name="page">A 1-indexed page number</param>
public ISearch Page(int page);

/// <summary>
/// The number of search results to return per page
/// </summary>
/// <param name="limit">A limit between 1 and 50</param>
public ISearch PerPage(int limit);
}
15 changes: 15 additions & 0 deletions src/UrlBuilder/Twibooru/TwibooruBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Nullforce.Api.UrlBuilder.Twibooru;

public abstract class TwibooruBase
{
protected readonly string _apiBaseUri;
protected readonly string _apiKey;
protected string _uri;
public string Uri => _uri;

public TwibooruBase(string apiBaseUri, string apiKey)
{
_apiBaseUri = apiBaseUri;
_apiKey = apiKey;
}
}
39 changes: 39 additions & 0 deletions src/UrlBuilder/Twibooru/TwibooruClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Nullforce.Api.UrlBuilder.Twibooru;

public class TwibooruClient
{
private readonly string _apiBaseUri = "https://twibooru.org/api/v3";
private readonly string _apiKey;

public TwibooruClient()
{
}

public TwibooruClient(string apiKey)
{
_apiKey = apiKey;
}

public TwibooruGetImage GetImage(int imageId)
{
return new TwibooruGetImage(_apiBaseUri, _apiKey, imageId);
}

/// <summary>
/// Gets the image response for the featured image
/// </summary>
/// <returns>A fluent API wrapper for featured image</returns>
public TwibooruGetFeaturedImage GetFeaturedImage()
{
return new TwibooruGetFeaturedImage(_apiBaseUri, _apiKey);
}

/// <summary>
/// Exposes the Twibooru Search as a Fluent API.
/// </summary>
/// <returns>A fluent API wrapper for search</returns>
public TwibooruSearch Search()
{
return new TwibooruSearch(_apiBaseUri, _apiKey);
}
}
12 changes: 12 additions & 0 deletions src/UrlBuilder/Twibooru/TwibooruGetFeaturedImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Flurl;

namespace Nullforce.Api.UrlBuilder.Twibooru;

public class TwibooruGetFeaturedImage : TwibooruBase, IGetFeaturedImage
{
public TwibooruGetFeaturedImage(string apiBaseUri, string apiKey)
: base(apiBaseUri, apiKey)
{
_uri = apiBaseUri.AppendPathSegment("posts/featured");
}
}
22 changes: 22 additions & 0 deletions src/UrlBuilder/Twibooru/TwibooruGetImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Flurl;

namespace Nullforce.Api.UrlBuilder.Twibooru;

public class TwibooruGetImage : TwibooruBase, IGetImage
{
public TwibooruGetImage(string apiBaseUri, string apiKey, int imageId)
: base(apiBaseUri, apiKey)
{
_uri = apiBaseUri.AppendPathSegment($"images/{imageId}");
}

/// <summary>
/// Applies a Twibooru filter
/// </summary>
/// <param name="filterId">A user or system filter ID</param>
public IGetImage WithFilterId(int filterId)
{
_uri = _uri.SetQueryParam("filter_id", filterId);
return this;
}
}
Loading

0 comments on commit 9290a32

Please sign in to comment.