Skip to content

Commit 88a595a

Browse files
committedOct 6, 2017
fix youtube link embeded #39
1 parent dd3b2fc commit 88a595a

File tree

4 files changed

+88
-34
lines changed

4 files changed

+88
-34
lines changed
 

‎README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
- [x] Tap handleable
1919
- [x] Clearable image cache
2020
- [x] Clearable data cache
21-
- [x] Support Swift2.3
22-
- [x] Support Swift3
21+
- [x] Support Swift3.2
2322
- [x] Support Carthage since 0.10.1
2423

2524
## Usage
@@ -122,6 +121,8 @@ NoticeObserveKit to your `Cartfile`:
122121
github "marty-suzuki/URLEmbeddedView"
123122
```
124123

124+
In the project settings add $(SDKROOT)/usr/include/libxml2 to the "header search paths" field
125+
125126
## Use in Objective-C
126127

127128
```objective-c
@@ -148,7 +149,7 @@ github "marty-suzuki/URLEmbeddedView"
148149

149150
## Requirements
150151

151-
- Xcode 8.0 or greater
152+
- Xcode 9 or greater
152153
- iOS 8.0 or greater
153154
- [Kanna(鉋)](https://github.com/tid-kijyun/Kanna)
154155
- UIKit

‎URLEmbeddedView.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = "URLEmbeddedView"
11-
s.version = "0.10.1"
11+
s.version = "0.10.2"
1212
s.summary = "URLEmbeddedView is a view that automatically cache the Open Graph Protocol."
1313

1414
# This description is used to generate tags and improve search results.

‎URLEmbeddedView/OGData.swift

+15
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ public final class OGData: NSManagedObject {
5656
case .description : pageDescription = content.replacingOccurrences(of: "\n", with: " ")
5757
}
5858
}
59+
60+
func setValue(withYoutubeJson json: [AnyHashable : Any]) {
61+
if let title = json["title"] as? String {
62+
self.pageTitle = title
63+
}
64+
if let type = json["type"] as? String {
65+
self.pageType = type
66+
}
67+
if let providerName = json["provider_name"] as? String {
68+
self.siteName = providerName
69+
}
70+
if let image = json["thumbnail_url"] as? String {
71+
self.imageUrl = image
72+
}
73+
}
5974

6075
func save() {
6176
updateDate = Date()

‎URLEmbeddedView/OGDataProvider.swift

+68-30
Original file line numberDiff line numberDiff line change
@@ -91,42 +91,80 @@ public final class OGDataProvider: NSObject {
9191
}
9292
}
9393
ogData.sourceUrl = urlString
94-
guard let URL = URL(string: urlString) else {
94+
guard let url = URL(string: urlString) else {
9595
completion?(ogData, NSError(domain: "can not create NSURL with \"\(urlString)\"", code: 9999, userInfo: nil))
9696
return nil
9797
}
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
110108
}
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+
114132
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
123147
}
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+
}
130168
taskContainers[uuidString] = TaskContainer(uuidString: uuidString, task: task, completion: completion)
131169
task.resume()
132170
return uuidString

0 commit comments

Comments
 (0)
Please sign in to comment.