Skip to content

Commit

Permalink
Added asuna.ga as an image source
Browse files Browse the repository at this point in the history
  • Loading branch information
Serious-senpai committed Jan 15, 2023
1 parent 8614ac3 commit cbea3ad
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ abstract class ApplicationException implements Exception {

class RequestCancelledException implements ApplicationException {
@override
final String message = "Request to fetch image has been cancelled";
final message = "Request to fetch image has been cancelled";
}
53 changes: 44 additions & 9 deletions lib/sources.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ abstract class ImageSource {

class _BaseImageSource extends ImageSource {
@override
final Set<String> sfw = <String>{};
final sfw = <String>{};

@override
final Set<String> nsfw = <String>{};
final nsfw = <String>{};

@override
String get baseUrl => throw UnimplementedError;
Expand Down Expand Up @@ -75,11 +75,11 @@ class _BaseImageSource extends ImageSource {
}
}

class WaifuPics extends _BaseImageSource {
class WaifuPICS extends _BaseImageSource {
@override
final String baseUrl = "api.waifu.pics";
final baseUrl = "api.waifu.pics";

WaifuPics(ImageClient client) : super(client);
WaifuPICS(ImageClient client) : super(client);

@override
Future<void> populateCategories() async {
Expand All @@ -100,11 +100,11 @@ class WaifuPics extends _BaseImageSource {
}
}

class WaifuIm extends _BaseImageSource {
class WaifuIM extends _BaseImageSource {
@override
final String baseUrl = "api.waifu.im";
final baseUrl = "api.waifu.im";

WaifuIm(ImageClient client) : super(client);
WaifuIM(ImageClient client) : super(client);

@override
Future<void> populateCategories() async {
Expand Down Expand Up @@ -145,6 +145,41 @@ class WaifuIm extends _BaseImageSource {
}
}

class AsunaGA extends _BaseImageSource {
@override
final baseUrl = "asuna.ga";

final _urlMap = <String, String>{};

AsunaGA(ImageClient client) : super(client);

@override
Future<void> populateCategories() async {
final converter = <String, String>{"wholesome_foxes": "foxes"};

var response = await http.get(Uri.https("asuna.ga", "/api"));
var data = json.decode(response.body);
for (var tag in data["allEndpoints"]) {
var url = data["endpointInfo"][tag]["url"];
tag = converter[tag] ?? tag;
_urlMap[tag] = url;

sfw.add(tag);
}
}

@override
Future<String> getImageUrl(String category, {required bool isSfw}) async {
var response = await http.get(Uri.parse(_urlMap[category]!));
var data = json.decode(response.body);
return data["url"];
}
}

List<ImageSource> constructSources(ImageClient client) {
return <ImageSource>[WaifuPics(client), WaifuIm(client)];
return <ImageSource>[
WaifuPICS(client),
WaifuIM(client),
AsunaGA(client),
];
}

0 comments on commit cbea3ad

Please sign in to comment.