Skip to content
This repository was archived by the owner on Jan 18, 2025. It is now read-only.

Commit a997865

Browse files
Merge branch '131' into release-candidate/3.3.0
2 parents 45d9531 + f1b1b04 commit a997865

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
---
2222

23+
#### Changed
24+
- fixed youtube and open graph tags image metadata issues
25+
- Changed by [nafis042](https://github.com/nafis042)
26+
2327
## [3.2.0](https://github.com/LeonardoCardoso/Swift-Link-Preview/releases/tag/3.2.0)
2428

2529
#### Changed

Sources/Regex.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class Regex {
1212

1313
static let imagePattern = "(.+?)\\.(gif|jpg|jpeg|png|bmp)$"
1414
static let videoTagPattern = "<video[^>]+src=\"([^\"]+)"
15+
static let secondaryVideoTagPattern = "og:video\" content=\"([^\"](.+?))\"(.+?)[/]?>"
1516
static let imageTagPattern = "<img(.+?)src=\"([^\"](.+?))\"(.+?)[/]?>"
17+
static let secondaryImageTagPattern = "og:image\" content=\"([^\"](.+?))\"(.+?)[/]?>"
1618
static let titlePattern = "<title(.*?)>(.*?)</title>"
1719
static let metatagPattern = "<meta(.*?)>"
1820
static let metatagContentPattern = "content=(\"(.*?)\")|('(.*?)')"

Sources/SwiftLinkPreview.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ extension SwiftLinkPreview {
380380
}
381381
var request = URLRequest( url: sourceUrl )
382382
request.addValue("text/html,application/xhtml+xml,application/xml", forHTTPHeaderField: "Accept")
383+
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")
383384
let (data, urlResponse, error) = session.synchronousDataTask(with: request )
384385
if let error = error {
385386
if !cancellable.isCancelled {
@@ -627,9 +628,23 @@ extension SwiftLinkPreview {
627628
result.images = imgs
628629
result.image = imgs.first
629630
}
631+
else{
632+
let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 1)
633+
if !values.isEmpty {
634+
result.images = values
635+
result.image = values.first
636+
}
637+
}
630638
}
631639
} else {
632-
result.images = [self.addImagePrefixIfNeeded(mainImage ?? String(), result: result)]
640+
let values = Regex.pregMatchAll(htmlCode, regex: Regex.secondaryImageTagPattern, index: 1)
641+
if !values.isEmpty {
642+
result.images = values
643+
result.image = values.first
644+
}
645+
else{
646+
result.images = [self.addImagePrefixIfNeeded(mainImage ?? String(), result: result)]
647+
}
633648
}
634649
return result
635650
}

0 commit comments

Comments
 (0)