Skip to content

Commit

Permalink
Merge pull request #42 from marty-suzuki/fix-youtube
Browse files Browse the repository at this point in the history
fix youtube link embeded #39
  • Loading branch information
marty-suzuki authored Oct 6, 2017
2 parents dd3b2fc + 88a595a commit dfaa6bb
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 34 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
- [x] Tap handleable
- [x] Clearable image cache
- [x] Clearable data cache
- [x] Support Swift2.3
- [x] Support Swift3
- [x] Support Swift3.2
- [x] Support Carthage since 0.10.1

## Usage
Expand Down Expand Up @@ -122,6 +121,8 @@ NoticeObserveKit to your `Cartfile`:
github "marty-suzuki/URLEmbeddedView"
```

In the project settings add $(SDKROOT)/usr/include/libxml2 to the "header search paths" field

## Use in Objective-C

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

## Requirements

- Xcode 8.0 or greater
- Xcode 9 or greater
- iOS 8.0 or greater
- [Kanna(鉋)](https://github.com/tid-kijyun/Kanna)
- UIKit
Expand Down
2 changes: 1 addition & 1 deletion URLEmbeddedView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "URLEmbeddedView"
s.version = "0.10.1"
s.version = "0.10.2"
s.summary = "URLEmbeddedView is a view that automatically cache the Open Graph Protocol."

# This description is used to generate tags and improve search results.
Expand Down
15 changes: 15 additions & 0 deletions URLEmbeddedView/OGData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ public final class OGData: NSManagedObject {
case .description : pageDescription = content.replacingOccurrences(of: "\n", with: " ")
}
}

func setValue(withYoutubeJson json: [AnyHashable : Any]) {
if let title = json["title"] as? String {
self.pageTitle = title
}
if let type = json["type"] as? String {
self.pageType = type
}
if let providerName = json["provider_name"] as? String {
self.siteName = providerName
}
if let image = json["thumbnail_url"] as? String {
self.imageUrl = image
}
}

func save() {
updateDate = Date()
Expand Down
98 changes: 68 additions & 30 deletions URLEmbeddedView/OGDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,42 +91,80 @@ public final class OGDataProvider: NSObject {
}
}
ogData.sourceUrl = urlString
guard let URL = URL(string: urlString) else {
guard let url = URL(string: urlString) else {
completion?(ogData, NSError(domain: "can not create NSURL with \"\(urlString)\"", code: 9999, userInfo: nil))
return nil
}
var request = URLRequest(url: URL)
request.setValue(Const.userAgent, forHTTPHeaderField: "User-Agent")
request.timeoutInterval = 5
let uuidString = UUID().uuidString

let task = session.dataTask(with: request, completionHandler: { [weak self] data, response, error in
let completion = self?.taskContainers[uuidString]?.completion
_ = self?.taskContainers.removeValue(forKey: uuidString)

if let error = error {
completion?(ogData, error)
return

let task: URLSessionDataTask
let uuidString: String
if url.host?.contains("www.youtube.com") == true {
guard
let escapedString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let url = URL(string: "https://www.youtube.com/oembed?url=\(escapedString)")
else {
completion?(ogData, NSError(domain: "can not create NSURL with \"\(urlString)\"", code: 9999, userInfo: nil))
return nil
}
guard let data = data,
let html = Kanna.HTML(html: data, encoding: String.Encoding.utf8),
let header = html.head else {
var request = URLRequest(url: url)
request.timeoutInterval = 5
uuidString = UUID().uuidString

task = session.dataTask(with: request, completionHandler: { [weak self] data, response, error in
let completion = self?.taskContainers[uuidString]?.completion
_ = self?.taskContainers.removeValue(forKey: uuidString)

if let error = error {
completion?(ogData, error)
return
}
guard
let data = data,
let rawJson = try? JSONSerialization.jsonObject(with: data, options: []),
let json = rawJson as? [AnyHashable : Any]
else {
completion?(ogData, nil)
return
}
ogData.setValue(withYoutubeJson: json)
ogData.save()

completion?(ogData, nil)
return
}
let metaTags = header.xpath(Const.metaTagKey)
for metaTag in metaTags {
guard let property = metaTag[Const.propertyKey],
let content = metaTag[Const.contentKey]
, property.hasPrefix(Const.propertyPrefix) else {
continue
})
} else {
var request = URLRequest(url: url)
request.setValue(Const.userAgent, forHTTPHeaderField: "User-Agent")
request.timeoutInterval = 5
uuidString = UUID().uuidString

task = session.dataTask(with: request, completionHandler: { [weak self] data, response, error in
let completion = self?.taskContainers[uuidString]?.completion
_ = self?.taskContainers.removeValue(forKey: uuidString)

if let error = error {
completion?(ogData, error)
return
}
ogData.setValue(property: property, content: content)
}
ogData.save()

completion?(ogData, nil)
})
guard let data = data,
let html = Kanna.HTML(html: data, encoding: String.Encoding.utf8),
let header = html.head else {
completion?(ogData, nil)
return
}
let metaTags = header.xpath(Const.metaTagKey)
for metaTag in metaTags {
guard let property = metaTag[Const.propertyKey],
let content = metaTag[Const.contentKey]
, property.hasPrefix(Const.propertyPrefix) else {
continue
}
ogData.setValue(property: property, content: content)
}
ogData.save()

completion?(ogData, nil)
})
}
taskContainers[uuidString] = TaskContainer(uuidString: uuidString, task: task, completion: completion)
task.resume()
return uuidString
Expand Down

0 comments on commit dfaa6bb

Please sign in to comment.