Use video preview if available

This commit is contained in:
Maurice Parker 2020-05-05 21:08:51 -05:00
parent 00cb83559c
commit 6c698c5b94
2 changed files with 34 additions and 5 deletions

View File

@ -73,10 +73,11 @@ struct RedditLinkData: Codable {
return mediaEmbedContent return mediaEmbedContent
} }
if isVideo ?? false { if url.hasSuffix(".gif") {
guard let fallbackURL = media?.video?.fallbackURL else { return "<figure><img src=\"\(url)\"></figure>"
return nil }
}
if isVideo ?? false, let videoURL = media?.video?.fallbackURL {
var html = "<video " var html = "<video "
if let previewImageURL = preview?.images?.first?.source?.url { if let previewImageURL = preview?.images?.first?.source?.url {
html += "poster=\"\(previewImageURL)\" " html += "poster=\"\(previewImageURL)\" "
@ -84,7 +85,19 @@ struct RedditLinkData: Codable {
if let width = media?.video?.width, let height = media?.video?.height { if let width = media?.video?.width, let height = media?.video?.height {
html += "width=\"\(width)\" height=\"\(height)\" " html += "width=\"\(width)\" height=\"\(height)\" "
} }
html += "src=\"\(fallbackURL)\"></video>" html += "src=\"\(videoURL)\"></video>"
return html
}
if let videoPreviewURL = preview?.videoPreview?.url {
var html = "<video "
if let previewImageURL = preview?.images?.first?.source?.url {
html += "poster=\"\(previewImageURL)\" "
}
if let width = preview?.videoPreview?.width, let height = preview?.videoPreview?.height {
html += "width=\"\(width)\" height=\"\(height)\" "
}
html += "src=\"\(videoPreviewURL)\"></video>"
return html return html
} }

View File

@ -11,9 +11,11 @@ import Foundation
struct RedditPreview: Codable { struct RedditPreview: Codable {
let images: [RedditPreviewImage]? let images: [RedditPreviewImage]?
let videoPreview: RedditVideoPreview?
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case images = "images" case images = "images"
case videoPreview = "reddit_video_preview"
} }
} }
@ -41,3 +43,17 @@ struct RedditPreviewImageSource: Codable {
} }
} }
struct RedditVideoPreview: Codable {
let url: String?
let width: Int?
let height: Int?
enum CodingKeys: String, CodingKey {
case url = "fallback_url"
case width = "width"
case height = "height"
}
}