2017-11-20 22:16:06 +01:00
|
|
|
//
|
|
|
|
// FaviconURLFinder.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2017-11-20 22:16:06 +01:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/20/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSParser
|
|
|
|
|
2019-11-26 02:54:09 +01:00
|
|
|
// The favicon URLs may be specified in the head section of the home page.
|
2017-11-20 22:16:06 +01:00
|
|
|
|
|
|
|
struct FaviconURLFinder {
|
|
|
|
|
2020-01-31 23:09:01 +01:00
|
|
|
private static var ignoredMimeTypes = [String]()
|
|
|
|
private static var ignoredExtensions = [String]()
|
2017-11-20 22:16:06 +01:00
|
|
|
|
2020-01-31 23:09:01 +01:00
|
|
|
static var ignoredTypes: [String]? {
|
|
|
|
didSet {
|
|
|
|
guard let ignoredTypes = ignoredTypes else {
|
|
|
|
return
|
|
|
|
}
|
2020-01-31 22:59:16 +01:00
|
|
|
|
|
|
|
for type in ignoredTypes {
|
|
|
|
if let mimeTypes = UTTypeCopyAllTagsWithClass(type as CFString, kUTTagClassMIMEType)?.takeRetainedValue() {
|
|
|
|
ignoredMimeTypes.append(contentsOf: mimeTypes as! [String])
|
|
|
|
}
|
|
|
|
if let extensions = UTTypeCopyAllTagsWithClass(type as CFString, kUTTagClassFilenameExtension)?.takeRetainedValue() {
|
|
|
|
ignoredExtensions.append(contentsOf: extensions as! [String])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 23:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds favicon URLs in a web page.
|
|
|
|
/// - Parameters:
|
|
|
|
/// - homePageURL: The page to search.
|
|
|
|
/// - completion: A closure called when the links have been found.
|
|
|
|
/// - urls: An array of favicon URLs as strings.
|
|
|
|
static func findFaviconURLs(with homePageURL: String, _ completion: @escaping (_ urls:[String]?) -> Void) {
|
|
|
|
|
|
|
|
guard let _ = URL(string: homePageURL) else {
|
|
|
|
completion(nil)
|
|
|
|
return
|
|
|
|
}
|
2020-01-31 22:59:16 +01:00
|
|
|
|
|
|
|
// If the favicon has an explicit type, check that for an ignored type; otherwise, check the file extension.
|
2017-12-18 19:20:28 +01:00
|
|
|
HTMLMetadataDownloader.downloadMetadata(for: homePageURL) { (htmlMetadata) in
|
2020-01-31 22:59:16 +01:00
|
|
|
let faviconURLs = htmlMetadata?.faviconLinks.filter({ (faviconLink) -> Bool in
|
|
|
|
if faviconLink.type != nil {
|
|
|
|
if ignoredMimeTypes.contains(faviconLink.type) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let url = URL(string: faviconLink.urlString!), ignoredExtensions.contains(url.pathExtension) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}).map { $0.urlString! }
|
|
|
|
|
|
|
|
completion(faviconURLs)
|
2017-12-14 04:46:03 +01:00
|
|
|
}
|
2017-11-20 22:16:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|