Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

Commit

Permalink
Add dynamikcli
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffResc committed May 29, 2020
1 parent b860406 commit 58f6fc3
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 0 deletions.
17 changes: 17 additions & 0 deletions dynamikcli/DynamikController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
This file is heavily based off StellaeController.h by Zachary Thomas Paul <[email protected]>
Thank you to Stellae for making this part easy :)
https://github.com/LacertosusRepo/Open-Source-Tweaks/blob/master/Stellae/StellaeController.h
*/

@interface DynamikController : NSObject
+(id)sharedInstance;
-(id)init;
-(BOOL)dataIsValidImage:(NSData *)data;
-(UIImage *)getImageFromReddit:(NSString *)subredditName numberOfPostsGrabbed:(int)postsGrabbed nsfwFiltered:(BOOL)nsfwFilter;
-(NSString *)getFinalRedditURL:(NSString *)subredditName numberOfPostsGrabbed:(int)postsGrabbed;
-(NSDictionary *)getRedditJSONData:(NSString *)redditURL;
-(BOOL)postIsNSFW:(int)postNumber fromDictionary:(NSDictionary *)dictionary;
-(void)saveURL:(NSString *)URL forKey:(NSString *)key;
-(NSArray *)excludedProviders;
@end
115 changes: 115 additions & 0 deletions dynamikcli/DynamikController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
This file is heavily based off StellaeController.m by Zachary Thomas Paul <[email protected]>
Thank you to Stellae for making this part easy :)
https://github.com/LacertosusRepo/Open-Source-Tweaks/blob/master/Stellae/StellaeController.m
*/
#import "DynamikController.h"

@implementation DynamikController
+(id)sharedInstance {
static DynamikController *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}

-(id)init {
if(self = [super init]) {
NSLog(@"Dynamik || Initalized");
}
return self;
}

// Thanks! - https://stackoverflow.com/a/5042365
- (BOOL)dataIsValidImage:(NSData *)data {
uint8_t c;
[data getBytes:&c length:1];

switch (c) {
case 0xFF:
return true;
case 0x89:
return true;
}
return false;
}

-(UIImage *)getImageFromReddit:(NSString *)subredditName numberOfPostsGrabbed:(int)postsGrabbed nsfwFiltered:(BOOL)nsfwFilter {
NSString *finalRedditURL = [self getFinalRedditURL:subredditName numberOfPostsGrabbed:postsGrabbed];
NSDictionary *redditJSONDictionary = [self getRedditJSONData:finalRedditURL];
int postNumber = arc4random_uniform([redditJSONDictionary[@"data"][@"children"] count]);
BOOL postIsNSFW = [self postIsNSFW:postNumber fromDictionary:redditJSONDictionary];

if(nsfwFilter && postIsNSFW) {
NSLog(@"Dynamik || NSFW filter is on and post is NSFW - %d", postIsNSFW);
return nil;
} else {
NSString *redditImageURL = redditJSONDictionary[@"data"][@"children"][postNumber][@"data"][@"url"];
NSLog(@"Dynamik || Found image at URL: %@", redditImageURL);
if([redditImageURL containsString:@"imgur.com"] && ![redditImageURL containsString:@"i.imgur"]) {
redditImageURL = [redditImageURL stringByAppendingString:@".jpg"];
}

NSString *postURL = @"https://reddit.com";
postURL = [postURL stringByAppendingString:redditJSONDictionary[@"data"][@"children"][postNumber][@"data"][@"permalink"]];
[self saveURL:postURL forKey:@"currentRedditURL"];
[self saveURL:redditImageURL forKey:@"currentImageURL"];

NSLog(@"Dynamik || Downloading image...");
NSData *redditImageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:redditImageURL]];
NSLog(@"Dynamik || Finished downloading...");
if ([self dataIsValidImage:redditImageData]) {
UIImage *redditUIImage = [UIImage imageWithData:redditImageData];
return redditUIImage;
} else {
NSLog(@"Dynamik || Image invalid, not PNG or JPEG...");
return nil;
}
}

