-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathNSData+TwitchAdBlock.m
More file actions
54 lines (53 loc) · 2.43 KB
/
NSData+TwitchAdBlock.m
File metadata and controls
54 lines (53 loc) · 2.43 KB
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
#import "NSData+TwitchAdBlock.h"
@implementation NSData (TwitchAdBlock)
- (NSData *)twab_requestDataForRequest:(NSURLRequest *)request {
if (!request) return self;
NSData *modifiedData;
if ([request.URL.host isEqualToString:@"gql.twitch.tv"] &&
[request.URL.path isEqualToString:@"/gql"]) {
NSError *error;
id json = [NSJSONSerialization JSONObjectWithData:self
options:NSJSONReadingMutableContainers
error:&error];
if (!json || error) return self;
if ([json isKindOfClass:NSMutableDictionary.class]) {
NSMutableDictionary *jsonDictionary = json;
NSString *platform = [NSUUID UUID].UUIDString;
if ([jsonDictionary[@"operationName"] isEqualToString:@"StreamAccessToken"] ||
[jsonDictionary[@"query"] containsString:@"StreamAccessToken"] ||
[jsonDictionary[@"operationName"] isEqualToString:@"VodAccessToken"])
jsonDictionary[@"variables"][@"params"][@"platform"] = platform;
else if ([jsonDictionary[@"operationName"] isEqualToString:@"ClipAccessToken"])
jsonDictionary[@"variables"][@"tokenParams"][@"platform"] = platform;
}
modifiedData = [NSJSONSerialization dataWithJSONObject:json options:0 error:&error];
if (error) return self;
}
return modifiedData ?: self;
}
- (NSData *)twab_responseDataForRequest:(NSURLRequest *)request {
if (!request) return self;
NSData *modifiedData;
if ([request.URL.host isEqualToString:@"gql.twitch.tv"] &&
[request.URL.path isEqualToString:@"/gql"]) {
NSError *error;
id json = [NSJSONSerialization JSONObjectWithData:self
options:NSJSONReadingMutableContainers
error:&error];
if (!json || error) return self;
if ([json isKindOfClass:NSMutableArray.class]) {
NSMutableArray *jsonArray = json;
for (NSMutableDictionary *operation in jsonArray) {
NSMutableDictionary *feedItems = operation[@"data"][@"feedItems"];
if (feedItems)
feedItems[@"edges"] = [feedItems[@"edges"]
filteredArrayUsingPredicate:[NSPredicate
predicateWithFormat:@"node.__typename != 'FeedAd'"]];
}
}
modifiedData = [NSJSONSerialization dataWithJSONObject:json options:0 error:&error];
if (error) return self;
}
return modifiedData ?: self;
}
@end