-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtil.m
317 lines (265 loc) · 14.1 KB
/
Util.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#import "Util.h"
@implementation Util
+ (void)extractVideoInfoFromNode:(id)node
completion:(void (^)(NSString *videoId, NSString *videoTitle, NSString *ownerName))completion {
if (!completion)
return;
if (![node isKindOfClass:NSClassFromString(@"YTInlinePlaybackPlayerNode")]) {
NSLog(@"[Gonerino] Error: extractVideoInfoFromNode received incorrect node type: %@",
NSStringFromClass([node class]));
return;
}
@try {
UIView *view = [node view];
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"YTElementsInlineMutedPlaybackView")]) {
YTElementsInlineMutedPlaybackView *playbackView = (YTElementsInlineMutedPlaybackView *)subview;
YTASDPlayableEntry *playableEntry = playbackView.asdPlayableEntry;
if (playableEntry && playableEntry.hasNavigationEndpoint) {
NSString *description = [playableEntry.navigationEndpoint description];
if (!description)
return;
NSError *error = nil;
NSString *videoId = nil;
NSString *videoTitle = nil;
NSString *ownerName = nil;
NSArray *patterns = @[
@"video_id: \"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"",
@"video_title: \"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"",
@"owner_display_name: \"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\""
];
for (NSString *pattern in patterns) {
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:0
error:&error];
if (error) {
NSLog(@"[Gonerino] Regex error for pattern %@: %@", pattern, error);
continue;
}
NSTextCheckingResult *match = [regex firstMatchInString:description
options:0
range:NSMakeRange(0, description.length)];
if (match && match.numberOfRanges > 1) {
NSString *value = [description substringWithRange:[match rangeAtIndex:1]];
value = [value stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
value = [value stringByReplacingOccurrencesOfString:@"\\'" withString:@"'"];
if ([pattern hasPrefix:@"video_id:"]) {
videoId = value;
} else if ([pattern hasPrefix:@"video_title:"]) {
videoTitle = value;
} else if ([pattern hasPrefix:@"owner_display_name:"]) {
ownerName = value;
}
}
}
if (videoId || videoTitle || ownerName) {
completion(videoId, videoTitle, ownerName);
}
return;
}
}
}
} @catch (NSException *exception) {
NSLog(@"[Gonerino] Exception in extractVideoInfoFromNode: %@", exception);
}
}
+ (BOOL)nodeContainsBlockedVideo:(id)node {
if ([node respondsToSelector:@selector(accessibilityLabel)]) {
NSString *accessibilityLabel = [node accessibilityLabel];
if (accessibilityLabel) {
if ([[WordManager sharedInstance] isWordBlocked:accessibilityLabel]) {
NSLog(@"[Gonerino] Blocking video because of blocked word: %@", accessibilityLabel);
return YES;
}
}
}
if ([node isKindOfClass:NSClassFromString(@"ASTextNode")]) {
NSAttributedString *attributedText = [(ASTextNode *)node attributedText];
NSString *text = [attributedText string];
if ([[WordManager sharedInstance] isWordBlocked:text]) {
NSLog(@"[Gonerino] Blocking content with blocked word: %@", text);
return YES;
}
if ([text containsString:@" · "]) {
NSArray *components = [text componentsSeparatedByString:@" · "];
if (components.count >= 1) {
NSString *potentialChannelName = components[0];
if ([[ChannelManager sharedInstance] isChannelBlocked:potentialChannelName]) {
NSLog(@"[Gonerino] Blocking content from blocked channel: %@", potentialChannelName);
return YES;
}
}
}
}
if ([node respondsToSelector:@selector(channelName)]) {
NSString *nodeChannelName = [node channelName];
if ([[ChannelManager sharedInstance] isChannelBlocked:nodeChannelName]) {
NSLog(@"[Gonerino] Blocking content from blocked channel: %@", nodeChannelName);
return YES;
}
}
if ([node respondsToSelector:@selector(ownerName)]) {
NSString *nodeOwnerName = [node ownerName];
if ([[ChannelManager sharedInstance] isChannelBlocked:nodeOwnerName]) {
NSLog(@"[Gonerino] Blocking content from blocked channel: %@", nodeOwnerName);
return YES;
}
}
__block BOOL isBlocked = NO;
if ([node isKindOfClass:NSClassFromString(@"ASTextNode")]) {
NSAttributedString *attributedText = [(ASTextNode *)node attributedText];
NSString *text = [attributedText string];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GonerinoPeopleWatched"] &&
[text isEqualToString:@"People also watched this video"]) {
NSLog(@"[Gonerino] Blocking 'People also watched' section");
return YES;
}
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GonerinoMightLike"] &&
[text isEqualToString:@"You might also like this"]) {
NSLog(@"[Gonerino] Blocking 'You might also like' section");
return YES;
}
}
if ([node isKindOfClass:NSClassFromString(@"YTInlinePlaybackPlayerNode")]) {
[self
extractVideoInfoFromNode:node
completion:^(NSString *videoId, NSString *videoTitle, NSString *ownerName) {
if ([[VideoManager sharedInstance] isVideoBlocked:videoId]) {
isBlocked = YES;
NSLog(@"[Gonerino] Blocking video with id: %@", videoId);
}
if ([[ChannelManager sharedInstance] isChannelBlocked:ownerName]) {
isBlocked = YES;
NSLog(@"[Gonerino] Blocking video with id %@: Channel %@ is blocked", videoId,
ownerName);
}
if ([[WordManager sharedInstance] isWordBlocked:videoTitle]) {
isBlocked = YES;
NSLog(@"[Gonerino] Blocking video with id %@: title contains blocked word", videoId);
}
if ([[WordManager sharedInstance] isWordBlocked:ownerName]) {
isBlocked = YES;
NSLog(@"[Gonerino] Blocking video with id %@: channel name contains blocked word",
videoId);
}
}];
return isBlocked;
}
if ([node respondsToSelector:@selector(subnodes)]) {
NSArray *subnodes = [node subnodes];
for (id subnode in subnodes) {
if ([self nodeContainsBlockedVideo:subnode]) {
return YES;
}
}
}
return NO;
}
+ (UIImage *)createBlockChannelIconWithSize:(CGSize)size {
@try {
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
if (!context) {
NSLog(@"[Gonerino] Failed to create graphics context");
return nil;
}
CGContextSetShouldAntialias(context, YES);
CGContextSetAllowsAntialiasing(context, YES);
CGContextSetShouldSmoothFonts(context, NO);
[[UIColor whiteColor] setStroke];
CGFloat noSymbolRadius = size.width * 0.45;
CGPoint center = CGPointMake(size.width / 2, size.height / 2);
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:center
radius:noSymbolRadius
startAngle:0
endAngle:2 * M_PI
clockwise:YES];
CGFloat bodyRadius = size.width * 0.3;
CGPoint bodyCenter = CGPointMake(size.width / 2, size.height * 0.85);
UIBezierPath *bodyPath = [UIBezierPath bezierPathWithArcCenter:bodyCenter
radius:bodyRadius
startAngle:M_PI
endAngle:2 * M_PI
clockwise:YES];
CGFloat headRadius = size.width * 0.15;
CGPoint headCenter = CGPointMake(size.width / 2, size.height * 0.35);
UIBezierPath *headPath = [UIBezierPath bezierPathWithArcCenter:headCenter
radius:headRadius
startAngle:0
endAngle:2 * M_PI
clockwise:YES];
UIBezierPath *linePath = [UIBezierPath bezierPath];
CGFloat offset = noSymbolRadius * 0.7071;
[linePath moveToPoint:CGPointMake(center.x - offset, center.y - offset)];
[linePath addLineToPoint:CGPointMake(center.x + offset, center.y + offset)];
CGFloat lineWidth = 1.5;
circlePath.lineWidth = lineWidth;
headPath.lineWidth = lineWidth;
bodyPath.lineWidth = lineWidth;
linePath.lineWidth = lineWidth;
[circlePath stroke];
[bodyPath stroke];
[headPath stroke];
[linePath stroke];
UIImage *icon = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [icon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
} @catch (NSException *exception) {
NSLog(@"[Gonerino] Exception in createBlockChannelIcon: %@", exception);
return nil;
}
}
+ (UIImage *)createBlockVideoIconWithSize:(CGSize)size {
@try {
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
if (!context) {
NSLog(@"[Gonerino] Failed to create graphics context");
return nil;
}
CGContextSetShouldAntialias(context, YES);
CGContextSetAllowsAntialiasing(context, YES);
CGContextSetShouldSmoothFonts(context, NO);
[[UIColor whiteColor] setStroke];
[[UIColor whiteColor] setFill];
CGPoint center = CGPointMake(size.width / 2, size.height / 2);
UIBezierPath *rectPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(size.width * 0.2, size.height * 0.3,
size.width * 0.6, size.height * 0.4)
cornerRadius:3.0];
UIBezierPath *trianglePath = [UIBezierPath bezierPath];
CGFloat triangleSize = size.width * 0.2;
CGPoint triangleCenter = center;
[trianglePath
moveToPoint:CGPointMake(triangleCenter.x - triangleSize / 2, triangleCenter.y - triangleSize / 2)];
[trianglePath addLineToPoint:CGPointMake(triangleCenter.x + triangleSize / 2, triangleCenter.y)];
[trianglePath
addLineToPoint:CGPointMake(triangleCenter.x - triangleSize / 2, triangleCenter.y + triangleSize / 2)];
[trianglePath closePath];
CGFloat noSymbolRadius = size.width * 0.45;
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:center
radius:noSymbolRadius
startAngle:0
endAngle:2 * M_PI
clockwise:YES];
UIBezierPath *linePath = [UIBezierPath bezierPath];
CGFloat offset = noSymbolRadius * 0.7071;
[linePath moveToPoint:CGPointMake(center.x - offset, center.y - offset)];
[linePath addLineToPoint:CGPointMake(center.x + offset, center.y + offset)];
CGFloat lineWidth = 1.5;
rectPath.lineWidth = lineWidth;
trianglePath.lineWidth = lineWidth;
circlePath.lineWidth = lineWidth;
linePath.lineWidth = lineWidth;
[rectPath stroke];
[trianglePath fill];
[circlePath stroke];
[linePath stroke];
UIImage *icon = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [icon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
} @catch (NSException *exception) {
NSLog(@"[Gonerino] Exception in createBlockVideoIcon: %@", exception);
return nil;
}
}
@end