-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPBMetaTagParser.m
More file actions
143 lines (115 loc) · 4.27 KB
/
PBMetaTagParser.m
File metadata and controls
143 lines (115 loc) · 4.27 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
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
//
// PBMetaTagParser.m
// Pastie
//
// Created by Tanner Bennett on 3/10/24.
//
#import <UIKit/UIKit.h>
#import "PBMetaTagParser.h"
#import "NSArray+Map.h"
@interface PBMetaTagParser ()
/// The raw underlying tags, used to later populate `ogTags`
@property (nonatomic) NSMutableArray<NSDictionary *> *metaTags;
@end
@implementation PBMetaTagParser
- (instancetype)init {
self = [super init];
if (self) {
_metaTags = [NSMutableArray new];
}
return self;
}
- (id)description {
return [NSString
stringWithFormat:@"<PBMetaTagParser: %p\n\tsite: %@,\n\ttitle: %@,\n\turl: %@,\n\timage: %@,\n\tdesc: %@>",
self, self.site, self.title, self.url, self.image, self.desc
];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary<NSString *,NSString *> *)attributes {
if ([elementName isEqualToString:@"meta"] && [attributes[@"property"] hasPrefix:@"og:"]) {
[self.metaTags addObject:attributes];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
// Not sure why this would be needed twice...
NSArray *ogTags = [self.metaTags pastie_filtered:^BOOL(NSDictionary *attrs, NSUInteger idx) {
return [attrs[@"property"] hasPrefix:@"og:"];
}];
// Pull the content out of the tag
NSMutableDictionary *ogDict = [NSMutableDictionary new];
for (NSDictionary *tag in ogTags) {
ogDict[tag[@"property"]] = [self unescapedTagContent:tag[@"content"]];
}
_ogTags = ogDict;
[self populateTagProperties];
}
- (NSString *)unescapedTagContent:(NSString *)content {
NSParameterAssert(content);
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
};
NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData:data options:options documentAttributes:nil error:nil
];
NSString *decodedString = attributedString.string;
return decodedString;
}
- (void)populateTagProperties {
_site = self.ogTags[@"og:site_name"];
_title = self.ogTags[@"og:title"];
_url = self.ogTags[@"og:url"];
_image = self.ogTags[@"og:image"];
_desc = self.ogTags[@"og:description"];
// _url = [self cleanedURL:_url];
}
- (NSString *)cleanedURL:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
// Strip query parameters from these domains
NSArray<NSString *> *stripDomains = @[
@"tiktok.com"
];
for (NSString *domain in stripDomains) {
if ([url.host hasSuffix:domain]) {
// Use URLComponents to strip the query
NSURLComponents *components = [NSURLComponents
componentsWithURL:url resolvingAgainstBaseURL:NO
];
components.query = nil;
return components.URL.absoluteString;
}
}
return urlString;
}
+ (void)fetchMetaTagsForURL:(NSURL *)url completion:(void(^)(PBMetaTagParser *parser, NSError *error))callback {
NSParameterAssert(url); NSParameterAssert(callback);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.allHTTPHeaderFields = @{
@"User-Agent": @"facebookexternalhit/1.1"
};
NSURLSessionDataTask *task = [NSURLSession.sharedSession
dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(nil, error);
});
} else {
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
PBMetaTagParser *parser = [PBMetaTagParser new];
xmlParser.delegate = parser;
if (![xmlParser parse]) {
[parser parserDidEndDocument:xmlParser];
}
dispatch_async(dispatch_get_main_queue(), ^{
callback(parser, nil);
});
}
}
];
[task resume];
}
@end