forked from qnblackcat/iSponsorBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSponsorBlockRequest.m
154 lines (151 loc) · 9.49 KB
/
SponsorBlockRequest.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
#import "SponsorBlockRequest.h"
#import <objc/runtime.h>
@implementation SponsorBlockRequest
+(void)getSponsorTimes:(NSString *)videoID completionTarget:(id)target completionSelector:(SEL)sel {
__block NSMutableArray *skipSegments = [NSMutableArray array];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *categories = @"[%22sponsor%22,%20%22intro%22,%20%22outro%22,%20%22interaction%22,%20%22selfpromo%22,%20%22music_offtopic%22]";
//NSString *categories = @"[%22sponsor%22,%20%22intro%22,%20%22outro%22,%20%22interaction%22,%20%22selfpromo%22,%20%22music_offtopic%22,%20%22preview%22,%20%22filler%22]";
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://sponsor.ajay.app/api/skipSegments?videoID=%@&categories=%@", videoID, categories]]];
request.HTTPMethod = @"GET";
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (data != nil && error == nil){
NSArray *jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSMutableArray *segments = [NSMutableArray array];
for(NSDictionary *dict in jsonData) {
SponsorSegment *segment = [[SponsorSegment alloc] initWithStartTime:[[dict objectForKey:@"segment"][0] floatValue] endTime:[[dict objectForKey:@"segment"][1] floatValue] category:(NSString *)[dict objectForKey:@"category"] UUID:(NSString *)[dict objectForKey:@"UUID"]];
[segments addObject:segment];
}
skipSegments = [segments sortedArrayUsingComparator:^NSComparisonResult(SponsorSegment *a, SponsorSegment *b){
NSNumber *first = @(a.startTime);
NSNumber *second = @(b.startTime);
return [first compare:second];
}].mutableCopy;
NSMutableArray *seekBarSegments = skipSegments.mutableCopy;
for(SponsorSegment *segment in skipSegments.copy) {
NSInteger setting = [[kCategorySettings objectForKey:segment.category] intValue];
switch (setting) {
case 0:
[skipSegments removeObject:segment];
[seekBarSegments removeObject:segment];
break;
case 2:
[skipSegments removeObject:segment];
break;
//only leaves the object in seekBarSegments so it appears in the seek bar but doesn't get skipped
default:
break;
}
if(segment.endTime - segment.startTime < kMinimumDuration) {
[skipSegments removeObject:segment];
[seekBarSegments removeObject:segment];
}
}
[target performSelectorOnMainThread:sel withObject:skipSegments waitUntilDone:NO];
if([target isKindOfClass:objc_getClass("YTPlayerViewController")]){
YTPlayerViewController *playerViewController = (YTPlayerViewController *)target;
id overlayView = playerViewController.view.overlayView;
if([overlayView isKindOfClass:objc_getClass("YTMainAppVideoPlayerOverlayView")]){
if(playerViewController.view.overlayView.playerBar.playerBar) {
[playerViewController.view.overlayView.playerBar.playerBar performSelectorOnMainThread:@selector(setSkipSegments:) withObject:seekBarSegments waitUntilDone:NO];
}
else {
[playerViewController.view.overlayView.playerBar.segmentablePlayerBar performSelectorOnMainThread:@selector(setSkipSegments:) withObject:seekBarSegments waitUntilDone:NO];
}
}
}
}
}];
[dataTask resume];
}
+(void)postSponsorTimes:(NSString *)videoID sponsorSegments:(NSArray <SponsorSegment *> *)segments userID:(NSString *)userID withViewController:(UIViewController *)viewController {
for(SponsorSegment *segment in segments){
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://sponsor.ajay.app/api/skipSegments?videoID=%@&startTime=%f&endTime=%f&category=%@&userID=%@", videoID, segment.startTime, segment.endTime, segment.category, userID]]];
request.HTTPMethod = @"POST";
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *URLResponse = (NSHTTPURLResponse *)response;
if(URLResponse.statusCode != 200) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error" message:[NSString stringWithFormat:@"Error Code: %ld %@", URLResponse.statusCode, [NSHTTPURLResponse localizedStringForStatusCode:URLResponse.statusCode]] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[viewController presentViewController:alert animated:YES completion:nil];
});
return;
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Success" message:@"Successfully Submitted Segments" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[viewController presentViewController:alert animated:YES completion:nil];
});
}
}];
[dataTask resume];
}
}
+(void)normalVoteForSegment:(SponsorSegment *)segment userID:(NSString *)userID type:(BOOL)type withViewController:(UIViewController *)viewController {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://sponsor.ajay.app/api/voteOnSponsorTime?UUID=%@&userID=%@&type=%d", segment.UUID, userID, type]]];
request.HTTPMethod = @"POST";
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *URLResponse = (NSHTTPURLResponse *)response;
NSString *title;
CGFloat delay;
if(URLResponse.statusCode != 200) {
title = [NSString stringWithFormat:@"Error voting: (%ld %@)", URLResponse.statusCode, [NSHTTPURLResponse localizedStringForStatusCode:URLResponse.statusCode]];
delay = 3.0f;
}
else {
title = @"Successfully Voted";
delay = 1.0f;
}
dispatch_async(dispatch_get_main_queue(), ^{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:viewController.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.label.text = title;
hud.offset = CGPointMake(0.f, 50);
[hud hideAnimated:YES afterDelay:delay];
});
}];
[dataTask resume];
}
+(void)categoryVoteForSegment:(SponsorSegment *)segment userID:(NSString *)userID category:(NSString *)category withViewController:(UIViewController *)viewController {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://sponsor.ajay.app/api/voteOnSponsorTime?UUID=%@&userID=%@&category=%@", segment.UUID, userID, category]]];
request.HTTPMethod = @"POST";
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *URLResponse = (NSHTTPURLResponse *)response;
NSString *title;
CGFloat delay;
if(URLResponse.statusCode != 200) {
title = [NSString stringWithFormat:@"Error voting: (%ld %@)", URLResponse.statusCode, [NSHTTPURLResponse localizedStringForStatusCode:URLResponse.statusCode]];
delay = 3.0f;
}
else {
title = @"Successfully Voted";
delay = 1.0f;
}
dispatch_async(dispatch_get_main_queue(), ^{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:viewController.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.label.text = title;
hud.offset = CGPointMake(0.f, 50);
[hud hideAnimated:YES afterDelay:delay];
});
}];
[dataTask resume];
}
+(void)viewedVideoSponsorTime:(SponsorSegment *)segment {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://sponsor.ajay.app/api/viewedVideoSponsorTime?UUID=%@",segment.UUID]]];
request.HTTPMethod = @"POST";
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];
[dataTask resume];
}
@end