@@ -91,42 +91,80 @@ public final class OGDataProvider: NSObject {
91
91
}
92
92
}
93
93
ogData. sourceUrl = urlString
94
- guard let URL = URL ( string: urlString) else {
94
+ guard let url = URL ( string: urlString) else {
95
95
completion ? ( ogData, NSError ( domain: " can not create NSURL with \" \( urlString) \" " , code: 9999 , userInfo: nil ) )
96
96
return nil
97
97
}
98
- var request = URLRequest ( url: URL)
99
- request. setValue ( Const . userAgent, forHTTPHeaderField: " User-Agent " )
100
- request. timeoutInterval = 5
101
- let uuidString = UUID ( ) . uuidString
102
-
103
- let task = session. dataTask ( with: request, completionHandler: { [ weak self] data, response, error in
104
- let completion = self ? . taskContainers [ uuidString] ? . completion
105
- _ = self ? . taskContainers. removeValue ( forKey: uuidString)
106
-
107
- if let error = error {
108
- completion ? ( ogData, error)
109
- return
98
+
99
+ let task : URLSessionDataTask
100
+ let uuidString : String
101
+ if url. host? . contains ( " www.youtube.com " ) == true {
102
+ guard
103
+ let escapedString = urlString. addingPercentEncoding ( withAllowedCharacters: . urlQueryAllowed) ,
104
+ let url = URL ( string: " https://www.youtube.com/oembed?url= \( escapedString) " )
105
+ else {
106
+ completion ? ( ogData, NSError ( domain: " can not create NSURL with \" \( urlString) \" " , code: 9999 , userInfo: nil ) )
107
+ return nil
110
108
}
111
- guard let data = data,
112
- let html = Kanna . HTML ( html: data, encoding: String . Encoding. utf8) ,
113
- let header = html. head else {
109
+ var request = URLRequest ( url: url)
110
+ request. timeoutInterval = 5
111
+ uuidString = UUID ( ) . uuidString
112
+
113
+ task = session. dataTask ( with: request, completionHandler: { [ weak self] data, response, error in
114
+ let completion = self ? . taskContainers [ uuidString] ? . completion
115
+ _ = self ? . taskContainers. removeValue ( forKey: uuidString)
116
+
117
+ if let error = error {
118
+ completion ? ( ogData, error)
119
+ return
120
+ }
121
+ guard
122
+ let data = data,
123
+ let rawJson = try ? JSONSerialization . jsonObject ( with: data, options: [ ] ) ,
124
+ let json = rawJson as? [ AnyHashable : Any ]
125
+ else {
126
+ completion ? ( ogData, nil )
127
+ return
128
+ }
129
+ ogData. setValue ( withYoutubeJson: json)
130
+ ogData. save ( )
131
+
114
132
completion ? ( ogData, nil )
115
- return
116
- }
117
- let metaTags = header. xpath ( Const . metaTagKey)
118
- for metaTag in metaTags {
119
- guard let property = metaTag [ Const . propertyKey] ,
120
- let content = metaTag [ Const . contentKey]
121
- , property. hasPrefix ( Const . propertyPrefix) else {
122
- continue
133
+ } )
134
+ } else {
135
+ var request = URLRequest ( url: url)
136
+ request. setValue ( Const . userAgent, forHTTPHeaderField: " User-Agent " )
137
+ request. timeoutInterval = 5
138
+ uuidString = UUID ( ) . uuidString
139
+
140
+ task = session. dataTask ( with: request, completionHandler: { [ weak self] data, response, error in
141
+ let completion = self ? . taskContainers [ uuidString] ? . completion
142
+ _ = self ? . taskContainers. removeValue ( forKey: uuidString)
143
+
144
+ if let error = error {
145
+ completion ? ( ogData, error)
146
+ return
123
147
}
124
- ogData. setValue ( property: property, content: content)
125
- }
126
- ogData. save ( )
127
-
128
- completion ? ( ogData, nil )
129
- } )
148
+ guard let data = data,
149
+ let html = Kanna . HTML ( html: data, encoding: String . Encoding. utf8) ,
150
+ let header = html. head else {
151
+ completion ? ( ogData, nil )
152
+ return
153
+ }
154
+ let metaTags = header. xpath ( Const . metaTagKey)
155
+ for metaTag in metaTags {
156
+ guard let property = metaTag [ Const . propertyKey] ,
157
+ let content = metaTag [ Const . contentKey]
158
+ , property. hasPrefix ( Const . propertyPrefix) else {
159
+ continue
160
+ }
161
+ ogData. setValue ( property: property, content: content)
162
+ }
163
+ ogData. save ( )
164
+
165
+ completion ? ( ogData, nil )
166
+ } )
167
+ }
130
168
taskContainers [ uuidString] = TaskContainer ( uuidString: uuidString, task: task, completion: completion)
131
169
task. resume ( )
132
170
return uuidString
0 commit comments