return nil;
}

-(NSString *)getFinalRedditURL:(NSString *)subredditName numberOfPostsGrabbed:(int)postsGrabbed {
if([subredditName isEqualToString:@""] || [subredditName containsString:@"r/"] || [subredditName containsString:@" "]) {
NSLog(@"Dynamik || Error with subredditName - %@", subredditName);
subredditName = @"spaceporn";
}

NSString *redditURL = [NSString stringWithFormat:@"https://reddit.com/r/%@/hot.json?limit=%d", subredditName, postsGrabbed];
return redditURL;
}

-(NSDictionary *)getRedditJSONData:(NSString *)redditURL {
NSError *error = nil;
NSURL *redditJSONURL = [NSURL URLWithString:redditURL];
NSData *redditJSONData = [NSData dataWithContentsOfURL:redditJSONURL];
NSDictionary *redditJSONDictionary = nil;

if(redditJSONData != nil) {
redditJSONDictionary = [NSJSONSerialization JSONObjectWithData:redditJSONData options:0 error:&error];
} else {
NSLog(@"Dynamik || Error getting data - %@", error);
return nil;
}

return redditJSONDictionary;
}

-(BOOL)postIsNSFW:(int)postNumber fromDictionary:(NSDictionary *)dictionary {
return [dictionary[@"data"][@"children"][postNumber][@"data"][@"over_18"] boolValue];
}

-(void)saveURL:(NSString *)URL forKey:(NSString *)key {
NSString *file = @"/User/Library/Preferences/com.jeffresc.dynamiksaveddata.plist";
NSMutableDictionary *saveddata = [[NSMutableDictionary alloc] initWithContentsOfFile:file];
[saveddata setObject:URL forKey:key];
[saveddata writeToFile:file atomically:YES];
}

-(NSArray *)excludedProviders {
return [NSArray arrayWithObjects:@"gfycat.com", nil];
}
@end
14 changes: 14 additions & 0 deletions dynamikcli/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# The dynamikcli subproject is a fork of WallpaperChanger by Capt Inc
# Thank you so much to them for their work, it helped so much
# Please checkout his project here if you are interested: https://github.com/captinc/WallpaperChanger

ARCHS = arm64 arm64e
TARGET = iphone:clang::11.0
include $(THEOS)/makefiles/common.mk

TOOL_NAME = dynamikcli
dynamikcli_FILES = main.m DynamikController.m
dynamikcli_CFLAGS = -fobjc-arc
dynamikcli_FRAMEWORKS = UIKit

include $(THEOS_MAKE_PATH)/tool.mk
125 changes: 125 additions & 0 deletions dynamikcli/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#import <stdio.h>
#import <string.h>
#import <dlfcn.h>
#import <objc/runtime.h>
#import "DynamikController.h"

void performSelectorWithInteger(id parent, SEL selector, NSInteger integer) {
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[parent methodSignatureForSelector:selector]];
[inv setTarget:parent];
[inv setSelector:selector];
[inv setArgument:&integer atIndex:2];
[inv invoke];
}

