-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1505 from planetary-social/open-graph/swiftsoup
Use SwiftSoup to parse Open Graph metadata
- Loading branch information
Showing
13 changed files
with
362 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Foundation | ||
|
||
/// Parses the Open Graph metadata from an HTML document. | ||
protocol OpenGraphParser { | ||
/// Fetches the Open Graph video metadata from the given HTML document. | ||
/// - Parameter html: An HTML document. | ||
/// - Returns: The Open Graph video metadata from the HTML. | ||
func videoMetadata(html: Data) -> OpenGraphMedia? | ||
} | ||
|
||
/// An Open Graph property in the HTML. | ||
enum OpenGraphProperty: String { | ||
case videoHeight = "og:video:height" | ||
case videoWidth = "og:video:width" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Foundation | ||
import SwiftSoup | ||
|
||
/// Parses the Open Graph metadata from an HTML document using SwiftSoup. | ||
struct SoupOpenGraphParser: OpenGraphParser { | ||
func videoMetadata(html: Data) -> OpenGraphMedia? { | ||
let htmlString = String(decoding: html, as: UTF8.self) | ||
guard let document = try? SwiftSoup.parse(htmlString) else { return nil } | ||
|
||
guard let widthString = openGraphProperty(.videoWidth, from: document), | ||
let width = Double(widthString) else { | ||
return nil | ||
} | ||
guard let heightString = openGraphProperty(.videoHeight, from: document), | ||
let height = Double(heightString) else { | ||
return nil | ||
} | ||
|
||
return OpenGraphMedia(type: .video, width: width, height: height) | ||
} | ||
} | ||
|
||
extension SoupOpenGraphParser { | ||
/// Gets the Open Graph property value from the given HTML document. | ||
/// - Parameters: | ||
/// - property: The Open Graph property to fetch from the HTML document. | ||
/// - document: The HTML document. | ||
/// - Returns: The value of the Open Graph property, or `nil` if none is found. | ||
private func openGraphProperty(_ property: OpenGraphProperty, from document: Document) -> String? { | ||
try? document.select("meta[property=\(property.rawValue)]").attr("content") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Foundation | ||
|
||
/// A service that fetches metadata for a URL. | ||
protocol OpenGraphService { | ||
/// Fetches metadata for the given URL. | ||
/// - Parameter url: The URL to fetch. | ||
/// - Returns: The Open Graph metadata for the URL. | ||
func fetchMetadata(for url: URL) async throws -> OpenGraphMetadata | ||
} | ||
|
||
/// A default implementation for `OpenGraphService`. | ||
struct DefaultOpenGraphService: OpenGraphService { | ||
let session: URLSessionProtocol | ||
let parser: OpenGraphParser | ||
|
||
func fetchMetadata(for url: URL) async throws -> OpenGraphMetadata { | ||
let request = URLRequest(url: url) | ||
let (data, _) = try await session.data(for: request) | ||
let videoMetadata = parser.videoMetadata(html: data) | ||
return OpenGraphMetadata(media: videoMetadata) | ||
} | ||
} | ||
|
||
/// Open Graph metadata for a URL. | ||
struct OpenGraphMetadata: Equatable { | ||
let media: OpenGraphMedia? | ||
} | ||
|
||
/// Open Graph metadata for media, such as an image or video. | ||
struct OpenGraphMedia: Equatable { | ||
let type: OpenGraphMediaType? | ||
let width: Double? | ||
let height: Double? | ||
} | ||
|
||
/// The type of Open Graph media. | ||
enum OpenGraphMediaType { | ||
case image | ||
case video | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Foundation | ||
|
||
/// A mock Open Graph parser that can be used for testing. | ||
struct MockOpenGraphParser: OpenGraphParser { | ||
func videoMetadata(html: Data) -> OpenGraphMedia? { | ||
nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Foundation | ||
import XCTest | ||
|
||
class SoupOpenGraphParserTests: XCTestCase { | ||
let sampleHTML = """ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta property="og:video:width" content="2560"> | ||
<meta property="og:video:height" content="1440"> | ||
</head> | ||
<body> | ||
<h1>Sample HTML</h1> | ||
</body> | ||
</html> | ||
""" | ||
|
||
// swiftlint:disable:next implicitly_unwrapped_optional | ||
var youTubeHTML: Data! | ||
|
||
override func setUpWithError() throws { | ||
youTubeHTML = try htmlData(filename: "youtube_video_toxic") | ||
} | ||
|
||
func test_parse() throws { | ||
// Arrange | ||
let parser = SoupOpenGraphParser() | ||
let data = try XCTUnwrap(sampleHTML.data(using: .utf8)) | ||
|
||
// Act | ||
let videoMetadata = try XCTUnwrap(parser.videoMetadata(html: data)) | ||
|
||
// Assert | ||
XCTAssertEqual(videoMetadata.width, 2560) | ||
XCTAssertEqual(videoMetadata.height, 1440) | ||
} | ||
|
||
func test_parse_youTube() throws { | ||
// Arrange | ||
let parser = SoupOpenGraphParser() | ||
let data = try XCTUnwrap(youTubeHTML) | ||
|
||
// Act | ||
let videoMetadata = try XCTUnwrap(parser.videoMetadata(html: data)) | ||
|
||
// Assert | ||
XCTAssertEqual(videoMetadata.width, 1280) | ||
XCTAssertEqual(videoMetadata.height, 720) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import XCTest | ||
|
||
class DefaultOpenGraphServiceTests: XCTestCase { | ||
func test_fetchMetadata() async throws { | ||
// Arrange | ||
let url = try XCTUnwrap(URL(string: "https://youtu.be/5qvdbyRH9wA?si=y_KTgLR22nH0-cs8")) | ||
let subject = DefaultOpenGraphService(session: MockURLSession(), parser: MockOpenGraphParser()) | ||
let expected = OpenGraphMetadata(media: nil) | ||
|
||
// Act | ||
let metadata = try await subject.fetchMetadata(for: url) | ||
|
||
// Assert | ||
XCTAssertEqual(metadata, expected) | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters