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

Adding metadata support for fetching custom meta tags from the website #157

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Sources/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public struct Response {
public internal(set) var icon: String?
public internal(set) var video: String?
public internal(set) var price: String?

public internal(set) var metaData: [String: String]?

public init() { }

}
39 changes: 37 additions & 2 deletions Sources/SwiftLinkPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ open class SwiftLinkPreview: NSObject {
result.icon = $0.icon
result.video = $0.video
result.price = $0.price

result.metaData = $0.metaData

self.cache.slp_setCachedResponse(url: unshortened.absoluteString, response: result)
self.cache.slp_setCachedResponse(url: url.absoluteString, response: result)

Expand Down Expand Up @@ -605,7 +606,7 @@ extension SwiftLinkPreview {
result.description = value.decoded.extendedTrim
}
}

result.metaData = self.parse(metadataFrom: htmlCode as String)
return (htmlCode, result)
}

Expand Down Expand Up @@ -806,4 +807,38 @@ extension SwiftLinkPreview: URLSessionDataDelegate {
completionHandler(request)
}

private func parse(metadataFrom htmlString: String) -> [String: String] {
// Regex to extract meta tag regex.
let metatagRegex = try! NSRegularExpression(pattern: "<meta(?:\".*?\"|\'.*?\'|[^\'\"])*?>", options: [.dotMatchesLineSeparators])
let metaTagMatches = metatagRegex.matches(in: htmlString, options: [], range: NSRange(location: 0, length: htmlString.utf16.count))
if metaTagMatches.isEmpty { return [:] }
// Regex to extract og property, content and theme colors.
let propertyRegex = try! NSRegularExpression(pattern: "\\sproperty=(?:\"|\')og:([a-zA_Z:]+?)(?:\"|\')", options: [])
let contentRegex = try! NSRegularExpression(pattern: "\\scontent=(?:\"|\')(.*?)(?:\"|\')", options: [])
let nsString = htmlString as NSString
// Create [ogProperty: content] dictionary.
return metaTagMatches.reduce([String: String]()) { attributes, result -> [String: String] in
var attributes = attributes
let property = { () -> (name: String, content: String)? in
let metaTag = nsString.substring(with: result.range(at: 0))
let range = NSRange(location: 0, length: metaTag.utf16.count)
guard let contentMatch = contentRegex.matches(in: metaTag, options: [], range: range).first else {
return nil
}
let nsMetaTag = metaTag as NSString
let content = nsMetaTag.substring(with: contentMatch.range(at: 1))
// Gets property="og:*" || property='og:*' value.
if let propertyMatch = propertyRegex.matches(in: metaTag, options: [], range: range).first {
let property = nsMetaTag.substring(with: propertyMatch.range(at: 1))
return (name: property, content: content)
}
return nil
}()
if let property = property {
attributes[property.name] = property.content
}
return attributes
}
}

}