This repository has been archived by the owner on Jun 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAutoExceptions.m
165 lines (161 loc) · 7.9 KB
/
AutoExceptions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// AutoExceptions.m
// Hachidori
//
// Created by Tail Red on 1/31/15.
// Copyright 2015 MAL Updater OS X Group. All rights reserved. Code licensed under New BSD License
//
#import "AutoExceptions.h"
#import <EasyNSURLConnection/EasyNSURLConnection.h>
#import "MAL_Updater_OS_XAppDelegate.h"
#import "Utility.h"
@implementation AutoExceptions
#pragma mark Importing Exceptions and Auto Exceptions
+ (void)importToCoreData{
MAL_Updater_OS_XAppDelegate *delegate = (MAL_Updater_OS_XAppDelegate *)[NSApplication sharedApplication].delegate;
NSManagedObjectContext *moc = delegate.managedObjectContext;
// Check Exceptions
NSArray *oexceptions = [[NSUserDefaults standardUserDefaults] objectForKey:@"exceptions"];
if (oexceptions.count > 0) {
NSLog(@"Importing Exception List");
NSFetchRequest *allExceptions = [[NSFetchRequest alloc] init];
allExceptions.entity = [NSEntityDescription entityForName:@"Exceptions" inManagedObjectContext:moc];
NSError *error = nil;
NSArray *exceptions = [moc executeFetchRequest:allExceptions error:&error];
for (NSDictionary *d in oexceptions) {
NSString *title = d[@"detectedtitle"];
BOOL exists = false;
for (NSManagedObject *entry in exceptions) {
if ([title isEqualToString:(NSString *)[entry valueForKey:@"detectedTitle"]]) {
exists = true;
break;
}
}
if (!exists) {
NSString *correcttitle = (NSString *)d[@"correcttitle"];
NSString *showid = (NSString *)d[@"showid"];
// Add Exceptions to Core Data
NSManagedObject *obj = [NSEntityDescription
insertNewObjectForEntityForName:@"Exceptions"
inManagedObjectContext: moc];
// Set values in the new record
[obj setValue:title forKey:@"detectedTitle"];
[obj setValue:correcttitle forKey:@"correctTitle"];
[obj setValue:showid forKey:@"id"];
[obj setValue:@0 forKey:@"episodeOffset"];
[obj setValue:@0 forKey:@"episodethreshold"];
}
}
//Save
[moc save:&error];
// Erase exceptions data from preferences
[[NSUserDefaults standardUserDefaults] setObject:[[NSMutableArray alloc] init] forKey:@"exceptions"];
}
}
+ (void)updateAutoExceptions{
// This method retrieves the auto exceptions JSON and import new entries
NSURL *url = [NSURL URLWithString:@"https://exceptions.ateliershiori.moe"];
EasyNSURLConnection *request = [[EasyNSURLConnection alloc] initWithURL:url];
[Utility setUserAgent:request];
//Ignore Cookies
[request setUseCookies:NO];
//Test API
[request startRequest];
// Get Status Code
long statusCode = [request getStatusCode];
switch (statusCode) {
case 200:{
NSLog(@"Updating Auto Exceptions!");
if (![[NSUserDefaults standardUserDefaults] valueForKey:@"updatedaexceptions"]) {
[AutoExceptions clearAutoExceptions];
[[NSUserDefaults standardUserDefaults] setObject:@(true)forKey:@"updatedaexceptions"];
}
//Parse and Import
NSData *jsonData = request.response.responsedata;
NSError *error = nil;
NSArray *a;
@try {
a = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
}
@catch (NSException *e) {
NSLog(@"Unable to refresh auto exceptions database: %@", e);
return;
}
MAL_Updater_OS_XAppDelegate *delegate = (MAL_Updater_OS_XAppDelegate *)[NSApplication sharedApplication].delegate;
NSManagedObjectContext *moc = delegate.managedObjectContext;
[moc performBlockAndWait:^{
for (NSDictionary *d in a) {
NSString *detectedtitle = d[@"detectedtitle"];
NSString *group = d[@"group"];
NSString *correcttitle = d[@"correcttitle"];
bool iszeroepisode = ((NSNumber *)d[@"iszeroepisode"]).boolValue;
int offset = ((NSNumber *)d[@"offset"]).intValue;
NSError *derror = nil;
NSManagedObject *obj = [self checkAutoExceptionsEntry:detectedtitle group:group correcttitle:correcttitle zeroepisode:iszeroepisode offset:offset];
if (obj) {
// Update Entry
[obj setValue:d[@"offset"] forKey:@"episodeOffset"];
[obj setValue:d[@"threshold"] forKey:@"episodethreshold"];
}
else {
// Add Entry to Auto Exceptions
obj = [NSEntityDescription
insertNewObjectForEntityForName:@"AutoExceptions"
inManagedObjectContext: moc];
// Set values in the new record
[obj setValue:detectedtitle forKey:@"detectedTitle"];
[obj setValue:correcttitle forKey:@"correctTitle"]; // Use Universal Correct Title
[obj setValue:d[@"offset"] forKey:@"episodeOffset"];
[obj setValue:d[@"threshold"] forKey:@"episodethreshold"];
[obj setValue:group forKey:@"group"];
[obj setValue:@(iszeroepisode) forKey:@"iszeroepisode"];
[obj setValue:d[@"mappedepisode"] forKey:@"mappedepisode"];
}
//Save
[moc save:&derror];
}
}];
// Set the last updated date
[[NSUserDefaults standardUserDefaults] setValue:[NSDate date] forKey:@"ExceptionsLastUpdated"];
break;
}
default:
NSLog(@"Auto Exceptions List Update Failed!");
break;
}
}
+ (void)clearAutoExceptions{
// Remove All cache data from Auto Exceptions
MAL_Updater_OS_XAppDelegate *delegate = (MAL_Updater_OS_XAppDelegate *)[NSApplication sharedApplication].delegate;
NSManagedObjectContext *moc = delegate.managedObjectContext;
NSFetchRequest *allExceptions = [[NSFetchRequest alloc] init];
allExceptions.entity = [NSEntityDescription entityForName:@"AutoExceptions" inManagedObjectContext:moc];
NSError *error = nil;
NSArray *exceptions = [moc executeFetchRequest:allExceptions error:&error];
//error handling goes here
for (NSManagedObject *exception in exceptions) {
[moc deleteObject:exception];
}
error = nil;
[moc save:&error];
}
+ (NSManagedObject *)checkAutoExceptionsEntry:(NSString *)ctitle
group:(NSString *)group
correcttitle:(NSString *)correcttitle
zeroepisode:(bool)zeroepisode
offset:(int)offset{
// Return existing offline queue item
NSError *error;
MAL_Updater_OS_XAppDelegate *delegate = (MAL_Updater_OS_XAppDelegate *)[NSApplication sharedApplication].delegate;
NSManagedObjectContext *moc = delegate.managedObjectContext;
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(detectedTitle ==[c] %@) AND (correctTitle == %@) AND (group ==[c] %@) AND (iszeroepisode == %i) AND (episodeOffset == %i)", ctitle,correcttitle, group, zeroepisode, offset] ;
NSFetchRequest *exfetch = [[NSFetchRequest alloc] init];
exfetch.entity = [NSEntityDescription entityForName:@"AutoExceptions" inManagedObjectContext:moc];
exfetch.predicate = predicate;
NSArray *exceptions = [moc executeFetchRequest:exfetch error:&error];
if (exceptions.count > 0) {
return (NSManagedObject *)exceptions[0];
}
return nil;
}
@end