Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for youtube url fetch metadata issue #131

Merged
merged 3 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

---

#### Changed
- fixed youtube and open graph tags image metadata issues
- Changed by [nafis042](https://github.com/nafis042)

## [3.2.0](https://github.com/LeonardoCardoso/Swift-Link-Preview/releases/tag/3.2.0)

#### Changed
Expand Down
2 changes: 2 additions & 0 deletions Sources/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class Regex {

static let imagePattern = "(.+?)\\.(gif|jpg|jpeg|png|bmp)$"
static let videoTagPattern = "<video[^>]+src=\"([^\"]+)"
static let secondaryVideoTagPattern = "og:video\" content=\"([^\"](.+?))\"(.+?)[/]?>"
static let imageTagPattern = "<img(.+?)src=\"([^\"](.+?))\"(.+?)[/]?>"
static let secondaryImageTagPattern = "og:image\" content=\"([^\"](.+?))\"(.+?)[/]?>"
static let titlePattern = "<title(.*?)>(.*?)</title>"
static let metatagPattern = "<meta(.*?)>"
static let metatagContentPattern = "content=(\"(.*?)\")|('(.*?)')"
Expand Down
17 changes: 16 additions & 1 deletion Sources/SwiftLinkPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ extension SwiftLinkPreview {
}
var request = URLRequest( url: sourceUrl )
request.addValue("text/html,application/xhtml+xml,application/xml", forHTTPHeaderField: "Accept")
request.addValue("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", forHTTPHeaderField: "user-Agent")
let (data, urlResponse, error) = session.synchronousDataTask(with: request )
if let error = error {
if !cancellable.isCancelled {
Expand Down Expand Up @@ -579,9 +580,23 @@ extension SwiftLinkPreview {
result.images = imgs
result.image = imgs.first
}
else{
let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 1)
if !values.isEmpty {
result.images = values
result.image = values.first
}
}
}
} else {
result.images = [self.addImagePrefixIfNeeded(mainImage ?? String(), result: result)]
let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 1)
if !values.isEmpty {
result.images = values
result.image = values.first
}
else{
result.images = [self.addImagePrefixIfNeeded(mainImage ?? String(), result: result)]
}
}
return result
}
Expand Down