forked from Atelier-Shiori/hachidori
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHachidori+MultiScrobble.m
278 lines (268 loc) · 14.7 KB
/
Hachidori+MultiScrobble.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// Hachidori+MultiScrobble.m
// Hachidori
//
// Created by 香風智乃 on 1/14/19.
//
#import "Hachidori+MultiScrobble.h"
#import "Hachidori+UserStatus.h"
#import "Hachidori+Update.h"
#import <AFNetworking/AFNetworking.h>
#import "ScoreConversion.h"
@implementation Hachidori (MultiScrobble)
- (void)multiscrobbleWithType:(MultiScrobbleType)scrobbletype withTitleID:(NSString *)titleid {
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
if ([defaults boolForKey:@"multiscrobbleenabled"]) {
NSDictionary *mapping = [self lookupmappings:titleid];
switch (scrobbletype) {
case MultiScrobbleTypeScrobble:
case MultiScrobbleTypeCorrection:
[self performMultiScrobbleScrobbleWithMapping:mapping withScrobbleType:scrobbletype];
break;
case MultiScrobbleTypeEntryupdate:
[self performMultiScrobbleEntryUpdateWithMapping:mapping];
break;
case MultiScrobbleTypeRevertRewatch:
[self performMultiScrobbleRevertRewatchingWithMapping:mapping];
break;
default:
break;
}
}
}
- (void)performMultiScrobbleScrobbleWithMapping:(NSDictionary *)mapping withScrobbleType:(MultiScrobbleType)type {
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
if (type == MultiScrobbleTypeCorrection && self.correcting && ![defaults boolForKey:@"multiscrobblescorrectionsenabled"]) {
return;
}
else if (type == MultiScrobbleTypeScrobble && !self.correcting && ![defaults boolForKey:@"multiscrobblescrobblesenabled"]) {
return;
}
// Perform MultiScrobble Scrobble
if ([defaults boolForKey:@"multiscrobblekitsuenabled"] && [Hachidori currentService] != 0) {
if ([Hachidori getFirstAccount:0]) {
if (mapping[@"kitsu_id"] != [NSNull null] && ((NSNumber *)mapping[@"kitsu_id"]).intValue > 0) {
// Obtain Entry
self.kitsumanager.detectedscrobble = self.detectedscrobble.copy;
self.kitsumanager.detectedscrobble.AniID = ((NSNumber *)mapping[@"kitsu_id"]).stringValue;
if ([self checkstatus:self.kitsumanager.detectedscrobble.AniID withService:0]) {
if ([self shouldMultiScrobble:self.kitsumanager.detectedscrobble]) {
int status = [self performupdate:self.kitsumanager.detectedscrobble.AniID withService:0];
switch (status) {
case 21:
case 22:
[NSNotificationCenter.defaultCenter postNotificationName:@"MultiScrobbleNotification" object:@{@"title" : @"MultiScrobble", @"message" : self.correcting ? @"Correction was successful on Kitsu" : @"Scrobble was successful on Kitsu", @"identifier" : @"multiscrobble-kitsu"}];
break;
default:
[NSNotificationCenter.defaultCenter postNotificationName:@"MultiScrobbleNotification" object:@{@"title" : @"MultiScrobble", @"message" : self.correcting ? @"Correction was not successful on Kitsu" : @"Scrobble was not successful on Kitsu", @"identifier" : @"multiscrobble-kitsu"}];
break;
}
}
else {
[self.kitsumanager kitsustoreLastScrobbled];
}
}
}
}
}
else if ([defaults boolForKey:@"multiscrobbleanilistenabled"] && [Hachidori currentService] != 1) {
if ([Hachidori getFirstAccount:1]) {
if (mapping[@"anilist_id"] != [NSNull null] && ((NSNumber *)mapping[@"anilist_id"]).intValue > 0) {
// Obtain Entry
self.anilistmanager.detectedscrobble = self.detectedscrobble.copy;
self.anilistmanager.detectedscrobble.AniID = ((NSNumber *)mapping[@"anilist_id"]).stringValue;
if ([self checkstatus:self.anilistmanager.detectedscrobble.AniID withService:1]) {
if ([self shouldMultiScrobble:self.anilistmanager.detectedscrobble]) {
int status = [self performupdate:self.anilistmanager.detectedscrobble.AniID withService:1];
switch (status) {
case 21:
case 22:
[NSNotificationCenter.defaultCenter postNotificationName:@"MultiScrobbleNotification" object:@{@"title" : @"MultiScrobble", @"message" : self.correcting ? @"Correction was successful on AniList" : @"Scrobble was successful on AniList", @"identifier" : @"multiscrobble-anilist"}];
break;
default:
[NSNotificationCenter.defaultCenter postNotificationName:@"MultiScrobbleNotification" object:@{@"title" : @"MultiScrobble", @"message" : self.correcting ? @"Correction was not successful on AniList" : @"Scrobble was not successful on AniList", @"identifier" : @"multiscrobble-anilist"}];
break;
}
}
else {
[self.anilistmanager aniliststoreLastScrobbled];
}
}
}
}
}
}
- (void)performMultiScrobbleEntryUpdateWithMapping:(NSDictionary *)mapping {
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
if ([defaults boolForKey:@"multiscrobbleentryupdatesenabled"]) {
// Perform entry update
if ([defaults boolForKey:@"multiscrobblekitsuenabled"] && [Hachidori currentService] != 0) {
if ([Hachidori getFirstAccount:0] && self.kitsumanager.lastscrobble) {
if (mapping[@"kitsu_id"] != [NSNull null] && ((NSNumber *)mapping[@"kitsu_id"]).intValue > 0) {
int convertedscore;
switch ([Hachidori currentService]) {
case 1:
// Raw Score to Rating Twenty
convertedscore = [ScoreConversion translateadvancedKitsuRatingtoRatingTwenty:(int)(self.lastscrobble.TitleScore/10)];
break;
}
[self updatestatus:((NSNumber *)mapping[@"kitsu_id"]).stringValue episode:@(self.lastscrobble.DetectedCurrentEpisode).stringValue score:convertedscore watchstatus:self.lastscrobble.WatchStatus notes:self.lastscrobble.TitleNotes isPrivate:self.lastscrobble.isPrivate completion:^(bool success) {
[NSNotificationCenter.defaultCenter postNotificationName:@"MultiScrobbleNotification" object:@{@"title" : @"MultiScrobble", @"message" : success ? @"Entry Update on Kitsu is successful" : @"Entry Update had failed on Kitsu", @"identifier" : @"multiscrobble-kitsu"}];
} withService:0];
}
}
}
else if ([defaults boolForKey:@"multiscrobbleanilistenabled"] && [Hachidori currentService] != 1) {
if ([Hachidori getFirstAccount:1] && self.anilistmanager.lastscrobble) {
if (mapping[@"anilist_id"] != [NSNull null] && ((NSNumber *)mapping[@"anilist_id"]).intValue > 0) {
int convertedscore;
switch ([Hachidori currentService]) {
case 0:
// Rating Twenty to Raw Score
convertedscore = [ScoreConversion ratingTwentytoAdvancedScore:self.lastscrobble.TitleScore];
break;
}
[self updatestatus:((NSNumber *)mapping[@"anilist_id"]).stringValue episode:@(self.lastscrobble.DetectedCurrentEpisode).stringValue score:convertedscore watchstatus:self.lastscrobble.WatchStatus notes:self.lastscrobble.TitleNotes isPrivate:self.lastscrobble.isPrivate completion:^(bool success) {
[NSNotificationCenter.defaultCenter postNotificationName:@"MultiScrobbleNotification" object:@{@"title" : @"MultiScrobble", @"message" : success ? @"Entry Update on AniList is successful" : @"Entry Update had failed on AniList", @"identifier" : @"multiscrobble-anilist"}];
} withService:1];
}
}
}
}
}
- (void)performMultiScrobbleRevertRewatchingWithMapping:(NSDictionary *)mapping {
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
if ([defaults boolForKey:@"multiscrobblescrobblesenabled"]) {
if ([defaults boolForKey:@"multiscrobblekitsuenabled"] && [Hachidori currentService] != 0) {
if (mapping[@"kitsu_id"] != [NSNull null] && ((NSNumber *)mapping[@"kitsu_id"]).intValue > 0 && self.kitsumanager.lastscrobble) {
if ([self.kitsumanager.lastscrobble.AniID isEqualToString:((NSNumber *)mapping[@"kitsu_id"]).stringValue] && self.kitsumanager.lastscrobble.rewatching) {
bool success = [self.kitsumanager kitsustopRewatching:self.kitsumanager.lastscrobble.AniID];
[NSNotificationCenter.defaultCenter postNotificationName:@"MultiScrobbleNotification" object:@{@"title" : @"MultiScrobble", @"message" : success ? @"Rewatch revert on Kitsu is successful" : @"Rewatch revert had failed on Kitsu", @"identifier" : @"multiscrobble-kitsu"}];
}
}
}
else if ([defaults boolForKey:@"multiscrobbleanilistenabled"] && [Hachidori currentService] != 1) {
if (mapping[@"anilist_id"] != [NSNull null] && ((NSNumber *)mapping[@"anilist_id"]).intValue > 0 && self.anilistmanager.lastscrobble) {
if ([self.anilistmanager.lastscrobble.AniID isEqualToString:((NSNumber *)mapping[@"anilist_id"]).stringValue] && self.anilistmanager.lastscrobble.rewatching) {
bool success = [self.anilistmanager aniliststopRewatching:self.anilistmanager.lastscrobble.AniID];
[NSNotificationCenter.defaultCenter postNotificationName:@"MultiScrobbleNotification" object:@{@"title" : @"MultiScrobble", @"message" : success ? @"Rewatch revert on AniList is successful" : @"Rewatch revert had failed on AniList", @"identifier" : @"multiscrobble-anilist"}];
}
}
}
}
}
- (NSDictionary *)lookupmappings:(NSString *)titleid {
NSDictionary *mapping = [self retrieveExistingMappingAsDictionary:titleid];
if (mapping) {
return mapping;
}
return [self retrievemappings:titleid];
}
- (NSDictionary *)retrievemappings:(NSString *)titleid {
NSString *site;
switch ([Hachidori currentService]) {
case 0:
site = @"kitsu";
break;
case 1:
site = @"anilist";
break;
default:
return nil;
}
NSString *hatourl;
#ifdef oss
hatourl = [NSString stringWithFormat:@"http://localhost:50420/api/mappings/%@/anime/%@", site, titleid];
#else
hatourl = [NSString stringWithFormat:@"https://hato.malupdaterosx.moe/api/mappings/%@/anime/%@", site, titleid];
#endif
NSURLSessionDataTask *task;
NSError *error;
[self.syncmanager.requestSerializer clearAuthorizationHeader];
id responseObject = [self.syncmanager syncGET:hatourl parameters:nil task:&task error:&error];
long statusCode = ((NSHTTPURLResponse *)task.response).statusCode;
switch (statusCode) {
case 200: {
if (responseObject[@"data"] && responseObject[@"data"] != [NSNull null]) {
[self saveTitleIDMappings:responseObject[@"data"] withTitleId:titleid];
return [self retrieveExistingMappingAsDictionary:titleid];
}
else {
return nil;
}
break;
}
case 404: {
NSLog(@"Title mappings for %@ not found", titleid);
return nil;
}
default: {
NSLog(@"Title mappings lookup failed: %@", error.localizedDescription);
return nil;
}
}
return nil;
}
- (void)saveTitleIDMappings:(NSDictionary *)mapping withTitleId:(NSString *)titleid {
NSManagedObject *map = [self retrieveExistingMapping:titleid];
if (!map) {
map = [NSEntityDescription insertNewObjectForEntityForName:@"Titleidmappings" inManagedObjectContext:self.managedObjectContext];
}
[map setValuesForKeysWithDictionary:mapping];
[self.managedObjectContext save:nil];
}
- (NSDictionary *)retrieveExistingMappingAsDictionary:(NSString *)titleid {
NSManagedObject *mapping = [self retrieveExistingMapping:titleid];
if (mapping) {
NSArray *keys = mapping.entity.attributesByName.allKeys;
return [mapping dictionaryWithValuesForKeys:keys];
}
return nil;
}
- (NSManagedObject *)retrieveExistingMapping:(NSString *)titleid {
__block NSArray *mappings = @[];
[self.managedObjectContext performBlockAndWait:^{
NSFetchRequest *fetchRequest = [NSFetchRequest new];
fetchRequest.entity = [NSEntityDescription entityForName:@"Titleidmappings" inManagedObjectContext:self.managedObjectContext];
NSPredicate *predicate;
switch ([Hachidori currentService]) {
case 0:
predicate = [NSPredicate predicateWithFormat:@"kitsu_id == %i", titleid.intValue];
break;
case 1:
predicate = [NSPredicate predicateWithFormat:@"anilist_id == %i", titleid.intValue];
break;
default:
break;
}
if (predicate) {
fetchRequest.predicate = predicate;
NSError *error = nil;
mappings = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
}
}];
if (mappings.count > 0) {
return mappings[0];
}
return nil;
}
- (bool)shouldMultiScrobble:(DetectedScrobbleStatus *)dstatus {
// Checks if Hachidori should proceed with doing a multiscrobble.
if (!dstatus.airing && !dstatus.completedairing) {
// User attempting to update title that haven't been aired.
return NO;
}
else if ((dstatus.DetectedEpisode).intValue == dstatus.TotalEpisodes && dstatus.airing && !dstatus.completedairing) {
// User attempting to complete a title, which haven't finished airing
return NO;
}
else if (dstatus.DetectedEpisode.intValue <= dstatus.DetectedCurrentEpisode && (![dstatus.WatchStatus isEqualToString:@"completed"] || ![[NSUserDefaults standardUserDefaults] boolForKey:@"RewatchEnabled"])) {
return NO;
}
else if (dstatus.DetectedEpisode.intValue == dstatus.DetectedCurrentEpisode && dstatus.DetectedCurrentEpisode == dstatus.TotalEpisodes && dstatus.TotalEpisodes > 1 && [dstatus.WatchStatus isEqualToString:@"completed"]) {
//Do not set rewatch status for current episode equal to total episodes.
return NO;
}
return YES;
}
@end