82 lines
2.4 KiB
Mathematica
82 lines
2.4 KiB
Mathematica
|
//
|
||
|
// BooruClient.m
|
||
|
// cunnyfinder
|
||
|
//
|
||
|
// Created by James Shiffer on 12/19/22.
|
||
|
// Copyright © 2022 FemboyFinancial. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "BooruClient.h"
|
||
|
|
||
|
@implementation BooruClient
|
||
|
|
||
|
-(id)init:(NSArray *)waifus
|
||
|
{
|
||
|
self.urlFormat = @"https://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=1&tags=%@%%20%@";
|
||
|
self.tags = @"score%3A%3E%3D10%20sort%3Arandom%201girl%20solo";
|
||
|
self.waifus = [[NSMutableArray alloc] initWithArray:waifus];
|
||
|
self.metadata = nil;
|
||
|
return self;
|
||
|
}
|
||
|
|
||
|
+(NSString *)urlEncode:(NSString*)str
|
||
|
{
|
||
|
CFStringRef urlString = CFURLCreateStringByAddingPercentEscapes(
|
||
|
NULL,
|
||
|
(CFStringRef)str,
|
||
|
NULL,
|
||
|
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
|
||
|
kCFStringEncodingUTF8
|
||
|
);
|
||
|
return (NSString *)CFBridgingRelease(urlString);
|
||
|
}
|
||
|
|
||
|
|
||
|
-(BooruImage *)search
|
||
|
{
|
||
|
NSString* randomWaifu = self.waifus[arc4random_uniform(self.waifus.count)];
|
||
|
NSLog(@"searching for pics of %@", randomWaifu);
|
||
|
NSString* urlStr = [[NSString alloc] initWithFormat:self.urlFormat, self.tags, [BooruClient urlEncode:randomWaifu]];
|
||
|
NSURL* url = [NSURL URLWithString:urlStr];
|
||
|
NSXMLParser* xml = [[NSXMLParser alloc] initWithContentsOfURL:url];
|
||
|
BooruXMLParserDelegate* parser = [[BooruXMLParserDelegate alloc] init];
|
||
|
[xml setDelegate:parser];
|
||
|
[xml setShouldProcessNamespaces:NO];
|
||
|
[xml setShouldReportNamespacePrefixes:NO];
|
||
|
[xml setShouldResolveExternalEntities:NO];
|
||
|
[xml parse];
|
||
|
if ([xml parserError])
|
||
|
{
|
||
|
NSLog(@"parsing error: %@", [xml parserError]);
|
||
|
}
|
||
|
NSLog(@"found image id: %@", parser.data[@"id"]);
|
||
|
return [[BooruImage alloc] init:parser.data];
|
||
|
}
|
||
|
|
||
|
-(void)downloadAndDisplayImage:(NSURL *)url inView:(NSImageView *)imageView
|
||
|
{
|
||
|
self.imageView = imageView;
|
||
|
NSOperationQueue* queue = [NSOperationQueue new];
|
||
|
NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage:) object:url];
|
||
|
[queue addOperation:operation];
|
||
|
}
|
||
|
|
||
|
-(void)downloadImage:(NSURL *)url
|
||
|
{
|
||
|
NSData* imageData = [[NSData alloc] initWithContentsOfURL:url];
|
||
|
NSImage* image = [[NSImage alloc] initWithData:imageData];
|
||
|
[self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:YES];
|
||
|
}
|
||
|
|
||
|
-(void)displayImage:(NSImage *)image
|
||
|
{
|
||
|
self.imageView.image = image;
|
||
|
}
|
||
|
|
||
|
-(void)reset
|
||
|
{
|
||
|
self.metadata = nil;
|
||
|
}
|
||
|
|
||
|
@end
|