int setWallpaper(NSString *subreddit, int numberOfPostsGrabbed, int location, bool parallax, bool nsfwFilter, bool highQualityFilter) {
int tries = 0;
while (tries < 5) {
UIImage *newImage = [[DynamikController sharedInstance] getImageFromReddit:subreddit numberOfPostsGrabbed:numberOfPostsGrabbed nsfwFiltered:nsfwFilter];
if (newImage == nil) {
NSLog(@"Recieved invalid image, trying again...");
tries++;
continue;
}
dlopen("/System/Library/PrivateFrameworks/SpringBoardFoundation.framework/SpringBoardFoundation", RTLD_LAZY);
void *SBUIServs = dlopen("/System/Library/PrivateFrameworks/SpringBoardUIServices.framework/SpringBoardUIServices", RTLD_LAZY);

id lightOptions = [[objc_getClass("SBFWallpaperOptions") alloc] init];
id darkOptions = [[objc_getClass("SBFWallpaperOptions") alloc] init];

if (!parallax) {
performSelectorWithInteger(lightOptions, @selector(setParallaxFactor:), 0);
performSelectorWithInteger(darkOptions, @selector(setParallaxFactor:), 0);
}

if (@available(iOS 13, *)) {
int (*SBSUIWallpaperSetImages)(NSDictionary *imagesDict, NSDictionary *optionsDict, int location, int interfaceStyle) = dlsym(SBUIServs, "SBSUIWallpaperSetImages");

performSelectorWithInteger(lightOptions, @selector(setWallpaperMode:), 1);
performSelectorWithInteger(darkOptions, @selector(setWallpaperMode:), 2);
SBSUIWallpaperSetImages(@{@"light":newImage, @"dark":newImage}, @{@"light":lightOptions, @"dark":darkOptions}, location, UIUserInterfaceStyleDark);
}
else {
void (*SBSUIWallpaperSetImage)(UIImage *image, NSDictionary *optionsDict, NSInteger location) = dlsym(SBUIServs, "SBSUIWallpaperSetImage");
SBSUIWallpaperSetImage(newImage, lightOptions, location);
}
return 0;
}

NSLog(@"Tried too many times, quitting...");
return 1;
}

void displayUsage() {
printf("Usage: dynamikcli -s [subreddit] -m [# of posts] [location] [-p] [-n] [-q]\n");
printf(" -s\tSubreddit\n");
printf(" For a full list of suggested subreddits, see the GitHub page at https://github.com/JeffResc/Dynamik/\n");
printf("\n");
printf(" -m\tNumber of posts\n");
printf(" Number of hot posts to grab, it will choose a random image from one of them\n");
printf("\n");
printf(" -l\tSet only the lock screen wallpaper\n");
printf(" -h\tSet only the home screen wallpaper\n");
printf(" -b\tSet both wallpapers\n");
printf(" Choose between -h, -l, and -b. Do not specify more than one\n");
printf("\n");
printf(" -p\tEnable parallax (optional parameter - parallax is off by default)\n");
printf("\n");
printf(" -n\tEnable NSFW Filter (optional parameter - NSFW Filter is off by default)\n");
printf("\n");
printf(" -q\tEnable High-Quality Filter (optional parameter - High-Quality Filter is off by default)\n");
printf("\n");
printf(" --help\tShow this help page\n");
printf("\n");
printf(" All arguments are required except -p\n");
}

int main(int argc, char *argv[], char *envp[]) {
if (argc == 1 || !strcmp(argv[1], "--help")) {
displayUsage();
return 1;
}

int location;
int numberOfPostsGrabbed;
NSString *subreddit;
bool parallax = false;
bool nsfwFilter = false;
bool highQualityFilter = false;

int args = 0;
for (int i = 1; i < argc; i++) { //parse the arguments
if (!strcmp(argv[i], "-s")) {
subreddit = [NSString stringWithUTF8String:argv[i + 1]];
args++;
} else if (!strcmp(argv[i], "-m")) {
numberOfPostsGrabbed = atoi(argv[i + 1]);
if (numberOfPostsGrabbed > 0 && numberOfPostsGrabbed <= 10) {
args++;
}
} else if (!strcmp(argv[i], "-l")) {
location = 1;
args++;
} else if (!strcmp(argv[i], "-h")) {
location = 2;
args++;
} else if (!strcmp(argv[i], "-b")) {
location = 3;
args++;
} else if (!strcmp(argv[i], "-p")) {
parallax = true;
} else if (!strcmp(argv[i], "-n")) {
nsfwFilter = true;
} else if (!strcmp(argv[i], "-q")) {
highQualityFilter = true;
}
}

if (args == 3) {
return setWallpaper(subreddit, numberOfPostsGrabbed, location, parallax, nsfwFilter, highQualityFilter);
} else {
displayUsage();
return 1;
}
return 0;
}

0 comments on commit 58f6fc3

Please sign in to comment.