2017-11-27 04:57:45 +01:00
|
|
|
|
//
|
2017-11-27 05:40:07 +01:00
|
|
|
|
// RSHTMLMetadata+Extension.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
|
// NetNewsWire
|
2017-11-27 04:57:45 +01:00
|
|
|
|
//
|
|
|
|
|
// 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 {
|
2019-11-07 00:36:05 +01:00
|
|
|
|
if image.width / image.height > 2 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2017-11-27 04:57:45 +01:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-27 05:40:07 +01:00
|
|
|
|
func largestAppleTouchIcon() -> String? {
|
|
|
|
|
|
|
|
|
|
guard let icons = appleTouchIcons, !icons.isEmpty else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var bestImage: RSHTMLMetadataAppleTouchIcon? = nil
|
|
|
|
|
|
|
|
|
|
for image in icons {
|
2019-11-07 00:36:05 +01:00
|
|
|
|
if image.size.width / image.size.height > 2 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2017-11-27 05:40:07 +01:00
|
|
|
|
if bestImage == nil {
|
|
|
|
|
bestImage = image
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if image.size.height > bestImage!.size.height && image.size.width > bestImage!.size.width {
|
|
|
|
|
bestImage = image;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bestImage?.urlString
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-27 04:57:45 +01:00
|
|
|
|
func bestWebsiteIconURL() -> String? {
|
|
|
|
|
|
|
|
|
|
// TODO: metadata icons — sometimes they’re large enough to use here.
|
|
|
|
|
|
2017-11-27 05:40:07 +01:00
|
|
|
|
if let appleTouchIcon = largestAppleTouchIcon() {
|
|
|
|
|
return appleTouchIcon
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-27 04:57:45 +01:00
|
|
|
|
if let openGraphImageURL = largestOpenGraphImageURL() {
|
|
|
|
|
return openGraphImageURL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return twitterProperties.imageURL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func bestFeaturedImageURL() -> String? {
|
|
|
|
|
|
|
|
|
|
if let openGraphImageURL = largestOpenGraphImageURL() {
|
|
|
|
|
return openGraphImageURL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return twitterProperties.imageURL
|
|
|
|
|
}
|
|
|
|
|
}
|