NetNewsWire/Evergreen/Images/RSHTMLMetadata+Extension.swift
2017-11-26 20:40:07 -08:00

90 lines
1.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// RSHTMLMetadata+Extension.swift
// Evergreen
//
// Created by Brent Simmons on 11/26/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import Foundation
import RSParser
extension RSHTMLMetadata {
func largestOpenGraphImageURL() -> String? {
guard let openGraphImages = openGraphProperties?.images, !openGraphImages.isEmpty else {
return nil
}
var bestImage: RSHTMLOpenGraphImage? = nil
for image in openGraphImages {
if bestImage == nil {
bestImage = image
continue
}
if image.height > bestImage!.height && image.width > bestImage!.width {
bestImage = image
}
}
guard let url = bestImage?.secureURL ?? bestImage?.url else {
return nil
}
// Bad ones we should ignore.
let badURLs = Set(["https://s0.wp.com/i/blank.jpg"])
guard !badURLs.contains(url) else {
return nil
}
return url
}
func largestAppleTouchIcon() -> String? {
guard let icons = appleTouchIcons, !icons.isEmpty else {
return nil
}
var bestImage: RSHTMLMetadataAppleTouchIcon? = nil
for image in icons {
if bestImage == nil {
bestImage = image
continue
}
if image.size.height > bestImage!.size.height && image.size.width > bestImage!.size.width {
bestImage = image;
}
}
return bestImage?.urlString
}
func bestWebsiteIconURL() -> String? {
// TODO: metadata icons sometimes theyre large enough to use here.
if let appleTouchIcon = largestAppleTouchIcon() {
return appleTouchIcon
}
if let openGraphImageURL = largestOpenGraphImageURL() {
return openGraphImageURL
}
return twitterProperties.imageURL
}
func bestFeaturedImageURL() -> String? {
if let openGraphImageURL = largestOpenGraphImageURL() {
return openGraphImageURL
}
return twitterProperties.imageURL
}
}